NCode.Buffers
5.7.1
Prefix Reserved
dotnet add package NCode.Buffers --version 5.7.1
NuGet\Install-Package NCode.Buffers -Version 5.7.1
<PackageReference Include="NCode.Buffers" Version="5.7.1" />
<PackageVersion Include="NCode.Buffers" Version="5.7.1" />
<PackageReference Include="NCode.Buffers" />
paket add NCode.Buffers --version 5.7.1
#r "nuget: NCode.Buffers, 5.7.1"
#:package NCode.Buffers@5.7.1
#addin nuget:?package=NCode.Buffers&version=5.7.1
#tool nuget:?package=NCode.Buffers&version=5.7.1
NCode.Buffers
Pinned, pooled .NET buffers that zero themselves on dispose, for keys and other secrets, plus fixed-size buffer writers and string splitting without substring copies.
The documentation site has a quick start, a guide for each task, the concepts behind the buffers, the evidence for each guarantee and a reference for every public member.
Installation
dotnet add package NCode.Buffers
The target frameworks are the ones the package's .csproj declares; they are listed on nuget.org's Frameworks tab and shown by the badge above. CHANGELOG.md records when a framework is added or dropped and which release line to stay on. The package depends on Nerdbank.Streams, whose Sequence<T> the pooled buffer writers return.
Example
This example derives a key from a password, verifies an HMAC-SHA256 signature with it, and zeroes the derived key and the expected MAC when the method returns.
using System.Security.Cryptography;
using NCode.Buffers;
public static class Signatures
{
public static bool VerifySignature(
ReadOnlySpan<byte> password,
ReadOnlySpan<byte> salt,
ReadOnlySpan<byte> payload,
ReadOnlySpan<byte> signature
)
{
// A pinned, pooled 32-byte key, zeroed when keyOwner is disposed.
using var keyOwner = BufferFactory.Rent(32, isSensitive: true, out Span<byte> key);
Rfc2898DeriveBytes.Pbkdf2(
password,
salt,
key,
iterations: 600_000,
HashAlgorithmName.SHA256
);
// The expected MAC lives on the stack and is zeroed when expectedLifetime is disposed.
Span<byte> expected = stackalloc byte[HMACSHA256.HashSizeInBytes];
using var expectedLifetime = expected.GetSecureLifetime();
HMACSHA256.HashData(key, payload, expected);
// Constant time, so the comparison doesn't reveal where the MACs differ.
return CryptographicOperations.FixedTimeEquals(expected, signature);
}
}
Binary text
BinaryTextEncoding converts between bytes and Base64, Base64url or upper or lower case hex text, in every BinaryTextFormat. RentEncoded and RentDecoded rent the result, WriteEncoded and WriteDecoded and their Utf8 forms write to a caller's buffer writer, TryEncode and TryDecode and their Utf8 forms work in a caller's span, IsValid and IsValidUtf8 check text, GetEncodedLength, GetMaxDecodedLength and GetDecodedLength size buffers, and EncodeToString and DecodeToArray turn a public value into a string or a byte[], which nothing zeroes. Decode base64url with Base64UrlStrict, which refuses text the strict rules reject. Both hexadecimal formats decode digits in either case; HexUpper and HexLower differ only in the case they encode.
IsValid checks text without decoding it or throwing. GetEncodedLength, GetMaxDecodedLength and GetDecodedLength size a buffer: the last is exact for Base64UrlStrict and hex, whose length fixes the count, and throws for Base64 and Base64Url, which are sized with GetMaxDecodedLength and the count written. TryEncode and TryDecode fill a span you pass and return false only when it is too short, writing nothing then; invalid text throws FormatException with the span zeroed.
// Check strict Base64url text, then decode it into a stack buffer that is zeroed on dispose.
const string text = "-_8B";
var isValid = BinaryTextEncoding.IsValid(text, BinaryTextFormat.Base64UrlStrict);
var length = BinaryTextEncoding.GetDecodedLength(
text.Length,
BinaryTextFormat.Base64UrlStrict
);
Span<byte> key = stackalloc byte[length];
using var keyLifetime = key.GetSecureLifetime();
var isDecoded = BinaryTextEncoding.TryDecode(
text,
BinaryTextFormat.Base64UrlStrict,
key,
out var count
);
IsValidUtf8, TryEncodeUtf8, TryDecodeUtf8, WriteEncodedUtf8 and WriteDecodedUtf8 do the same for UTF-8 text, such as a JSON value's bytes, and accept and write exactly the text the character forms do.
// Decode UTF-8 text into a sensitive writer, and encode a hash as UTF-8 hex.
using var keyWriter = BufferFactory.CreatePooledBufferWriter(isSensitive: true);
var keyLength = BinaryTextEncoding.WriteDecodedUtf8(
"-_8B"u8,
BinaryTextFormat.Base64UrlStrict,
keyWriter
);
var hash = SHA256.HashData("data"u8);
Span<byte> hex = stackalloc byte[64];
var isEncoded = BinaryTextEncoding.TryEncodeUtf8(
hash,
BinaryTextFormat.HexLower,
hex,
out var hexLength
);
EncodeToString and DecodeToArray return a string or a byte[], which nothing zeroes, for public values such as a thumbprint, a hash or an identifier.
// A hash is public: keep its text as a string, and its bytes as an array.
var hash = SHA256.HashData("data"u8);
var thumbprint = BinaryTextEncoding.EncodeToString(hash, BinaryTextFormat.Base64UrlStrict);
byte[] decodedHash = BinaryTextEncoding.DecodeToArray(
thumbprint,
BinaryTextFormat.Base64UrlStrict
);
Learn more
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- Nerdbank.Streams (>= 2.14.416)
NuGet packages (68)
Showing the top 5 NuGet packages that depend on NCode.Buffers:
| Package | Downloads |
|---|---|
|
NCode.Extensions.DataProtection
Extensions and utilities for ASP.NET Core Data Protection, including a type-safe factory pattern for creating IDataProtector instances with automatic purpose string derivation, and high-performance span-based protection operations using IBufferWriter. |
|
|
NCode.Identity.Json
The JSON value model of JOSE, OAuth and OpenID Connect for NCode.Identity: NumericDate coercion, claim sets as object graphs, strict claim reads on JsonElement and the family's JSON converters. |
|
|
NCode.Identity.Jwk
First-class JSON Web Key (JWK) and JSON Web Key Set (JWKS) types for NCode.Identity, with JWK Thumbprint (RFC 7517 / 7638 / 9278) — a rich, serializable key model shared by the JOSE and Secrets libraries. |
|
|
NCode.Identity.Secrets.Abstractions
Package Description |
|
|
NCode.Identity.Events.Abstractions
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 5.7.1 | 57 | 12/4/2026 |
| 5.7.0 | 731 | 12/3/2026 |
| 5.6.1 | 208 | 11/30/2026 |
| 5.6.0 | 136 | 11/29/2026 |
| 5.5.0 | 178 | 11/21/2026 |
| 5.4.0 | 1,266 | 11/21/2026 |
| 5.3.2 | 247 | 11/20/2026 |
| 5.3.1 | 135 | 11/19/2026 |
| 5.3.0 | 119 | 11/19/2026 |
| 5.2.5 | 144 | 11/19/2026 |
| 5.2.4 | 114 | 11/19/2026 |
| 5.2.3 | 116 | 11/18/2026 |
| 5.2.2 | 126 | 11/18/2026 |
| 5.2.1 | 126 | 11/18/2026 |
| 5.2.0 | 118 | 11/18/2026 |
| 5.1.3 | 129 | 11/18/2026 |
| 5.1.2 | 120 | 11/17/2026 |
| 5.1.1 | 116 | 11/17/2026 |
| 5.1.0 | 117 | 11/17/2026 |
| 5.0.3 | 125 | 11/15/2026 |
See the changelog: https://github.com/NCodeGroup/Buffers/blob/HEAD/CHANGELOG.md