Project Description

Here you can find implementations of many hashing algorithms: for hashtables, cryptographic purpose and checksums. There are interfaces for hashing files, streams, common types of data and to generating HMAC's. Written in C# 4.0, VS2010.

Supported algorithms:

  • non-cryptographics 32-bits hash algorithms: Murmur2, Jenkins3, SuperFast, AP, Bernstein, Bernstein1, BKDR, DEK, DJB, ELF, FNV, FNV1a, JS, OneAtTime, PJW, Rotating, RS, SDBM, ShiftAndXor
  • non-cryptographic 64-bits algorithms: Murmur2, FNV, FNV1a
  • checksum algorithms: Adler32, CRC32 (diffrent polynomials supported), CRC64 (diffrent polynomials supported)
  • all algorithms build in System.Security.Cryptography: MD5, RIPEMD160, SHA1, SHA256, SHA384, SHA512
  • cryptographic algorithms: GOST, HAS160, Haval, MD2, MD4, MD5, RIPEMD128, RIPEMD160, RIPEMD256, RIPEMD320, SHA1, SHA224, SHA256, SHA384, SHA512, Snefru, Tiger, Tiger2, Whirlpool
  • All SHA-3 candidates: Blake, BlueMidnightWish, CubeHash, Echo, Fugue, Groestl, Hamsi, JH, Keccak, Luffa, Shabal, SHAvite3, SIMD, Skein (as reference I had used optimized x64 version without assembler and SIMD support).
  • HMAC for any from above.

Example

using System;
using System.Diagnostics;
using System.IO;
using HashLib;
using System.Linq;

namespace Examples
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Prepare temp file.
            string file_name = Path.GetTempFileName();
            using (var fs = new FileStream(file_name, FileMode.Open))
            {
                byte[] v = new byte[256];
                new Random().NextBytes(v);
                fs.Write(v, 0, v.Length);
            }

            // Prepare stream.
            MemoryStream ms = new MemoryStream(new byte[] { 2, 3, 4, 5, 6, 7 });

            // Choose algorithm. Explore HashFactory for more algorithms.
            IHash hash = HashFactory.Crypto.CreateSHA256();

            // Hash data immediate.
            HashResult r = hash.ComputeString("test", System.Text.Encoding.ASCII);

            // Hash data.
            hash.Initialize(); // Not mandatory after Compute and TransformFinal
            hash.TransformULong(6);
            hash.TransformString("test");
            r = hash.TransformFinal();

            // Calculate 32-bits hash.
            hash = HashFactory.Checksum.CreateCRC32a();
            uint crc32 = hash.ComputeString("test").GetUInt();

            // For CRCs you may specify parameters.
            hash = HashFactory.Checksum.CreateCRC32(
                HashLib.Checksum.CRC32Polynomials.IEEE_802_3, 
                uint.MaxValue, uint.MaxValue);
            hash = HashFactory.Checksum.CreateCRC32(
                0xF0F0F0F0, uint.MaxValue, uint.MaxValue);

            // Most hashes can be created in two ways.
            hash = HashFactory.Crypto.CreateHaval(HashRounds.Rounds3, 
                HashSize.HashSize256);
            hash = HashFactory.Crypto.CreateHaval_3_256();

            // Calculate 64-bits hash.
            hash = HashFactory.Hash64.CreateMurmur2();
            ulong crc64 = hash.ComputeString("test").GetULong();

            // Get some information about algorithm. BlockSize has 
            // only informative meaning.
            System.Console.WriteLine("{0}, {1}, {2}", hash.BlockSize, hash.HashSize, 
                hash.Name);

            // Here you can find algorithms grouped by its properties.
            foreach (var h in Hashes.CryptoAll)
                System.Console.WriteLine(((IHash)Activator.CreateInstance(h)).Name);
            foreach (var h in Hashes.CryptoNotBuildIn)
                System.Console.WriteLine(((IHash)Activator.CreateInstance(h)).Name);
            foreach (var h in Hashes.CryptoBuildIn)
                System.Console.WriteLine(((IHash)Activator.CreateInstance(h)).Name);
            foreach (var h in Hashes.Checksums)
                System.Console.WriteLine(((IHash)Activator.CreateInstance(h)).Name);
            // ... And more
            
            // Hash stream.
            r = hash.ComputeStream(ms);
            ms.Position = 2;
            r = hash.ComputeStream(ms); // Compute all bytes starting from 2
            ms.Position = 3;
            r = hash.ComputeStream(ms, 2); // Compute 2 bytes starting from 3

            hash.TransformInt(111);
            ms.Position = 0;
            hash.TransformStream(ms);
            r = hash.TransformFinal();

            // Hash file
            r = hash.ComputeFile(file_name);
            // Compute all bytes starting from 10
            r = hash.ComputeFile(file_name, 10);
            // Compute 10 bytes starting from 12.
            r = hash.ComputeFile(file_name, 12, 10); 

            hash.TransformInt(111);
            hash.TransformFile(file_name);
            r = hash.TransformFinal();

            // Calculate HMAC.
            IHMAC hmac = HashFactory.HMAC.CreateHMAC(HashFactory.Crypto.CreateSHA256());
            hmac.Key = Converters.ConvertStringToBytes("secret");
            r = hmac.ComputeString("test");

            // Get System.Security.Cryptography.HashAlgorithm wrapper for 
            // algorithms from this library.
            System.Security.Cryptography.HashAlgorithm hash2 = 
                HashFactory.Wrappers.HashToHashAlgorithm(hash);

            // And back.
            hash = HashFactory.Wrappers.HashAlgorithmToHash(hash2); 

            // Some algorithms has fast specialized methods for calculating hashes for 
            // all data types. There are designed for calculating good-behave hash
            // codes for hash-tables.
            hash = HashFactory.Hash32.CreateMurmur2();
            Debug.Assert(hash is IFastHashCodes);

            // Some algorithms can calculated hashes only when they had all needed 
            // data, they accumulated data to the very end.
            hash = HashFactory.Hash32.CreateMurmur2();
            Debug.Assert(hash is INonBlockHash);

            // Use build-in cryptography hash algorithms.
            hash = HashFactory.Crypto.BuildIn.CreateSHA256Cng();

            // Delete temp file.
            new FileInfo(file_name).Delete();
        }
    }
}

HashLibQualityTest

One of project in solution. Here you can test speed, quality and calcualte hashes.

1.png
2.png
3.png

SHA3CLRTest and SHA3MFCTest

Temporary projects used to convert SHA3 candidates from C++ to C++ CLI, and finally to C#. In 'Hash.ods' you may find measured performances of SHA3 candidates.

4.png

5.png

6.png



My Blog: http://anutom.blogspot.com/

Last edited Feb 3, 2012 at 3:50 PM by tomanu, version 49