Blake2b.NetCore
1.0.2
dotnet add package Blake2b.NetCore --version 1.0.2
NuGet\Install-Package Blake2b.NetCore -Version 1.0.2
<PackageReference Include="Blake2b.NetCore" Version="1.0.2" />
<PackageVersion Include="Blake2b.NetCore" Version="1.0.2" />
<PackageReference Include="Blake2b.NetCore" />
paket add Blake2b.NetCore --version 1.0.2
#r "nuget: Blake2b.NetCore, 1.0.2"
#:package Blake2b.NetCore@1.0.2
#addin nuget:?package=Blake2b.NetCore&version=1.0.2
#tool nuget:?package=Blake2b.NetCore&version=1.0.2
Blake2b.NetCore
Blake2b.NetCore is a .NET implementation of the BLAKE2b cryptographic hash function with support for both:
- Unkeyed hashing (
Blake2b) - Keyed hashing / MAC (
Blake2bMac)
The implementation is optimized for 64-bit platforms and integrates with PinnedMemory to support memory-sensitive use cases.
Hash compression and digest serialization use SIMD-aware fast paths on supported runtimes, with an automatic scalar fallback to preserve correctness on non-SIMD hardware.
This implementation does not support BLAKE2 tree hashing mode.
Table of contents
- Requirements
- Installation
- Quick start
- API reference
- Performance notes
- Best practices
- Validation and known test vectors
- Development
- Security notes
- License
Requirements
- .NET 8 SDK for building/testing this repository.
- Target runtime/framework for the project: .NET 8.
The repository contains a global.json pinning the SDK family used for development.
Installation
NuGet Package Manager (CLI)
dotnet add package Blake2b.NetCore
Package Manager Console
Install-Package Blake2b.NetCore
NuGet Gallery
Quick start
Hashing with Blake2b
using System;
using System.Text;
using Blake2b.NetCore;
using PinnedMemory;
var message = Encoding.UTF8.GetBytes("hello world");
using var blake2b = new Blake2b(); // default: 512-bit output (64 bytes)
using var output = new PinnedMemory<byte>(new byte[blake2b.GetLength()]);
blake2b.UpdateBlock(message, 0, message.Length);
blake2b.DoFinal(output, 0);
var hashHex = Convert.ToHexString(output.ToArray()).ToLowerInvariant();
Console.WriteLine(hashHex);
Message authentication with Blake2bMac
using System;
using System.Text;
using Blake2b.NetCore;
using PinnedMemory;
var message = Encoding.UTF8.GetBytes("payload");
using var key = new PinnedMemory<byte>(Encoding.UTF8.GetBytes("a-32-byte-demo-key-change-me-1234"), false);
using var mac = new Blake2bMac(key); // default: 64-byte output
using var output = new PinnedMemory<byte>(new byte[mac.GetLength()]);
mac.UpdateBlock(message, 0, message.Length);
mac.DoFinal(output, 0);
var macHex = Convert.ToHexString(output.ToArray()).ToLowerInvariant();
Console.WriteLine(macHex);
// Optional explicit zeroization of sensitive inputs when done:
mac.ClearKey();
API reference
Blake2b
Constructor
Blake2b(int digestSize = 512)
digestSizeis in bits.- Valid range: 8..512, in multiples of 8.
- Examples: 256 ⇒ 32-byte digest, 512 ⇒ 64-byte digest.
Core methods
void Update(byte b)
void UpdateBlock(byte[] message, int offset, int len)
void UpdateBlock(PinnedMemory<byte> message, int offset, int len)
void DoFinal(PinnedMemory<byte> output, int outOffset)
void Reset()
int GetLength()
int GetBlockSize()
void Dispose()
Behavior notes
DoFinal(...)finalizes and then resets internal state so the instance can be reused.GetLength()returns output length in bytes.GetBlockSize()returns 128 bytes for BLAKE2b.
Blake2bMac
Constructors
Blake2bMac(PinnedMemory<byte> key)
Blake2bMac(PinnedMemory<byte> key, byte[] salt, int digestLength = 64)
keylength: 0..64 bytes.digestLengthis in bytes, valid range 1..64.saltmust be exactly 16 bytes if provided.
Core methods
void Update(byte b)
void UpdateBlock(byte[] message, int offset, int len)
void UpdateBlock(PinnedMemory<byte> message, int offset, int len)
void DoFinal(PinnedMemory<byte> output, int outOffset)
void Reset()
int GetLength()
int GetBlockSize()
void ClearKey()
void ClearSalt()
void Dispose()
Behavior notes
DoFinal(...)finalizes and resets while retaining key/salt configuration for reuse.ClearKey()andClearSalt()allow explicit zeroization of sensitive material.
Performance notes
- On runtimes where
Vector128hardware acceleration is available, the library uses SIMD-assisted code paths for block word loading and chain-value folding in bothBlake2bandBlake2bMac. - On runtimes without SIMD support, the implementation automatically falls back to scalar processing with identical outputs.
- For validation and troubleshooting, SIMD can be disabled at runtime via the
AppContextswitchBlake2b.NetCore.DisableSimd(used by the test suite to assert SIMD/scalar parity).
Best practices
1) Prefer keyed mode (Blake2bMac) for authenticity
- Use
Blake2bfor integrity fingerprinting. - Use
Blake2bMacwhen you need an authentication tag with a secret key.
2) Validate digest size intentionally
- For compatibility with many external systems, 32-byte (256-bit) or 64-byte (512-bit) digests are common.
- Keep digest length consistent across all producers/consumers.
3) Reuse instances when hashing many messages serially
DoFinal(...)resets state, so a single instance can process many independent messages.- Avoid sharing one instance across concurrent threads.
4) Handle sensitive material explicitly
- Keep MAC keys in pinned memory where practical.
- Call
ClearKey()/ClearSalt()when secrets are no longer needed. - Dispose instances promptly (
usingblocks).
5) Stream large inputs in chunks
- For files or network payloads, feed chunks via
UpdateBlock(...)instead of loading everything in memory. - Example chunk size: 4 KB to 1 MB depending on I/O profile.
6) Use fixed encoding when hashing strings
- Always pick an explicit encoding (
UTF8,ASCII, etc.). - Do not hash language runtime string memory directly.
Validation and known test vectors
The test project validates implementation behavior against published vectors and verifies SIMD/scalar parity, including:
- BLAKE2b-512 of empty string
- BLAKE2b-512 of
"abc" - Equivalence checks between SIMD-enabled and SIMD-disabled execution paths for
Blake2bandBlake2bMac
Example expected values:
- Empty string:
786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce abc:ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923
Development
Build
dotnet build Blake2b.NetCore.sln
Test
dotnet test Blake2b.NetCore.sln
If dotnet is installed locally but not on PATH, invoke it explicitly:
$HOME/.dotnet/dotnet test Blake2b.NetCore.sln
Security notes
- This library implements BLAKE2b and keyed BLAKE2b MAC, but not tree hashing mode.
- Cryptographic usage should be reviewed for your threat model and protocol requirements.
- For protocol interoperability, ensure all participants agree on:
- digest size
- keyed vs unkeyed mode
- key and salt handling conventions
- input canonicalization and text encoding
License
MIT. See LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 was computed. 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. |
-
net8.0
- PinnedMemory (>= 1.0.1)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Blake2b.NetCore:
| Package | Downloads |
|---|---|
|
SecureRandom.NetCore
Implementation of a cryptographic pseudorandom number generator (CPRNG) using Blake2b. Optimized for PinnedMemory. |
|
|
Argon2.NetCore
Implementation of Argon2 key derivation function designed by Alex Biryukov, Daniel Dinu, and Dmitry Khovratovich. Optimized for PinnedMemory. |
GitHub repositories
This package is not used by any popular GitHub repositories.