Last active
May 29, 2023 06:19
-
-
Save CrowdHailer/aca60c4633c94ba73f507a7bd855366c to your computer and use it in GitHub Desktop.
Revisions
-
CrowdHailer revised this gist
Mar 8, 2019 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ // NEEDS TO RUN IN SECURE (HTTPS) CONTEXT. async function run() { // New key pair var keyPair = await window.crypto.subtle.generateKey( -
CrowdHailer created this gist
Mar 8, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,70 @@ async function run() { // New key pair var keyPair = await window.crypto.subtle.generateKey( { name: "ECDSA", namedCurve: "P-521" }, true, ["sign", "verify"] ); // Format public key var publicPEM = spkiToPEM(await window.crypto.subtle.exportKey( "spki", keyPair.publicKey )); console.log(publicPEM) // Encode message var message = "abc"; var encoder = new TextEncoder(); var bytes = (encoder).encode(message); var signature = await window.crypto.subtle.sign( { name: "ECDSA", hash: {name: "SHA-256"}, }, keyPair.privateKey, bytes ); var encodedSignature = window.btoa(arrayBufferToString(signature)); console.log(encodedSignature); } // https://stackoverflow.com/questions/40314257/export-webcrypto-key-to-pem-format function spkiToPEM(keydata){ var keydataS = arrayBufferToString(keydata); var keydataB64 = window.btoa(keydataS); var keydataB64Pem = formatAsPem(keydataB64); return keydataB64Pem; } function arrayBufferToString( buffer ) { var binary = ''; var bytes = new Uint8Array( buffer ); var len = bytes.byteLength; for (var i = 0; i < len; i++) { binary += String.fromCharCode( bytes[ i ] ); } return binary; } function formatAsPem(str) { var finalString = '-----BEGIN PUBLIC KEY-----\n'; while(str.length > 0) { finalString += str.substring(0, 64) + '\n'; str = str.substring(64); } finalString = finalString + "-----END PUBLIC KEY-----"; return finalString; } run(); // Example out put for encodedSignature and publicPEM included below This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ message = "abc" encoded_signature = "AOqjfvb1P0fYdbnq+f3XsbOR/Lylq4csC5e1Ks4cKRTcWKQyH4CVr/XRHCmZDEWOpaA8gwuM3Z3qD21vMaixySUPASTDnd1J8shNx51MCbONIReTNuH53kzxykLpf0riSyMbEBQtie4/pxlrolUOsDja2f/ikgM/lGXtCTXz2faV4m2Y" public_PEM = "-----BEGIN PUBLIC KEY----- MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQB7PNQVbADLNyobtijE5NVZUvHs74h iMntCbp0C8pdU1IQRWlAfDeEs/iuxA32VARw9Q5/0mim8Si8JcpCJnhS0u8AESMf Ux3WqzHhB33t4q3iPsJbM7zmN91QNnbYErrGqEDCmSruPpKw1iK5dJ3/xQZbkpmR ztoVwrZoCoGUu+WTqEI= -----END PUBLIC KEY-----" signature = Base.decode64(encoded_signature) [key_entry] = :public_key.pem_decode(public_PEM) public_key = :public_key.pem_entry_decode(key_entry) :public_key.verify( message, :sha256, signature, public_key ) # returns false