Contents
BLAKE and BlakeSharp
BlakeSharp is my implementation of the BLAKE algorithm written in C#. It is public domain, i.e., you may freely include/use it in your
projects (also commercial ones). If you're using it, it would be nice if you'd mention me somewhere in the documentation of your program, but it's not required.
BLAKE is a cryptographically secure one-way hash function. It was designed by
Jean-Philippe Aumasson, Luca Henzen, Willi Meier, and Raphael C.-W. Phan. BLAKE is one of the five finalists in NIST's SHA-3 competition.
For more information on BLAKE, see the official BLAKE website.
BlakeSharp was derived from the reference C implementation of BLAKE. All code was rewritten in C#, i.e., only managed code is used
now, no native or unsafe code. Thus it's portable and runs fine under both .NET (Windows) and Mono (Linux / Mac OS X / ...).
Including BlakeSharp in Your Project
BlakeSharp consists of two classes that implement the two main instances of BLAKE: the Blake256
class implements the 256-bit version of BLAKE
(BLAKE-256, producing a 256-bit = 32 bytes long hash), and the Blake512
class implements the 512-bit version of BLAKE (BLAKE-512, producing a 512-bit = 64 bytes long hash).
The two classes are independent of each other (i.e., when you only need the 256-bit version, you don't need to include the 512-bit code, and vice versa).
There are two different ways you can include BlakeSharp:
- Include source code. If your project is written in C#, you can simply copy/include the BlakeSharp files in your project.
Depending on whether you want to use the 256-bit and/or the 512-bit version, you need to include the Blake256.cs
and/or Blake512.cs file(s). After including the file(s), the class(es) can be found in the
BlakeSharp
namespace. - Reference the BlakeSharp assembly. If your project is written in another .NET language, you can
reference the BlakeSharp assembly (BlakeSharp.dll file) in your project. This will make the
BlakeSharp
namespace available, in which you'll find the two classes.
Blake256.cs and Blake512.cs can be found in the BlakeSharp folder in the downloadable package.
A compiled BlakeSharp.dll is in Build/BlakeSharp/Release.
Using BlakeSharp
The two classes (Blake256
and Blake512
) are derived from the HashAlgorithm
.NET base class.
Consequently the classes are being used the very same as other hash algorithm classes of the .NET framework, like e.g., SHA1Managed
or SHA256Managed
.
Some usage examples:
- Hashing a string. In order to hash a string, you first need to decide which encoding you want to use. An encoding defines the way string characters are
mapped to byte sequences.
In test vectors, strings are often encoded using ANSI. Strings can be encoded in ANSI by using Encoding.Default
. So the code to hash the ANSI-encoded string
"The quick brown fox jumps over the lazy dog" could look like the following:
string str = "The quick brown fox jumps over the lazy dog";
byte[] pbText = Encoding.Default.GetBytes(str);
Blake512 blake512 = new Blake512();
byte[] pbHash = blake512.ComputeHash(pbText);
The pbHash
byte array now contains the BLAKE-512 hash (64 bytes).
Here we use the ComputeHash
method overload that accepts a byte[]
. This of course requires that the whole data
to be hashed is in memory, which might not always be possible (e.g., when hashing a multi-GB file). In such a case, the approaches below are more practical.
The ComputeHash
method automatically reinitializes the object, i.e., you can call it again immediately afterwards if you want;
no new instance of the class needs to be allocated.
Hashing a file. Computing the hash of the contents of a file is easy, too:
FileStream fsIn = new FileStream(strFilePath,
FileMode.Open, FileAccess.Read, FileShare.Read);
Blake512 blake512 = new Blake512();
byte[] pbHash = blake512.ComputeHash(fsIn);
fsIn.Close();
Here we use the ComputeHash
method overload that accepts a Stream
. This method consecutively reads data from the stream
in blocks and hashes these blocks. As only one block needs to be kept in memory all the time, this method requires very little memory and is suitable for hashing multi-GB files.
The ComputeHash
method automatically reinitializes the object, i.e., you can call it again immediately afterwards if you want;
no new instance of the class needs to be allocated.
Hashing on your own. If you want to feed the data yourself into the hash algorithm (in blocks), you can do so
by using the TransformBlock
and TransformFinalBlock
methods. Afterwards the hash can be queried using the Hash
property.
For details, please see the HashAlgorithm
documentation on MSDN.
The Demo Application
The downloadable package contains a demo application called BlakeSharpDemo, which can hash strings and files.
A compiled executable BlakeSharpDemo.exe can be found in Build/BlakeSharpDemo/Release.
BlakeSharpDemo.exe can be invoked with the following command line parameters:
-f
All parameters following this argument are interpreted as file paths and the application computes BLAKE-256 and BLAKE-512 hashes of them.
-s
All parameters following this argument are interpreted as ANSI strings and the application computes BLAKE-256 and BLAKE-512 hashes of them.
-t
Performs a very minimalistic self-test.
-tf
All parameters following this argument are interpreted as file paths to test vector files. The application loads the test vectors and verifies that correct hashes are computed.
-b256
Performs a benchmark of Blake256
. The output number indicates how many megabytes of data can be hashed per second on the current PC.
-b512
Performs a benchmark of Blake512
. The output number indicates how many megabytes of data can be hashed per second on the current PC.
Examples:
- BlakeSharpDemo.exe -f MyFile.txt MyFile2.txt
Computes and prints the BLAKE-256 and BLAKE-512 hashes of the contents of the files MyFile.txt and MyFile2.txt.
- BlakeSharpDemo.exe -s "The quick brown fox jumps over the lazy dog"
Outputs the following:
Text 'The quick brown fox jumps over the lazy dog' (encoding Windows-1252)
BLAKE-256:
7576698EE9CAD30173080678E5965916ADBB11CB5245D386BF1FFDA1CB26C9D7
BLAKE-512:
1F7E26F63B6AD25A0896FD978FD050A1766391D2FD0471A77AFB975E5034B7AD-
2D9CCF8DFB47ABBBE656E1B82FBC634BA42CE186E8DC5E1CE09A885D41F43451
BlakeSharpDemo.exe -tf ShortMsgKAT_512.txt LongMsgKAT_512.txtWhen you copy the files ShortMsgKAT_512.txt and LongMsgKAT_512.txt (from the KAT_MCT folder in the official BLAKE specification package)
into the BlakeSharpDemo application directory and run the command above, BlakeSharpDemo loads these two test vector files and verifies all the hashes. The output should be:
Summary: 255 test vectors validated successfully, 0 errors.
Summary: 65 test vectors validated successfully, 0 errors.
BlakeSharpDemo supports parsing and checking all test vector files except the MonteCarlo_*.txt files
(support for parsing these might be added in a later version of BlakeSharpDemo).
In order to run BlakeSharpDemo under Mono (Linux / Mac OS X / ...), prepend "mono
" to the command lines above.
That's it! Happy hashing!
Version History
- Version 1.0 - 2011-11-20
- Initial release (implementing BLAKE v1.4).