-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
67 lines (57 loc) · 1.99 KB
/
app.js
File metadata and controls
67 lines (57 loc) · 1.99 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
const { generateKeyPair } = require("crypto");
const jose = require("jose");
const alg = "ES256K"
const _getKeyPair = async () => {
return new Promise((resolve, reject) => {
generateKeyPair("ec", {
namedCurve: "secp256k1",
publicKeyEncoding: { type: "spki", format: "pem" },
privateKeyEncoding: { type: "pkcs8", format: "pem" }
}, (err, publicKey, privateKey) => {
if (err) return reject(err);
resolve({publicKey, privateKey});
});
});
};
async function main() {
const keyPair = await _getKeyPair();
console.log("publicKey: ", keyPair.publicKey.toString());
console.log("PrivateKey: ", keyPair.privateKey.toString());
const key = `${keyPair.privateKey.toString()}`;
//const privKey = await jose.importPKCS8(keyPair.privateKey.toString, alg);
const privKey = await jose.importPKCS8(key, alg);
const jwt = await new jose.SignJWT( {'urn:example:clami': true })
.setProtectedHeader({ alg })
.setIssuedAt()
.setIssuer('urn:example:issuer')
.setAudience('urn:example:audience')
.sign(privKey);
console.log(jwt);
const pubKey = await jose.importSPKI(`${keyPair.publicKey.toString()}`, alg);
// const { payload, protectedHeader } = await jose.jwtVerify(jwt, pubKey, {
// issuer: 'urn:example:issuer',
// audience: 'urn:example:audience',
// });
const { payload, protectedHeader } = await jose.jwtVerify(jwt, pubKey);
console.log(protectedHeader);
console.log(payload);
}
main();
//console.log("PrivateKey is : ", privateKey.toString());
/*
console.log(privKey.toString());
} else {
console.log("Error is: ", err);
}});
/*
const jwt = require("jsonwebtoken");
const secret = secp256k1.generateKeys();
const token = jwt.sign({ data: "jwt payload" }, secret, { algorithm: "ES256" });
const decoded = jwt.verify(token, secret, { algorithms: ["ES256"] }, (err, decoded) => {
if (err) {
console.error("JWT verification failed", err);
} else {
console.log("JWT verification succeeded", decoded);
}
});
*/