UNPKG

678 BJavaScriptView Raw
1const sodium = require('./')
2
3const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES)
4const nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)
5
6sodium.randombytes_buf(key)
7sodium.randombytes_buf(nonce)
8
9const message = Buffer.from('Hello, World!')
10const cipher = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES)
11
12sodium.crypto_secretbox_easy(cipher, message, nonce, key)
13
14console.log('Encrypted:', cipher)
15
16const plainText = Buffer.alloc(cipher.length - sodium.crypto_secretbox_MACBYTES)
17
18sodium.crypto_secretbox_open_easy(plainText, cipher, nonce, key)
19
20console.log('Plaintext:', plainText.toString())
21
22if (typeof window !== 'undefined') window.close()