sodium-javascript/example.js

23 lines
666 B
JavaScript
Raw Normal View History

2017-01-24 10:41:06 +00:00
var sodium = require('./')
2020-06-24 12:08:55 +00:00
var key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES)
var nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)
2017-01-24 10:41:06 +00:00
sodium.randombytes_buf(key)
sodium.randombytes_buf(nonce)
2020-06-24 12:08:55 +00:00
var message = Buffer.from('Hello, World!')
var cipher = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES)
2017-01-24 10:41:06 +00:00
sodium.crypto_secretbox_easy(cipher, message, nonce, key)
console.log('Encrypted:', cipher)
2020-06-24 12:08:55 +00:00
var plainText = Buffer.alloc(cipher.length - sodium.crypto_secretbox_MACBYTES)
2017-01-24 10:41:06 +00:00
sodium.crypto_secretbox_open_easy(plainText, cipher, nonce, key)
console.log('Plaintext:', plainText.toString())
if (typeof window !== 'undefined') window.close()