Click here to Skip to main content
15,861,125 members
Articles / Security / Cryptography

BlakeSharp - A C# Implementation of the BLAKE Hash Algorithm

Rate me:
Please Sign up or sign in to vote.
4.94/5 (32 votes)
20 Nov 2011CPOL5 min read 53.1K   1.5K   70   14
A C# implementation of the BLAKE hash algorithm.

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:

    C#
    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:
  • C#
    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.txt
  • 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.

That's it! Happy hashing!

Version History

  • Version 1.0 - 2011-11-20
    • Initial release (implementing BLAKE v1.4).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Unknown
Dominik started programming in Omikron Basic, a programming language for the good old Atari ST. After this, there was some short period of QBasic programming on the PC, but soon he began learning C++, which is his favorite language up to now.

Today, his programming experience includes C / C++ / [Visual] C++ [MFC], C#/.NET, Java, JavaScript, PHP and HTML and the basics of pure assembler.

He is interested in almost everything that has to do with computing; his special interests are security, cryptography and data compression.

You can find his latest freeware, open source projects and articles on his website: https://www.dominik-reichl.de/.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Alberto M.28-Mar-14 21:30
Alberto M.28-Mar-14 21:30 
GeneralMy vote of 5 Pin
Prasad Khandekar18-Mar-13 22:05
professionalPrasad Khandekar18-Mar-13 22:05 
QuestionHosted from within XCrypt Pin
Vasudevan Deepak Kumar24-Oct-12 10:20
Vasudevan Deepak Kumar24-Oct-12 10:20 
GeneralMy vote of 5 Pin
Abinash Bishoyi24-Sep-12 6:27
Abinash Bishoyi24-Sep-12 6:27 
GeneralMy vote of 5 Pin
s.hervy9-Apr-12 5:49
s.hervy9-Apr-12 5:49 
GeneralMy vote of 5 Pin
Member 43208446-Jan-12 7:38
Member 43208446-Jan-12 7:38 
Questionmy alternative project with all sha3 candidates Pin
Tomek Anuszkiewicz28-Nov-11 1:59
Tomek Anuszkiewicz28-Nov-11 1:59 
Generaleven on silverlight.. thanks. Pin
Leonardo Paneque23-Nov-11 9:05
Leonardo Paneque23-Nov-11 9:05 
QuestionCan i Use this in commercial application Pin
albert arul prakash21-Nov-11 0:08
albert arul prakash21-Nov-11 0:08 
AnswerRe: Can i Use this in commercial application Pin
Dominik Reichl21-Nov-11 8:18
Dominik Reichl21-Nov-11 8:18 
GeneralMy vote of 5 Pin
Pooya Paridel20-Nov-11 22:25
Pooya Paridel20-Nov-11 22:25 
GeneralMy vote of 5 Pin
HaBiX20-Nov-11 21:43
HaBiX20-Nov-11 21:43 
GeneralMy vote of 5 Pin
Kanasz Robert20-Nov-11 10:40
professionalKanasz Robert20-Nov-11 10:40 
GeneralMy vote of 5 Pin
Filip D'haene20-Nov-11 8:43
Filip D'haene20-Nov-11 8:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.