Created
April 13, 2018 21:20
-
-
Save fritzy/a8f64dc4e0e9613acbdb89c0c46f4046 to your computer and use it in GitHub Desktop.
Test of node-webcrypto-ossl
This file contains hidden or 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
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