asymmetric-crypto
Encryption and signing using public-key cryptography (via
tweetnacl
)
Install
npm install asymmetric-crypto
This module can be used for Node.js as well as browsers using Browserify.
Usage
const crypto = // Generate a key pairconst keyPair = crypto// -> {// secretKey: 'KOy7fMWMkRc+QX8dzpfX9VwJKlc/+Zkyw5C7RGTXT920IjiKUdOSe/3sNnrETw7ej9TBFzsPyRfkWGMsGLAufQ==',// publicKey: 'tCI4ilHTknv97DZ6xE8O3o/UwRc7D8kX5FhjLBiwLn0='// } // Regenerate a key pair from the secret keyconst newKeyPair = crypto// -> {// secretKey: 'KOy7fMWMkRc+QX8dzpfX9VwJKlc/+Zkyw5C7RGTXT920IjiKUdOSe/3sNnrETw7ej9TBFzsPyRfkWGMsGLAufQ==',// publicKey: 'tCI4ilHTknv97DZ6xE8O3o/UwRc7D8kX5FhjLBiwLn0='// } const myKeyPair = cryptoconst theirKeyPair = crypto // Encrypt dataconst encrypted = crypto// -> {// data: '63tP2r8WQuJ+k+jzsd8pbT6WYPHMTafpeg==',// nonce: 'BDHALdoeBiGg7wJbVdfJhVQQyvpxrBSo'// } // Decrypt dataconst decrypted = crypto// -> 'some data' // Sign a messageconst message = 'some message'const signature = crypto// -> '8oz1aNkSBG1qvYhc+E2VBkgHSxCORGdsyf7LFQuLDmZvJt6vaEzHMIsofmTykMunhCrChEHT9Fgw3sp/W6+7Bw==' // Verify the signature on a messageconst validSignature = crypto// -> true
Tests
npm test
Internals
tweetnacl
for the cryptographic implementationtweetnacl-util
for converting into / from stringsed2curve
for converting Ed25519 keys into curve25519-xsalsa20-poly1305 keys (so you can encrypt and sign with the same key pair)fast-memoize
to make converting keys more efficient
Licence
MIT
Thanks to @pguth for the inspiration. 😄