bitwiseshiftleft / sjcl

Stanford Javascript Crypto Library
http://bitwiseshiftleft.github.com/sjcl/
Other
7.18k stars 987 forks source link

Hash Value #366

Closed heatedrican closed 6 years ago

heatedrican commented 6 years ago

I am using the SHA256 module. An example hash value I am receiving looks like something like this:

262956376,621425568,-662978560,-55799626,202421628,872470707,-1721443650,853936394

How can I get back a simple SHA256 hash represented in hexadecimal consisting of 64 hexadecimal characters instead? I apologize in advance if this is a stupid question. Thank you.

robyoder commented 6 years ago

SJCL works with bit arrays. In order to get a hex string you'll need to use the hex codec to convert from bits to a string. For example:

sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash("test"));
// => "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
heatedrican commented 6 years ago

Thanks! I missed that namespace. The update method of the sjcl.hash.sha256.prototype object uses the utf8String codec to convert the data to bits. Do you know why we use utf8String here?

robyoder commented 6 years ago

I'm not sure what you mean exactly. The input to the hash function is any string, so sjcl.codec.utf8String.toBits converts that string to a bit array that the hash function can work with.

heatedrican commented 6 years ago

Gotcha! I think I was reading it wrong initially. Thanks again for the help!

Nilos commented 6 years ago

Thanks @robyoder