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 / ...).
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).
Blake256
Blake512
There are two different ways you can include BlakeSharp:
BlakeSharp
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.
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.
HashAlgorithm
SHA1Managed
SHA256Managed
Some usage examples:
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:
Encoding.Default
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).
pbHash
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.
ComputeHash
byte[]
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.
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.
Stream
TransformBlock
TransformFinalBlock
Hash
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:
Computes and prints the BLAKE-256 and BLAKE-512 hashes of the contents of the files MyFile.txt and MyFile2.txt.
Outputs the following:
Text 'The quick brown fox jumps over the lazy dog' (encoding Windows-1252) BLAKE-256: 7576698EE9CAD30173080678E5965916ADBB11CB5245D386BF1FFDA1CB26C9D7 BLAKE-512: 1F7E26F63B6AD25A0896FD978FD050A1766391D2FD0471A77AFB975E5034B7AD- 2D9CCF8DFB47ABBBE656E1B82FBC634BA42CE186E8DC5E1CE09A885D41F43451
When 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.
mono
That's it! Happy hashing!