dchest / tweetnacl-js

Port of TweetNaCl cryptographic library to JavaScript
https://tweetnacl.js.org
The Unlicense
1.77k stars 293 forks source link

Hash with adjustable length #164

Closed AndreasGassmann closed 5 years ago

AndreasGassmann commented 5 years ago

We currently use both libsodium and this library in our project. We do everything with nacl, except in these 2 cases:

sodium.crypto_generichash(20, Buffer.from(publicKey, 'hex')) sodium.crypto_generichash(32, operationBytes)

I would like to get rid of libsodium and only use tweetnacl-js, but I cannot seem to get this working with nacl because the length of the hash is not configurable.

Am I overlooking something, or is this not possible with tweetnacl-js?

dchest commented 5 years ago

libsodium's generic hash is BLAKE2b, and is not included in the original TweetNaCl, which TweetNaCl-js is a port of.

I have a JavaScript implementation of it here: https://github.com/StableLib/stablelib/tree/master/packages/blake2b (npm install @stablelib/blake2b)

The easy-to-use function is

export function hash(data: Uint8Array, digestLength = DIGEST_LENGTH, config?: Config): Uint8Array

that is

hash(Buffer.from(publicKey, 'hex'), 20)
hash(operationBytes, 32)

for your case.