SAIB.Blake2Fast
3.0.1
dotnet add package SAIB.Blake2Fast --version 3.0.1
NuGet\Install-Package SAIB.Blake2Fast -Version 3.0.1
<PackageReference Include="SAIB.Blake2Fast" Version="3.0.1" />
<PackageVersion Include="SAIB.Blake2Fast" Version="3.0.1" />
<PackageReference Include="SAIB.Blake2Fast" />
paket add SAIB.Blake2Fast --version 3.0.1
#r "nuget: SAIB.Blake2Fast, 3.0.1"
#:package SAIB.Blake2Fast@3.0.1
#addin nuget:?package=SAIB.Blake2Fast&version=3.0.1
#tool nuget:?package=SAIB.Blake2Fast&version=3.0.1
Blake2Fast
These RFC 7693-compliant BLAKE2 implementations have been tuned for high speed and low memory usage. Span<byte>
is used throughout for lower memory overhead compared to byte[]
based APIs.
On modern .NET, Blake2Fast includes SIMD-accelerated (SSE2 - AVX-512) implementations of both BLAKE2b and BLAKE2s.
Usage
One-Shot Hashing
The simplest way to calculate a hash is the one-shot ComputeHash
method.
var hash = Blake2b.ComputeHash(data);
BLAKE2 supports variable digest lengths from 1 to 32 bytes for BLAKE2s or 1 to 64 bytes for BLAKE2b.
var hash = Blake2b.ComputeHash(42, data);
BLAKE2 also natively supports keyed hashing.
var hash = Blake2b.ComputeHash(key, data);
Incremental Hashing
BLAKE2 hashes can be incrementally updated if you do not have the data available all at once.
async Task<byte[]> ComputeHashAsync(Stream data)
{
var hasher = Blake2b.CreateIncrementalHasher();
var buffer = ArrayPool<byte>.Shared.Rent(4096);
int bytesRead;
while ((bytesRead = await data.ReadAsync(buffer, 0, buffer.Length)) > 0)
hasher.Update(buffer.AsSpan(0, bytesRead));
ArrayPool<byte>.Shared.Return(buffer);
return hasher.Finish();
}
For convenience, the generic Update<T>()
method accepts any value type that does not contain reference fields, plus arrays and Spans of compatible types.
byte[] ComputeCompositeHash()
{
var hasher = Blake2b.CreateIncrementalHasher();
hasher.Update(42);
hasher.Update(Math.Pi);
hasher.Update("I love deadlines. I like the whooshing sound they make as they fly by.".AsSpan());
return hasher.Finish();
}
Be aware that the value passed to Update
is added to the hash state in its current memory layout, which may differ based on platform (endianness) or struct layout. Use care when calling Update
with types other than byte
if the computed hashes are to be used across application or machine boundaries.
For example, if you are adding a string to the hash state, you may hash the characters in memory layout as shown above, or you may use Encoding.GetBytes
to ensure the string bytes are handled consistently across platforms.
Allocation-Free Hashing
The output hash digest can be written to an existing buffer to avoid allocating a new array each time.
Span<byte> buffer = stackalloc byte[Blake2b.DefaultDigestLength];
Blake2b.ComputeAndWriteHash(data, buffer);
This is especially useful when performing an iterative hash, as might be used in a key derivation function.
byte[] DeriveBytes(string password, ReadOnlySpan<byte> salt)
{
// Create key from password, then hash the salt using the key
var pwkey = Blake2b.ComputeHash(Encoding.UTF8.GetBytes(password));
var hbuff = Blake2b.ComputeHash(pwkey, salt);
// Hash the hash lots of times, re-using the same buffer
for (int i = 0; i < 999_999; i++)
Blake2b.ComputeAndWriteHash(pwkey, hbuff, hbuff);
return hbuff;
}
System.Security.Cryptography Interop
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.
HashAlgorithm
is less efficient than the above methods, so use it only when necessary for compatibility.
byte[] WriteDataAndCalculateHash(byte[] data, string outFile)
{
using (var hashAlg = Blake2b.CreateHashAlgorithm())
using (var fileStream = new FileStream(outFile, FileMode.Create))
using (var cryptoStream = new CryptoStream(fileStream, hashAlg, CryptoStreamMode.Write))
{
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
return hashAlg.Hash;
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net46 is compatible. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6
- System.Buffers (>= 4.5.1)
- System.Memory (>= 4.5.5)
- System.Runtime.CompilerServices.Unsafe (>= 4.5.3)
-
.NETStandard 2.0
- System.Buffers (>= 4.5.1)
- System.Memory (>= 4.5.5)
- System.Runtime.CompilerServices.Unsafe (>= 4.5.3)
-
.NETStandard 2.1
- System.Runtime.CompilerServices.Unsafe (>= 6.0.0)
-
net6.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on SAIB.Blake2Fast:
Package | Downloads |
---|---|
Chrysalis
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
3.0.1 | 618 | 5/16/2025 |
See https://github.com/saucecontrol/Blake2Fast/releases for release-specific notes.