by Anish
Posted on Monday september 24, 2018
This sample chapter extracted from the book, Cryptography for JavaScript Developers.
The Web crypto api ECDSA algorithm identifier is used to perform signing and verification using the ECDSA algorithm specified in RFC6090 and using the SHA hash functions and elliptic curves defined in this specification.
This is the web cryptography api example of performing ecdsa message signing and verifying the signature
Generate ECDSA Keys using the named curved P-256, P-384, or P-521, The generated ECDSA keys is outputed in JWK format for demo purpose only
window.crypto.subtle.generateKey({
name: "ECDSA",
namedCurve: curve, //can be "P-256", "P-384", or "P-521"
},
true, //whether the key is extractable (i.e. can be used in exportKey)
["sign", "verify"] //can be any combination of "sign" and "verify"
)
.then(function(key) {
publicKey = key.publicKey;
privateKey = key.privateKey;
// For Demo Purpos Only Exported in JWK format
window.crypto.subtle.exportKey("jwk", key.publicKey).then(
function(keydata) {
publicKeyhold = keydata;
publicKeyJson = JSON.stringify(publicKeyhold);
document.getElementById("ecdsapublic").value = publicKeyJson;
}
);
window.crypto.subtle.exportKey("jwk", key.privateKey).then(
function(keydata) {
privateKeyhold = keydata;
privateKeyJson = JSON.stringify(privateKeyhold);
document.getElementById("ecdsaprivate").value = privateKeyJson;
}
);
ECDSA Sign the Message and generate signature
window.crypto.subtle.sign({
name: "ECDSA",
hash: {
name: "SHA-256"
}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
},
privateKey, //from generateKey or importKey above
asciiToUint8Array(plainText) //ArrayBuffer of data you want to sign
)
.then(function(signature) {
//returns an ArrayBuffer containing the signature
document.getElementById("cipherText").value = bytesToHexString(signature);
})
.catch(function(err) {
console.error(err);
});
})
.catch(function(err) {
console.error(err);
});
ECDSA Verify the message signature
window.crypto.subtle.verify({
name: "ECDSA",
hash: {name: "SHA-256"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
},
publicKey, //from generateKey or importKey above
hexStringToUint8Array(cipherText), //ArrayBuffer of the data
asciiToUint8Array(plainText)
)
.then(function(decrypted) {
alert("Verified " + decrypted);
})
.catch(function(err) {
console.error(err);
});
Thanku for reading !!! Give a Share for Support
Instead of directly asking for donations, I'm thrilled to offer you all nine of my books for just $9 on leanpub By grabbing this bundle you not only help cover my coffee, beer, and Amazon bills but also play a crucial role in advancing and refining this project. Your contribution is indispensable, and I'm genuinely grateful for your involvement in this journey!
Any private key value that you enter or we generate is not stored on this site, this tool is provided via an HTTPS URL to ensure that private keys cannot be stolen, for extra security run this software on your network, no cloud dependency