Skip to content

Instantly share code, notes, and snippets.

@jkyberneees
Last active September 3, 2018 22:08
Show Gist options
  • Select an option

  • Save jkyberneees/47c65609685e915b0c9e0ade43187978 to your computer and use it in GitHub Desktop.

Select an option

Save jkyberneees/47c65609685e915b0c9e0ade43187978 to your computer and use it in GitHub Desktop.
const nacl = require('tweetnacl')
nacl.util = require('tweetnacl-util')
// generating Bob key pair
const bob = nacl.box.keyPair()
// generating Alice key pair
const alice = nacl.box.keyPair()
// generating one time nonce for encryption
const nonce = nacl.randomBytes(24)
// Bob encrypt message for Alice (only requires Alice public key)
const utf8 = 'Hello Alice'
const box = nacl.box(
nacl.util.decodeUTF8(utf8),
nonce,
alice.publicKey,
bob.secretKey
)
const message = {box, nonce} // somehow send this message to Alice
// Alice decrypt message from Bob (only requires Bob public key)
const payload = nacl.box.open(message.box, message.nonce, bob.publicKey, alice.secretKey)
// assert.equal(utf8, nacl.util.encodeUTF8(payload))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment