Abstract encoding API of
libsodium
crypto_secretstream
Someday there will be a stream interface here too
var secretstream = require('secretstream-stream')
// Parameters
var header = Buffer.alloc(secretstream.HEADERBYTES)
var key = secretstream.keygen()
// Init encryption side, writing into header Buffer, which needs to be shared
// with decryption side
var tx = secretstream.encrypt(header, key)
var ciphertext = tx.encrypt(secretstream.TAG_MESSAGE, Buffer.from('Hello world!'))
// Setup the decrypt side
var rx = secretstream.decrypt(header, key)
var plaintext = rx.decrypt(ciphertext)
console.log(plaintext.equals(Buffer.from('Hello world!')), rx.decrypt.tag.equals(secretstream.TAG_MESSAGE))
tx.destroy()
rx.destroy()
secretstream.KEYBYTES
- Key sizesecretstream.HEADERBYTES
- Header sizesecretstream.ABYTES
- MAC size added to every message
secretstream.TAG_MESSAGE
secretstream.TAG_PUSH
secretstream.TAG_FINAL
secretstream.TAG_REKEY
Generate a new symmetric key for use with .encrypt
and .decrypt
. The key is
stored in a sodium Secure Buffer. You can also save a allocation by passing in
the key buffer, which must be at least .KEYBYTES
bytes.
Create an encrypt instance with key
, writing into header
. header
needs to
be passed the the decryption side somehow.
Encrypt Buffer plaintext
with added tag
using optional Buffer ad
, and
write into Buffer ciphertext
at offset
. ad
can be null
if unused, while
ciphertext
will be allocated if not given. offset
defalts to 0
.
Calculate the required length for a ciphertext
from plaintext
Buffer.
Number of bytes written into ciphertext
at last call to tx.encrypt
Destroys the internal state and zero all memory. Can only be called once,
you may never call encrypt
after and sets .bytes
to null
.
Create an decrypt instance with key
, using header
from encrypt
.
Decrypt Buffer ciphertext
using optional Buffer ad
, and
write into Buffer plaintext
at offset
. ad
can be null
if unused, while
plaintext
will be allocated if not given. offset
defalts to 0
.
Calculate the required length for a plaintext
from ciphertext
Buffer.
Number of bytes written into plaintext
at last call to rx.decrypt
A tag Buffer for the tag from the last decrypted ciphertext
. Should be
compared against one of the exported tags. Please review the libsodium
documentation
for how tags should be interpreted.
Destroys the internal state and zero all memory. Can only be called once,
you may never call encrypt
after and sets .bytes
and .tag
to null
.
npm install secretstream-stream