Skip to content

Commit 7e876fb

Browse files
committed
0.1.0
1 parent 230d291 commit 7e876fb

File tree

5 files changed

+108
-13
lines changed

5 files changed

+108
-13
lines changed

readme.md

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,99 @@
11
Blake2Fast
2-
===========
2+
==========
33

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.
55

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.

src/Blake2Fast/Blake2Fast.csproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@
99
<DebugType>pdbonly</DebugType>
1010
<DebugSymbols>true</DebugSymbols>
1111
<LangVersion>latest</LangVersion>
12-
<VersionPrefix>0.1.0-rc1</VersionPrefix>
12+
<VersionPrefix>0.1.0</VersionPrefix>
1313
<Authors>Clinton Ingram</Authors>
1414
<Product>Blake2Fast</Product>
1515
<Description>Optimized implementations of the Blake2b and Blake2s hashing algorithms. Uses SSE2-SSE4.1 intrinsics support on .NET Core 2.1</Description>
16-
<PackageTags>Blake2 Hash Blake2b Blake2s SSE SIMD</PackageTags>
1716
<Copyright>Copyright © 2018 Clinton Ingram</Copyright>
1817
<RepositoryType>git</RepositoryType>
1918
<RepositoryUrl>https://github.com/saucecontrol/Blake2Fast</RepositoryUrl>
20-
<PackageReleaseNotes>See https://github.com/saucecontrol/Blake2Fast/releases</PackageReleaseNotes>
2119
<PackageIconUrl>https://photosauce.net/icon64x64.png</PackageIconUrl>
2220
<PackageProjectUrl>https://github.com/saucecontrol/Blake2Fast</PackageProjectUrl>
2321
<PackageLicenseUrl>https://github.com/saucecontrol/Blake2Fast/license</PackageLicenseUrl>
22+
<PackageReleaseNotes>See https://github.com/saucecontrol/Blake2Fast/releases</PackageReleaseNotes>
23+
<PackageTags>Blake2 Hash Blake2b Blake2s SSE SIMD HashAlgorithm HMAC</PackageTags>
2424
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
25+
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
2526
</PropertyGroup>
2627

27-
<PropertyGroup Condition="'$(TargetFramework)'=='netcoreapp2.1' Or '$(TargetFramework)'=='netcoreapp2.1'">
28+
<PropertyGroup Condition="'$(TargetFramework)'=='netcoreapp2.0' Or '$(TargetFramework)'=='netcoreapp2.1'">
2829
<DefineConstants>$(DefineConstants);IMPLICIT_BYTESPAN</DefineConstants>
2930
</PropertyGroup>
3031

@@ -42,6 +43,7 @@
4243

4344
<ItemGroup>
4445
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.0" />
46+
<PackageReference Include="GitLink" Version="3.1.0" PrivateAssets="all" />
4547
</ItemGroup>
4648

4749
<ItemGroup>

src/Blake2Fast/Blake2b.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public static IBlake2Incremental CreateIncrementalHasher(int digestLength, ByteS
7070
/// <inheritdoc cref="ComputeHash(int, ByteSpan, ByteSpan)"/>
7171
public static byte[] ComputeHash(int digestLength, byte[] key, byte[] input) => ComputeHash(digestLength, key.AsByteSpan(), input.AsByteSpan());
7272

73-
/// <inheritdoc cref="ComputeHash(int, ByteSpan, ByteSpan)"/>
73+
/// <inheritdoc cref="CreateIncrementalHasher(int, ByteSpan)" />
7474
public static IBlake2Incremental CreateIncrementalHasher(byte[] key) => CreateIncrementalHasher(key.AsByteSpan());
7575

76-
/// <inheritdoc cref="ComputeHash(int, ByteSpan, ByteSpan)"/>
76+
/// <inheritdoc cref="CreateIncrementalHasher(int, ByteSpan)" />
7777
public static IBlake2Incremental CreateIncrementalHasher(int digestLength, byte[] key) => CreateIncrementalHasher(digestLength, key.AsByteSpan());
7878
#endif
7979

src/Blake2Fast/Blake2s.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public static IBlake2Incremental CreateIncrementalHasher(int digestLength, ByteS
7070
/// <inheritdoc cref="ComputeHash(int, ByteSpan, ByteSpan)"/>
7171
public static byte[] ComputeHash(int digestLength, byte[] key, byte[] input) => ComputeHash(digestLength, key.AsByteSpan(), input.AsByteSpan());
7272

73-
/// <inheritdoc cref="ComputeHash(int, ByteSpan, ByteSpan)"/>
73+
/// <inheritdoc cref="CreateIncrementalHasher(int, ByteSpan)" />
7474
public static IBlake2Incremental CreateIncrementalHasher(byte[] key) => CreateIncrementalHasher(key.AsByteSpan());
7575

76-
/// <inheritdoc cref="ComputeHash(int, ByteSpan, ByteSpan)"/>
76+
/// <inheritdoc cref="CreateIncrementalHasher(int, ByteSpan)" />
7777
public static IBlake2Incremental CreateIncrementalHasher(int digestLength, byte[] key) => CreateIncrementalHasher(digestLength, key.AsByteSpan());
7878
#endif
7979

src/Blake2Fast/_Blake2Api.ttinclude

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ namespace SauceControl.Blake2Fast
7070
/// <inheritdoc cref="ComputeHash(int, ByteSpan, ByteSpan)"/>
7171
public static byte[] ComputeHash(int digestLength, byte[] key, byte[] input) => ComputeHash(digestLength, key.AsByteSpan(), input.AsByteSpan());
7272

73-
/// <inheritdoc cref="ComputeHash(int, ByteSpan, ByteSpan)"/>
73+
/// <inheritdoc cref="CreateIncrementalHasher(int, ByteSpan)" />
7474
public static IBlake2Incremental CreateIncrementalHasher(byte[] key) => CreateIncrementalHasher(key.AsByteSpan());
7575

76-
/// <inheritdoc cref="ComputeHash(int, ByteSpan, ByteSpan)"/>
76+
/// <inheritdoc cref="CreateIncrementalHasher(int, ByteSpan)" />
7777
public static IBlake2Incremental CreateIncrementalHasher(int digestLength, byte[] key) => CreateIncrementalHasher(digestLength, key.AsByteSpan());
7878
#endif
7979

0 commit comments

Comments
 (0)