-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Ed25519 sign-verify example (#247)
* Add Ed25519 sign-verify example * Create ed25519.js * Link to Chromium and Chrome command-line switch to enable the feature * Close </a> * Apply suggestions from code review Co-authored-by: Daniel Huigens <[email protected]> * docs(web-crypto): add link to browser compat for ed25519, formatting * Update web-crypto/sign-verify/index.html Co-authored-by: Daniel Huigens <[email protected]> --------- Co-authored-by: Brian Thomas Smith <[email protected]> Co-authored-by: Daniel Huigens <[email protected]>
- Loading branch information
1 parent
d529271
commit 9189afb
Showing
3 changed files
with
254 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
(() => { | ||
/* | ||
Store the calculated signature here, so we can verify it later. | ||
*/ | ||
let signature; | ||
|
||
/* | ||
Fetch the contents of the "message" textbox, and encode it | ||
in a form we can use for sign operation. | ||
*/ | ||
function getMessageEncoding() { | ||
const messageBox = document.querySelector("#ed25519-message"); | ||
let message = messageBox.value; | ||
let enc = new TextEncoder(); | ||
return enc.encode(message); | ||
} | ||
|
||
/* | ||
Get the encoded message-to-sign, sign it and display a representation | ||
of the first part of it in the "signature" element. | ||
*/ | ||
async function signMessage(privateKey) { | ||
const signatureValue = document.querySelector(".ed25519 .signature-value"); | ||
signatureValue.classList.remove("valid", "invalid"); | ||
|
||
let encoded = getMessageEncoding(); | ||
signature = await window.crypto.subtle.sign("Ed25519", privateKey, encoded); | ||
|
||
signatureValue.classList.add("fade-in"); | ||
signatureValue.addEventListener("animationend", () => { | ||
signatureValue.classList.remove("fade-in"); | ||
}); | ||
let buffer = new Uint8Array(signature, 0, 5); | ||
signatureValue.textContent = `${buffer}...[${signature.byteLength} bytes total]`; | ||
} | ||
|
||
/* | ||
Fetch the encoded message-to-sign and verify it against the stored signature. | ||
* If it checks out, set the "valid" class on the signature. | ||
* Otherwise set the "invalid" class. | ||
*/ | ||
async function verifyMessage(publicKey) { | ||
const signatureValue = document.querySelector(".ed25519 .signature-value"); | ||
signatureValue.classList.remove("valid", "invalid"); | ||
|
||
let encoded = getMessageEncoding(); | ||
let result = await window.crypto.subtle.verify( | ||
"Ed25519", | ||
publicKey, | ||
signature, | ||
encoded | ||
); | ||
|
||
signatureValue.classList.add(result ? "valid" : "invalid"); | ||
} | ||
|
||
/* | ||
Generate a sign/verify key, then set up event listeners | ||
on the "Sign" and "Verify" buttons. | ||
*/ | ||
window.crypto.subtle | ||
.generateKey("Ed25519", true, ["sign", "verify"]) | ||
.then((keyPair) => { | ||
const signButton = document.querySelector(".ed25519 .sign-button"); | ||
signButton.addEventListener("click", () => { | ||
signMessage(keyPair.privateKey); | ||
}); | ||
|
||
const verifyButton = document.querySelector(".ed25519 .verify-button"); | ||
verifyButton.addEventListener("click", () => { | ||
verifyMessage(keyPair.publicKey); | ||
}); | ||
}); | ||
})(); |
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
Oops, something went wrong.