I am using the crypto module of node.js to generate a SHA256 hash like this:
const key = crypto.createHmac('sha256', data).digest('hex');
Now, tweetnacl throws the error: bad key size
when the key is passed in the secretbox
:
nacl.secretbox(data, Rnonc, key);
The parameters are converted into Uint8Array since the secretbox
requires arguments to be Uint8Array.
The error: bad key size
is throw from here in tweetnacl
since the crypto_secretbox_KEYBYTES
is defined as 32
here. The problem is the key returned from crypto
is in not 32 bytes size.
I have searched SO and relevant sites, but couldn't find any feasible solution but according to this - the SHA256 hash converted to hex produces:
32 separate hexadecimal numbers (or 32 bytes)
How can I generate a SHA256 key of 32 bytes in order to avoid this error using node.js? Is there something I am doing wrong in generating the SHA256 hash?