Modernise example

This commit is contained in:
Emil Bay 2020-06-24 14:49:26 +02:00
parent 2ca6264f50
commit 40f9b887f4

View File

@ -1,19 +1,19 @@
var sodium = require('./') const sodium = require('./')
var key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES) const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES)
var nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES) const nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)
sodium.randombytes_buf(key) sodium.randombytes_buf(key)
sodium.randombytes_buf(nonce) sodium.randombytes_buf(nonce)
var message = Buffer.from('Hello, World!') const message = Buffer.from('Hello, World!')
var cipher = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES) const cipher = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES)
sodium.crypto_secretbox_easy(cipher, message, nonce, key) sodium.crypto_secretbox_easy(cipher, message, nonce, key)
console.log('Encrypted:', cipher) console.log('Encrypted:', cipher)
var plainText = Buffer.alloc(cipher.length - sodium.crypto_secretbox_MACBYTES) const plainText = Buffer.alloc(cipher.length - sodium.crypto_secretbox_MACBYTES)
sodium.crypto_secretbox_open_easy(plainText, cipher, nonce, key) sodium.crypto_secretbox_open_easy(plainText, cipher, nonce, key)