You can try it yourself by putting in a string to hash below:
The specification of the Blake 512 algorithm is available online here: blake.pdf. This document also includes a reference C implementation, together with some test values against which to verify an implementation's correctness. If you're interested in what the algorithm actually does and how it works; that's the place to start reading! The only valid test case from the spec you can use with this implementation is the sample "2-block message" which encodes a sequence of 1152 zeros, using the standard salt (which is a sequence of 256 zeros). To run this test in your browser, open the developer console and enter:
Hopefully, the output should be:
313717D608E9CF75 8DCB1EB0F0C3CF9F C150B2D500FB33F5 1C52AFC99D358A2F 1374B8A38BBA7974 E7F6EF79CAB16F22 CE1E649D6E01AD95 89C213045D545DDE.
Handling the Input
Crucially, the binary operators such as and (
&), exclusive-or (
^) and shift (
2^32 which is written like so:
Although this looks bizarre at first, it actually quite sensible as you can't do
1 << 32 since you just get 1 back, nor can you do:
(1 << 16) << 16 as you get 0.
So, without 64 bit integers, we're forced to hack them in some way, the standard way of doing this is to use an 2 element array of 32 bit integers to represent a single 64 bit integer, then implementing all the required operations yourself. e.g. you could represent the number: 2^64-1 (a sequence of 64 one's) as:
There are several implementations that you can use to do this, the most notable being Google's closure library's Long. However, since that code is part of the closure library, which I don't want a dependancy on, I wrote my own 64 bit number class called
Word (i.e. as it represents a single 64 bit "word" in the algorithm). This version also supports the required "bit rotation" operation and is more light-weight, in that all operations alter the state of the
Word on which it is called, rather than returning a new instance each time.
Word class is private within the algorithm's code, it might be useful for other applications. The code for it as follows:
The rest of the code is pretty much a straight implementation of the specification and is available for free use, if there's something you think's odd about it please let me know!
Using the Algorithm
The code exposes only a single global variable - the function
blake512 which expects up to 3 parameters, and has the following definition:
The parameters are expected to be as follows:
||(string, required) the message to hash, encoded as previously described.||
||(string, optional) the salt to use when hashing. If this is defined must be a string of length 16 which is treated as a 256 bit binary sequence, encoded in the same manner as the message. If this is not defined, the default salt, consisting of a sequence of 256 zero's is used.|
||(boolean, optional) whether the output should be a string encoded in the same manner as the message (the default behavior) or a hexadecimal sequence.|
The in the case that
true, the following (probably non-optimal!) code is used to convert the encoded string to a hex sequence:
I don't think there'll be any cross browser issues or other problems but if you spot something please let me know and I'll try to fix the problem (time permitting)!