mirror of
https://framagit.org/hubzilla/core.git
synced 2026-06-21 00:52:33 -04:00
2.5 KiB
2.5 KiB
General-purpose cryptographic hash
See also: Libsodium's documentation on its generic hashing features.
crypto_generichash
General-purpose cryptographic hash (powered by BLAKE2).
Parameters and their respective types:
{Buffer}message{CryptographyKey|null}key (optional){number}output length (optional, defaults to 32)
Returns a Promise that resolves to a Buffer.
crypto_generichash_keygen
Returns a CryptographyKey object containing a key appropriate
for the crypto_generichash API.
crypto_generichash_init
Initialize a BLAKE2 hash context for stream hashing.
Parameters and their respective types:
{CryptographyKey|null}key (optional){number}output length (optional, defaults to 32)
Returns a Promise that resolves to... well, that depends on your backend.
- sodium-native returns a
CryptoGenericHashWrapobject. - libsodium-wrappers returns a number (a buffer's memory address)
crypto_generichash_update
Update the BLAKE2 hash state with a block of data.
Parameters and their respective types:
{*}hash state (see crypto_generichash_init()){string|Buffer}message chunk
Returns a Promise that resolves to void. Instead, state is updated in-place.
crypto_generichash_final
Obtain the final BLAKE2 hash output.
Parameters and their respective types:
{*}hash state (see crypto_generichash_init()){number}output length (optional, defaults to 32)
Returns a Promise that resolves to a Buffer.
Example for crypto_generichash
const { SodiumPlus } = require('sodium-plus');
let sodium;
(async function () {
if (!sodium) sodium = await SodiumPlus.auto();
let message = 'Any message can go here';
let hashed = await sodium.crypto_generichash(message);
console.log(hashed.toString('hex'));
let key = await sodium.crypto_generichash_keygen();
let hash2 = await sodium.crypto_generichash(message, key, 64);
let state = await sodium.crypto_generichash_init(key, 64);
await sodium.crypto_generichash_update(state, 'Any message ');
await sodium.crypto_generichash_update(state, 'can go here');
let hash3 = await sodium.crypto_generichash_final(state, 64);
if (!await sodium.sodium_memcmp(hash2, hash3)) {
throw new Error('Implementation is broken. You should never see this.');
}
console.log(hash2.toString('hex'));
})();