|
1 | 1 | Blake2Fast |
2 | | -=========== |
| 2 | +========== |
3 | 3 |
|
4 | | -Optimized implementations of Blake2b and Blake2s, using SSE intrinsics on .NET Core 2.1 |
| 4 | +These [RFC 7693](https://tools.ietf.org/html/rfc7693)-compliant Blake2 implementations have been tuned for high speed and low memory usage. The .NET Core 2.1 build supports the new X86 SIMD Intrinsics for even greater speed and `Span<T>` for even lower memory usage. |
5 | 5 |
|
6 | | -More to come... |
| 6 | +Sample benchmark results comparing with built-in .NET algorithms, 10MiB input, .NET Core 2.1 x64 and x86 runtimes: |
| 7 | + |
| 8 | +Method | Platform | Mean | Error | StdDev | Allocated | |
| 9 | +------------ |--------- |----------:|----------:|----------:|----------:| |
| 10 | +Blake2bFast | X64 | 12.13 ms | 0.0870 ms | 0.0771 ms | 0 B | |
| 11 | +Blake2sFast | X64 | 16.27 ms | 0.1362 ms | 0.1274 ms | 0 B | |
| 12 | +MD5 | X64 | 21.22 ms | 0.1190 ms | 0.1113 ms | 0 B | |
| 13 | +SHA256 | X64 | 46.16 ms | 0.2564 ms | 0.2398 ms | 0 B | |
| 14 | +SHA512 | X64 | 27.89 ms | 0.0982 ms | 0.0871 ms | 304 B | |
| 15 | +| | | | | | |
| 16 | +Blake2bFast | X86 | 16.56 ms | 0.0879 ms | 0.0779 ms | 0 B | |
| 17 | +Blake2sFast | X86 | 16.36 ms | 0.1103 ms | 0.1032 ms | 0 B | |
| 18 | +MD5 | X86 | 20.06 ms | 0.0996 ms | 0.0931 ms | 0 B | |
| 19 | +SHA256 | X86 | 52.47 ms | 0.3252 ms | 0.3042 ms | 0 B | |
| 20 | +SHA512 | X86 | 44.07 ms | 0.1643 ms | 0.1372 ms | 0 B | |
| 21 | + |
| 22 | +You can find more detailed comparison between Blake2Fast and other .NET Blake2 implementations starting [here](https://photosauce.net/blog/post/fast-hashing-with-blake2-part-1-nuget-is-a-minefield) |
| 23 | + |
| 24 | +Installation |
| 25 | +------------ |
| 26 | + |
| 27 | +Blake2Fast is available on [NuGet](https://www.nuget.org/packages/SauceControl.Blake2Fast/) |
| 28 | + |
| 29 | +``` |
| 30 | +PM> Install-Package SauceControl.Blake2Fast |
| 31 | +``` |
| 32 | + |
| 33 | +Usage |
| 34 | +----- |
| 35 | + |
| 36 | +### All-at-Once Hashing |
| 37 | + |
| 38 | +The simplest and lightest-weight way to calculate a hash is the all-at-once `ComputeHash` method. |
| 39 | +```C# |
| 40 | +Blake2b.ComputeHash(data); |
| 41 | +``` |
| 42 | + |
| 43 | +Blake2 supports variable digest lengths from 1 to 32 bytes for `Blake2s` or 1 to 64 bytes for `Blake2b`. |
| 44 | +```C# |
| 45 | +Blake2b.ComputeHash(48, data); |
| 46 | +``` |
| 47 | + |
| 48 | +Blake2 also natively supports keyed hashing. |
| 49 | +```C# |
| 50 | +Blake2b.ComputeHash(key, data); |
| 51 | +``` |
| 52 | + |
| 53 | +### Incremental Hashing |
| 54 | + |
| 55 | +Blake2 hashes can be incrementally updated if you do not have the data available all at once. |
| 56 | + |
| 57 | +```C# |
| 58 | +async Task<byte[]> CalculateHashAsync(Stream data) |
| 59 | +{ |
| 60 | + var incHash = Blake2b.CreateIncrementalHasher(); |
| 61 | + var buffer = new byte[4096]; |
| 62 | + int bytesRead; |
| 63 | + |
| 64 | + while ((bytesRead = await data.ReadAsync(buffer, 0, buffer.Length)) > 0) |
| 65 | + { |
| 66 | + // Use Span<T> for .NET Core 2.1 or ArraySegment<T> for others |
| 67 | + incHash.Update(new ArraySegment<byte>(buffer, 0, bytesRead)); |
| 68 | + } |
| 69 | + |
| 70 | + return incHash.Finish(); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### System.Security.Cryptography Interop |
| 75 | + |
| 76 | +For interoperating with code that uses `System.Security.Cryptography` primitives, Blake2Fast can create a `HashAlgorithm` wrapper. The wrapper inherits from `HMAC` in case keyed hashing is required. |
| 77 | + |
| 78 | +`HashAlgorithm` is less efficient than the above methods, so use it only when necessary. |
| 79 | + |
| 80 | +```C# |
| 81 | +byte[] WriteDataAndCalculateHash(byte[] data) |
| 82 | +{ |
| 83 | + using (var hashAlg = Blake2b.CreateHashAlgorithm()) |
| 84 | + using (var fileStream = new FileStream(@"c:\data\output.bin", FileMode.Create)) |
| 85 | + using (var cryptoStream = new CryptoStream(fileStream, hashAlg, CryptoStreamMode.Write)) |
| 86 | + { |
| 87 | + cryptoStream.Write(data, 0, data.Length); |
| 88 | + cryptoStream.FlushFinalBlock(); |
| 89 | + return hashAlg.Hash; |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +SIMD Intrinsics Warning |
| 95 | +----------------------- |
| 96 | + |
| 97 | +The X86 SIMD Intrinsics used in the .NET Core 2.1 build are not officially supported by Microsoft. Although the specific SSE Intrinsics used by Blake2Fast have been well-tested, the JIT support for the X86 Intrinsics in general is experimental in .NET Core 2.1. Please test with your specific hardware and report any issues here or in the [CoreCLR](https://github.com/dotnet/coreclr/) repo as appropriate. |
| 98 | + |
| 99 | +If you are uncomfortable using unsupported functionality, you can make a custom build of Blake2Fast by removing the `USE_INTRINSICS` define constant in the [project file](src/Blake2Fast/Blake2Fast.csproj). This applies only to .NET Core 2.1; the older build targets use only the scalar code. |
0 commit comments