forked from thibmeu/rfc9500
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pem-jwk.js
55 lines (45 loc) · 1.33 KB
/
pem-jwk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const fs = require('fs');
const { createPublicKey, createPrivateKey } = require('crypto');
const jose = require('jose');
async function pemToJwkWithThumbprint(pemFilePath) {
try {
// Read the PEM file
const pemContent = fs.readFileSync(pemFilePath, 'utf8');
let key;
if (pemFilePath.endsWith("pub.pem")) {
key = createPublicKey(pemContent);
} else {
key = createPrivateKey(pemContent);
}
// Convert the public key to JWK
const jwk = await jose.exportJWK(key);
// Calculate the JWK thumbprint
const thumbprint = await jose.calculateJwkThumbprint(jwk);
// Add the thumbprint as the 'kid'
jwk.kid = thumbprint;
return jwk;
} catch (error) {
console.error('Error converting PEM to JWK:', error);
throw error;
}
}
// Get the PEM file path from command line arguments
const args = process.argv.slice(2);
if (args.length !== 1) {
console.error('Usage: node script.js <path_to_pem_file>');
process.exit(1);
}
const pemFilePath = args[0];
// Check if the file exists
if (!fs.existsSync(pemFilePath)) {
console.error(`File not found: ${pemFilePath}`);
process.exit(1);
}
pemToJwkWithThumbprint(pemFilePath)
.then(jwk => {
console.log(JSON.stringify(jwk, null, 2));
})
.catch(error => {
console.error('Conversion failed:', error);
process.exit(1);
});