Skip to content

Instantly share code, notes, and snippets.

@eser
Created October 24, 2022 16:34
Show Gist options
  • Save eser/c20bd2dddcdb00dc5b2c40f32d074faf to your computer and use it in GitHub Desktop.
Save eser/c20bd2dddcdb00dc5b2c40f32d074faf to your computer and use it in GitHub Desktop.
const publicJwk = {
"kty": "RSA",
"kid": "",
"n": "",
"e": "AQAB"
};
const privateJwk = {
"kty": "RSA",
"kid": "",
"n": "",
"e": "AQAB",
"d": "",
"p": "",
"q": "",
"dp": "",
"dq": "",
"qi": ""
};
const test = async () => {
const privateKey = await globalThis.crypto.subtle.importKey(
"jwk",
privateJwk,
{ name: "RSA-PSS", hash: { name: "SHA-256" } },
false,
["sign"],
);
const publicKey = await globalThis.crypto.subtle.importKey(
"jwk",
publicJwk,
{ name: "RSA-PSS", hash: { name: "SHA-256" } },
false,
["verify"],
);
const encoded = new TextEncoder().encode("Hello World");
const signature = await globalThis.crypto.subtle.sign(
{
name: "RSA-PSS",
saltLength: 128, //the length of the salt
},
privateKey, //from generateKey or importKey above
encoded //ArrayBuffer of data you want to sign
)
const result = await globalThis.crypto.subtle.verify(
{
name: "RSA-PSS",
saltLength: 128, //the length of the salt
},
publicKey,
signature,
encoded
);
console.log(result);
};
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment