queicherius / asymmetric-crypto

Encryption and signing using public-key cryptography (via tweetnacl)
MIT License
16 stars 1 forks source link
maintained npm-package

asymmetric-crypto

Build Status Coverage Status Greenkeeper badge

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 = require('asymmetric-crypto')

// Generate a key pair
const keyPair = crypto.keyPair()
// -> {
//   secretKey: 'KOy7fMWMkRc+QX8dzpfX9VwJKlc/+Zkyw5C7RGTXT920IjiKUdOSe/3sNnrETw7ej9TBFzsPyRfkWGMsGLAufQ==',
//   publicKey: 'tCI4ilHTknv97DZ6xE8O3o/UwRc7D8kX5FhjLBiwLn0='
// }

// Regenerate a key pair from the secret key
const newKeyPair = crypto.fromSecretKey(keyPair.secretKey)
// -> {
//   secretKey: 'KOy7fMWMkRc+QX8dzpfX9VwJKlc/+Zkyw5C7RGTXT920IjiKUdOSe/3sNnrETw7ej9TBFzsPyRfkWGMsGLAufQ==',
//   publicKey: 'tCI4ilHTknv97DZ6xE8O3o/UwRc7D8kX5FhjLBiwLn0='
// }

const myKeyPair = crypto.keyPair()
const theirKeyPair = crypto.keyPair()

// Encrypt data
const encrypted = crypto.encrypt('some data', theirKeyPair.publicKey, myKeyPair.secretKey)
// -> {
//   data: '63tP2r8WQuJ+k+jzsd8pbT6WYPHMTafpeg==',
//   nonce: 'BDHALdoeBiGg7wJbVdfJhVQQyvpxrBSo'
// }

// Decrypt data
const decrypted = crypto.decrypt(encrypted.data, encrypted.nonce, myKeyPair.publicKey, theirKeyPair.secretKey)
// -> 'some data'

// Sign a message
const message = 'some message'
const signature = crypto.sign(message, myKeyPair.secretKey)
// -> '8oz1aNkSBG1qvYhc+E2VBkgHSxCORGdsyf7LFQuLDmZvJt6vaEzHMIsofmTykMunhCrChEHT9Fgw3sp/W6+7Bw=='

// Verify the signature on a message
const validSignature = crypto.verify(message, signature, myKeyPair.publicKey)
// -> true

Tests

npm test

Internals

Licence

MIT


Thanks to @pguth for the inspiration. :smile: