Skip to content

Instantly share code, notes, and snippets.

@fritzy
Created April 13, 2018 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fritzy/a8f64dc4e0e9613acbdb89c0c46f4046 to your computer and use it in GitHub Desktop.
Save fritzy/a8f64dc4e0e9613acbdb89c0c46f4046 to your computer and use it in GitHub Desktop.
Test of node-webcrypto-ossl
const RSA = { name: 'RSASSA-PKCS1-v1_5' };
const testValue = 'I am the very model of a modern major general.';
const testValue2 = 'I am the very model of a modern majr general.';
//if browser, uses window.crypto, if node wraps openssl
const WebCrypto = require('node-webcrypto-ossl');
const crypto = new WebCrypto();
(async () => {
console.log('generating key');
let keypair;
try {
keypair = await crypto.subtle.generateKey({
name: RSA.name,
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: { name: 'SHA-512' },
}, true, ['sign', 'verify']);
} catch (e) {
console.error(e);
}
console.log('keypair:', keypair);
console.log('signing...');
const sig = await crypto.subtle.sign(RSA, keypair.privateKey, Buffer.from(testValue, 'utf8'));
console.log('sig', sig);
const verified = await crypto.subtle.verify(RSA, keypair.publicKey, sig, Buffer.from(testValue2, 'utf8'));
console.log('verified:', verified);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment