Files
core/library/sodium-plus/docs/SodiumPlus/short-input-hashing.md
2024-03-10 22:38:21 +00:00

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:

  1. {string|Buffer} input
  2. {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);
})();