generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from whilefoo/action-signature
- Loading branch information
Showing
8 changed files
with
120 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,25 @@ | ||
export async function verifySignature(publicKeyPem: string, payload: unknown, signature: string) { | ||
const pemContents = publicKeyPem.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "").trim(); | ||
const binaryDer = Uint8Array.from(atob(pemContents), (c) => c.charCodeAt(0)); | ||
try { | ||
const pemContents = publicKeyPem.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "").trim(); | ||
const binaryDer = Uint8Array.from(atob(pemContents), (c) => c.charCodeAt(0)); | ||
|
||
const publicKey = await crypto.subtle.importKey( | ||
"spki", | ||
binaryDer, | ||
{ | ||
name: "RSASSA-PKCS1-v1_5", | ||
hash: "SHA-256", | ||
}, | ||
true, | ||
["verify"] | ||
); | ||
const publicKey = await crypto.subtle.importKey( | ||
"spki", | ||
binaryDer, | ||
{ | ||
name: "RSASSA-PKCS1-v1_5", | ||
hash: "SHA-256", | ||
}, | ||
true, | ||
["verify"] | ||
); | ||
|
||
const signatureArray = Uint8Array.from(atob(signature), (c) => c.charCodeAt(0)); | ||
const dataArray = new TextEncoder().encode(JSON.stringify(payload)); | ||
const signatureArray = Uint8Array.from(atob(signature), (c) => c.charCodeAt(0)); | ||
const dataArray = new TextEncoder().encode(JSON.stringify(payload)); | ||
|
||
return await crypto.subtle.verify("RSASSA-PKCS1-v1_5", publicKey, signatureArray, dataArray); | ||
return await crypto.subtle.verify("RSASSA-PKCS1-v1_5", publicKey, signatureArray, dataArray); | ||
} catch (error) { | ||
console.error(error); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters