Stream Encryption
Bindings for the crypto_secretstream API. See the libsodium crypto_secretstream docs for more information.
Constants
Buffer lengths (integer)
crypto_secretstream_xchacha20poly1305_ABYTEScrypto_secretstream_xchacha20poly1305_HEADERBYTEScrypto_secretstream_xchacha20poly1305_KEYBYTEScrypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAXcrypto_secretstream_xchacha20poly1305_TAGBYTES- NOTE: Unofficial constantcrypto_secretstream_xchacha20poly1305_STATEBYTES
Message tags (buffer)
crypto_secretstream_xchacha20poly1305_TAG_MESSAGEcrypto_secretstream_xchacha20poly1305_TAG_PUSHcrypto_secretstream_xchacha20poly1305_TAG_REKEYcrypto_secretstream_xchacha20poly1305_TAG_FINAL
crypto_secretstream_xchacha20poly1305_keygen
sodium.crypto_secretstream_xchacha20poly1305_keygen(k)
Generates a new encryption key.
kshould be abufferof lengthcrypto_secretstream_xchacha20poly1305_KEYBYTES
The generated key is stored in k.
Opaque API
crypto_secretstream_xchacha20poly1305_state_new
var state = sodium.crypto_secretstream_xchacha20poly1305_state_new()
Creates a new stream state. Returns an opaque object used in the next methods.
crypto_secretstream_xchacha20poly1305_init_push
sodium.crypto_secretstream_xchacha20poly1305_init_push(state, header, k)
Initializes state from the writer side with message header and encryption key. The header must be sent or stored with the stream. The key must be exchanged securely with the receiving / reading side.
stateshould be an opaque state objectheadershould be abufferof lengthcrypto_secretstream_xchacha20poly1305_HEADERBYTESkshould be abufferof lengthcrypto_secretstream_xchacha20poly1305_KEYBYTES
crypto_secretstream_xchacha20poly1305_push
var mlen = sodium.crypto_secretstream_xchacha20poly1305_push(state, c, m, [ad], tag)
Encrypts a message with a certain tag and optional additional data ad.
stateshould be an opaque state objectcshould be abufferof lengthm.length + crypto_secretstream_xchacha20poly1305_ABYTESmshould be abufferadis optional and should benullorbuffer. Included in the computation of authentication tag appended to the mtagshould be abufferof lengthcrypto_secretstream_xchacha20poly1305_TAGBYTES
Note that tag should be one of the crypto_secretstream_xchacha20poly1305_TAG_* constants. Returns number of encrypted bytes written to c.
crypto_secretstream_xchacha20poly1305_init_pull
sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header, k)
Initializes state from the reader side with message header and encryption key. The header must be retrieved from somewhere. The key must be exchanged securely with the sending / writing side.
stateshould be an opaque state objectheadershould be abufferof lengthcrypto_secretstream_xchacha20poly1305_HEADERBYTESkshould be abufferof lengthcrypto_secretstream_xchacha20poly1305_KEYBYTES
crypto_secretstream_xchacha20poly1305_pull
var clen = sodium.crypto_secretstream_xchacha20poly1305_pull(state, m, tag, c, [ad])
Decrypts a message with optional additional data ad, and writes message tag to tag. Make sure to check this!
stateshould be an opaque state objectmshould be abufferof lengthc.length - crypto_secretstream_xchacha20poly1305_ABYTEStagshould be abufferof lengthcrypto_secretstream_xchacha20poly1305_TAGBYTESadis optional and should benullorbuffer. Included in the computation of the authentication tag appended to the m
Note that tag should be one of the crypto_secretstream_xchacha20poly1305_TAG_* constants. Returns number of decrypted bytes written to m.
crypto_secretstream_xchacha20poly1305_rekey
sodium.crypto_secretstream_xchacha20poly1305_rekey(state)
Rekeys the opaque state object.
Stateful API
Replaces the above instance implementation in the N-API release.
var state = Buffer.alloc(sodium.crypto_secretstream_xchacha20poly1305_STATEBYTES)
Create a new buffer to hold stream state.
crypto_secretstream_xchacha20poly1305_init_push
sodium.crypto_secretstream_xchacha20poly1305_init_push(state, header, k)
Initializes state from the writer side with message header and encryption key. The header must be sent or stored with the stream. The key must be exchanged securely with the receiving / reading side.
stateshould be abufferof lengthcrypto_secretstream_xchacha20_poly1305_STATEBYTESbytesheadershould be abufferof lengthcrypto_secretstream_xchacha20poly1305_HEADERBYTESkshould be abufferof lengthcrypto_secretstream_xchacha20poly1305_KEYBYTES
crypto_secretstream_xchacha20poly1305_push
var mlen = sodium.crypto_secretstream_xchacha20poly1305_push(state, c, m, [ad], tag)
Encrypts a message with a certain tag and optional additional data ad.
stateshould be abufferof lengthcrypto_secretstream_xchacha20_poly1305_STATEBYTESbytescshould be abufferof lengthm.length + crypto_secretstream_xchacha20poly1305_ABYTESmshould be abufferadis optional and should benullorbuffer. Included in the computation of authentication tag appended to the mtagshould be abufferof lengthcrypto_secretstream_xchacha20poly1305_TAGBYTES
Note that tag should be one of the crypto_secretstream_xchacha20poly1305_TAG_* constants. Returns number of encrypted bytes written to c.
crypto_secretstream_xchacha20poly1305_init_pull
sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header, k)
Initializes state from the reader side with message header and encryption key. The header must be retrieved from somewhere. The key must be exchanged securely with the sending / writing side.
stateshould be abufferof lengthcrypto_secretstream_xchacha20_poly1305_STATEBYTESbytesheadershould be abufferof lengthcrypto_secretstream_xchacha20poly1305_HEADERBYTESkshould be abufferof lengthcrypto_secretstream_xchacha20poly1305_KEYBYTES
crypto_secretstream_xchacha20poly1305_pull
var clen = sodium.crypto_secretstream_xchacha20poly1305_pull(state, m, tag, c, [ad])
Decrypts a message with optional additional data ad, and writes message tag to tag. Make sure to check this!
stateshould be abufferof lengthcrypto_secretstream_xchacha20_poly1305_STATEBYTESbytesmshould be abufferof lengthc.length - crypto_secretstream_xchacha20poly1305_ABYTEStagshould be abufferof lengthcrypto_secretstream_xchacha20poly1305_TAGBYTESadis optional and should benullorbuffer. Included in the computation of the authentication tag appended to the m
Note that tag should be one of the crypto_secretstream_xchacha20poly1305_TAG_* constants. Returns number of decrypted bytes written to m.
crypto_secretstream_xchacha20poly1305_rekey
sodium.crypto_secretstream_xchacha20poly1305_rekey(state)
Rekeys the state buffer.
