Published
Edited
Sep 4, 2021
Insert cell
Insert cell
keyPair = window.crypto.subtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 4096,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256"
},
true,
["encrypt", "decrypt"]
);
Insert cell
exported1 = crypto.subtle.exportKey('jwk', keyPair.publicKey)
Insert cell
imported = crypto.subtle.importKey('jwk', exported1, {
name: "RSA-OAEP",
//modulusLength: 4096,
//publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256"
}, true, exported1.key_ops)
Insert cell
encrypted1 = crypto.subtle.encrypt({
name: "RSA-OAEP"
},
imported, // (the public key from above, to prove that works)
new TextEncoder().encode("testing") )
Insert cell
decoded1 = new TextDecoder().decode(await crypto.subtle.decrypt({
name: "RSA-OAEP"
},
keyPair.privateKey,
encrypted1))
Insert cell
md`## Signing and verifying`
Insert cell
keyPair2 = window.crypto.subtle.generateKey(
{
name: "RSASSA-PKCS1-v1_5",
modulusLength: 4096,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256"
},
true,
["sign", "verify"]
);
Insert cell
known_message = new TextEncoder().encode("The only one who knows this ounce of words is just a token, is he who has a ton to tell that must remain unspoken.")
Insert cell
signature = await window.crypto.subtle.sign(
"RSASSA-PKCS1-v1_5",
keyPair2.privateKey,
known_message
)
Insert cell
result = await window.crypto.subtle.verify(
"RSASSA-PKCS1-v1_5",
keyPair2.publicKey,
_base64ToArrayBuffer(signature_as_text), // (demonstrates conversion -- see below)
known_message
)
Insert cell
new Uint8Array(signature)
Insert cell
md`## Converting between ArrayBuffer and string (for embedding in JSON)`
Insert cell
signature_as_text = _arrayBufferToBase64(signature)
Insert cell
out = "MIFqnXKlR7eG4sOD4vEiEIB8uaJwALP4fyVw6qZpHrvIpu8uW8erYxqjXZqLK5Is/xrkfneMqP+yBvSJhKPq4inujN0V7wP+tUEZwN8OfWbKSXYXuKwT03WjGmeWYPig5eyATwSzbjonki8ct9J9x9CvyLla4qW4if2TkAuMpIOs9lWv/nYh2qq4I9oEgpPC6+0Sypldaz/BLf28e3R33M5rmevI6jbTarMZk0hB7iXrBLajfRAdrENtem9v7nmKkQbe/GgcaboSW4kb9eVHWx8tD2bItBDDYnBdncOcWUBfE/Vf5ZGsykjyqy6WZC8yMYtViq9A9YsdBIF+sZUiU7jckYz25e5D6GUj7bayrmi11Dha3rHdsxtMXmLt2eZqqV6AGbMdGPhIb+5hnymkT/vg7i++UNiQO5GJ4p+4t3S86czFzZtA4RyylhFGG6fkVZr/bwVAvfuBxlC7k4LkJ9wr7LvHDeEnguOmMp5MHzKCxNm2ZWrxtdVJgrY5xsizn1zjCAcZZzZgm9xdRCeyFYD45O0vbQPx9SyU2N+IJGsfPr2+ATYbzliohMZ0OkNzjbBlG6fnUDjTabQv2vqMylnH+UPR0FIpWqu5qUAkHdeLXFPlBVaB399KJtHzIg5LCpxbGbi3Tya8i+S+L/nFc/esdMJeKWMrW5rju0/z624="
Insert cell
function _arrayBufferToBase64( 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 window.btoa( binary );
}
Insert cell
function _base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
Insert cell
exported = JSON.stringify(await crypto.subtle.exportKey('jwk', keyPair2.publicKey))
Insert cell
exported2 = crypto.subtle.exportKey('jwk', keyPair2.publicKey)
Insert cell
imported2 = crypto.subtle.importKey('jwk', exported2, {
name: "RSASSA-PKCS1-v1_5",
hash: "SHA-256"
}, true, exported2.key_ops)
Insert cell
result2 = await window.crypto.subtle.verify(
"RSASSA-PKCS1-v1_5",
keyPair2.publicKey,
_base64ToArrayBuffer(signature_as_text), // (demonstrates conversion -- see below)
known_message
)
Insert cell
// any array buffer can be digested into a collision-resistant one
_arrayBufferToBase64(await crypto.subtle.digest('SHA-512', signature))
Insert cell
exported3 = _arrayBufferToBase64(await crypto.subtle.exportKey('spki', keyPair2.publicKey))
Insert cell
imported3 = crypto.subtle.importKey('spki', _base64ToArrayBuffer(exported3), {
name: "RSASSA-PKCS1-v1_5",
hash: "SHA-256"
}, true, exported2.key_ops)
Insert cell
result3 = await window.crypto.subtle.verify(
"RSASSA-PKCS1-v1_5",
keyPair2.publicKey,
_base64ToArrayBuffer(signature_as_text), // (demonstrates conversion -- see below)
known_message
)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more