Guest User

Untitled

a guest
Apr 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. const RSA = { name: 'RSASSA-PKCS1-v1_5' };
  2. const testValue = 'I am the very model of a modern major general.';
  3. const testValue2 = 'I am the very model of a modern majr general.';
  4.  
  5. //if browser, uses window.crypto, if node wraps openssl
  6. const WebCrypto = require('node-webcrypto-ossl');
  7. const crypto = new WebCrypto();
  8.  
  9. (async () => {
  10. console.log('generating key');
  11. let keypair;
  12. try {
  13. keypair = await crypto.subtle.generateKey({
  14. name: RSA.name,
  15. modulusLength: 2048,
  16. publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
  17. hash: { name: 'SHA-512' },
  18. }, true, ['sign', 'verify']);
  19. } catch (e) {
  20. console.error(e);
  21. }
  22. console.log('keypair:', keypair);
  23. console.log('signing...');
  24. const sig = await crypto.subtle.sign(RSA, keypair.privateKey, Buffer.from(testValue, 'utf8'));
  25. console.log('sig', sig);
  26. const verified = await crypto.subtle.verify(RSA, keypair.publicKey, sig, Buffer.from(testValue2, 'utf8'));
  27. console.log('verified:', verified);
  28. })()
Add Comment
Please, Sign In to add comment