mirror of
https://framagit.org/hubzilla/core.git
synced 2026-06-21 00:52:33 -04:00
1.2 KiB
1.2 KiB
Short-input hashing
See also: Libsodium's documentation on its short-input hashing features.
crypto_shorthash
Calculate a fast hash for short inputs.
Parameters and their respective types:
{string|Buffer}input{CryptographyKey}key
Returns a Promise that resolves to a Buffer.
crypto_shorthash_keygen
Returns a CryptographyKey object containing a key appropriate
for the crypto_shorthash API.
Example for crypto_shorthash
Warning: You probably want
crypto_generichash()for most use-cases.crypto_shorthash()does not offer collision resistance.
const { SodiumPlus } = require('sodium-plus');
let sodium;
(async function () {
if (!sodium) sodium = await SodiumPlus.auto();
let key = await sodium.crypto_shorthash_keygen();
let mapped = {};
mapped['foo'] = (await sodium.crypto_shorthash('foo', key)).toString('hex');
mapped['bar'] = (await sodium.crypto_shorthash('bar', key)).toString('hex');
mapped['baz'] = (await sodium.crypto_shorthash('baz', key)).toString('hex');
console.log(mapped);
})();