by Anish
Posted on Tuesday september 25, 2018
This sample chapter extracted from the book, Cryptography for JavaScript Developers.
The Web crypto api describes using The RSA-PSS algorithm identifier is used to perform signing and verification using the RSASSA-PSS algorithm specified in [RFC3447], using the SHA hash functions defined in this specification and the mask generation formula MGF1.
The recognized algorithm name for this algorithm is "RSA-PSS".
hash algorithms reference
This is the web cryptography api example of performing ECDH generateKey and derivebits, and then using generate key to encrypt and decrypt the message in AES
Generate RSA-PSS Keys, then sign the message using the RSA private key
window.crypto.subtle.generateKey({
name: "RSA-PSS",
modulusLength: 2048, //can be 1024, 2048, or 4096
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {name: "SHA-256"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
},
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;
Exporting the RSA-PSS Keys in JWK for the demo purpose only, you can export the keys if you want to save it into the filesystems
// 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("rsapublic").value = publicKeyJson;
}
);
window.crypto.subtle.exportKey("jwk", key.privateKey).then(
function(keydata) {
privateKeyhold = keydata;
privateKeyJson = JSON.stringify(privateKeyhold);
document.getElementById("rsaprivate").value = privateKeyJson;
}
);
Signing the message, using the RSA private keys
window.crypto.subtle.sign({
name: "RSA-PSS",
saltLength: 128, //the length of the salt
},
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);
});
Signature Validation : This operation requires RSA public key
window.crypto.subtle.verify({
name: "RSA-PSS",
saltLength: 128, //the length of the salt
},
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