Generic Hashing
Bindings for the crypto_generichash API. See the libsodium crypto_generichash docs for more information.
Constants
Buffer lengths (integer)
crypto_generichash_BYTES
crypto_generichash_BYTES_MIN
crypto_generichash_BYTES_MAX
crypto_generichash_KEYBYTES
crypto_generichash_KEYBYTES_MIN
crypto_generichash_KEYBYTES_MAX
crypto_generichash_STATEBYTES
String constants (string)
crypto_generichash_PRIMITIVE
crypto_generichash
sodium.crypto_generichash(out, in, [key])
Hashes a value with an optional key using the generichash
method.
out
should be abuffer
of length withincrypto_generichash_BYTES_MIN
-crypto_generichash_BYTES_MAX
in
should be abuffer
of any lengthkey
is an optionalbuffer
of length withincrypto_generichash_KEYBYTES_MIN
-crypto_generichash_KEYBYTES_MAX
The generated hash is stored in out
.
Also exposes crypto_generichash_BYTES
and crypto_generichash_KEYBYTES
that can be used as "default" buffer
sizes.
crypto_generichash_batch
sodium.crypto_generichash_batch(out, inArray, [key])
Same as crypto_generichash
, except that this hashes an array of buffer
's instead of a single one.
Instance API
crypto_generichash_instance
var instance = sodium.crypto_generichash_instance([key], [outlen])
Creates a generichash
instance that can hash a stream of input buffer
's.
key
is an optionalbuffer
as aboveoutlen
is thebuffer
size of your output
instance.update
instance.update(in)
Updates the instance with a new piece of data.
in
should be abuffer
of any size
instance.final
instance.final(out)
Finalizes the instance.
out
should be abuffer
as above with the same length you gave when creating the instance
The generated hash is stored in out
.
Stateful API
Replaces the above instance implementation in the N-API release
crypto_generichash_init
var state = Buffer.alloc(crypto_generichash_STATEBYTES)
sodium.crypto_generichash_init(state, [key], outlen)
Initialise a new hash state with an optional key and the desired output length.
state
must be a buffer of lengthcrypto_generichash_STATEBYTES
byteskey
is an optional buffer as above
crypto_generichash_update
sodium.crypto_generichash_update(state, in)
Update a hash state with a given input.
state
must be a buffer of lengthcrypto_generichash_STATEBYTES
bytesin
should be a abuffer
of any length
crypto_generichash_final(state, out)
sodium.crypto_generichash_final(state, out)
Finalize a given hash state and write the digest to output
buffer
state
must be a buffer of lengthcrypto_generichash_STATEBYTES
bytesout
must be abuffer
withcrypto_generichash_BYTES_MIN <= out.byteLength <= crypto_generichash_BYTES_MAX
out.byteLength
should equaloutlen
specified whencrypto_generichash_init
was called