Security Library for .NET

Cryptography and secure data handling for .NET, from hashes and signatures to tokens and secrets kept safe in memory.

What's inside

Discover the capabilities built to elevate your .NET applications.

Security Tokens

SecurityTokenBuilder generates tokens of any length from the characters you choose: lower and upper case letters, digits, two sets of special characters, or an alphabet of your own. Draw from the operating system's cryptographically secure random number generator for anything that has to be unguessable, or from a seeded pseudo-random source when you need repeatable output in tests.

Typed Cryptographic Primitives

Keys, signatures, hashes, salts and secrets are distinct types with value semantics, so a hash can't be passed where a signature is expected and nothing is handled as anonymous bytes. They parse and format as hex, convert from spans and memory, and compare in constant time.

Digital Signatures

Sign and verify data with ECDSA, EdDSA and RSA. ECDSA works on any curve the platform supports and with any hash algorithm, including SHA-3, in IEEE P1363 or DER format, and RSA supports PKCS #1 v1.5 and PSS padding. Check a signature with a simple true or false, or verify it with an exception on failure.

Ed25519 in Managed Code

EdDSA is a managed implementation of Ed25519 as specified in RFC 8032, with constant-time scalar multiplication, tested against the RFC's own vectors and Project Wycheproof's. It doesn't depend on the platform's own support for Ed25519.

Key Agreement

ECDH private keys derive a shared secret from another party's public key and return it as a Secret, so the agreed key material is encrypted in memory from the moment it exists.

Key Import and Export

Keys move in and out in the standard formats: PKCS #8, optionally encrypted with a password, SubjectPublicKeyInfo and PEM, alongside the raw SEC1, PKCS #1 and Ed25519 encodings. Named keys carry their algorithm with them, so a stored key always says what it's for.

Hashes

Hash computes digests with any HashAlgorithm, including HMAC and SHA-3, over bytes, text, streams or your own types, with optional repeated rounds. Hashes compare in constant time, order by value and concatenate, and named hashes record the algorithm that produced them.

SHA-3 and SHAKE

A fully managed implementation of FIPS 202: SHA3-224, SHA3-256, SHA3-384 and SHA3-512, plus the SHAKE128 and SHAKE256 extendable-output functions with an output length of your choosing. Each is a standard .NET HashAlgorithm, so it drops in wherever SHA-2 does.

Merkle Trees

Build a Merkle tree from hashes, or from any of your own types that can hash themselves, with whichever hash algorithm you choose. The root summarises the whole set in a single hash, so two collections can be checked for integrity by comparing one value.

Salts

Salt generates cryptographically strong random values of any length, with the option to exclude zero bytes, ready for hashing passwords and other values.

Secrets in Memory

Secret keeps sensitive values such as API keys and credentials encrypted in memory, each under a key of its own, and clears the original bytes as soon as it's created. Private keys are protected the same way. When you need a value, decrypt it into a buffer you control, and compare two secrets without revealing either.

Built for Modern .NET

Targets .NET 8, 9 and 10. The security package depends only on the ONIXLabs Core library, and the cryptography package adds only Microsoft's System.Security.Cryptography.Pkcs.