Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.6K Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to get a Hash[] of a specific octet sequence

E.g., Hash["x","SHA256"] and Hash[ByteArray[{120}], "SHA256"] give different hashes, and neither agrees with the SHA256 hash of the single octet 78 (hex). Is there a way to get the hash of a specific octet sequence?

POSTED BY: Philip Flanner
2 Replies

Assuming your byte sequence is specified as a list of hexadecimal strings, you can do this:

In[70]:= IntegerString[Hash[ExportString[FromDigits[#, 16] & /@ {"78"}, "Byte"], "SHA256"], 16, 64]

Out[70]= "2d711642b726b04401627ca9fbac32f5c8530fb1903cc4db02258717921a4881"

Hash[ByteArray[{120}], "SHA256"] doesn't match Hash["x", "SHA256"] because the former hashes the Wolfram Language expression ByteArray[{120}], not the sequence of bytes itself. To use a sequence of decimal bytes, do this:

IntegerString[
 Hash[ExportString[{120}, "Byte"], 
  "SHA256"], 16, 64]

Both of the above match IntegerString[Hash["x", "SHA256"], 16, 64].

POSTED BY: Jesse Friedman
In[1]:= IntegerString[Hash["x", "SHA256"], 16, 64]

Out[1]= "2d711642b726b04401627ca9fbac32f5c8530fb1903cc4db02258717921a4881"
POSTED BY: Ilian Gachevski
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard