dotnet-cryptography
.NET cryptography
Cryptography in .NET is a library of correct primitives that are easy to assemble incorrectly. The job is almost never to invent a scheme - it is to pick the primitive the situation calls for and use it the single way it is meant to be used. Everything here lives in System.Security.Cryptography. Floor is .NET 8 / C# 12, which covers every classical primitive below; post-quantum is a .NET 10+ addition flagged at the end.
Two boundaries this skill does not cross. Where keys and secrets live - a vault, a managed key service, environment config - is your secrets layer, never a literal in source and never a checked-in file. Signing a user in is dotnet-authentication. This skill is only the math and the API around it. On .NET Framework 4.8 two defaults are footguns - PBKDF2's SHA-1 default and the RandomNumberGenerator API name - covered in references/net-framework-48.md.
First principle: use the static one-shots
Each algorithm exposes static helpers (SHA256.HashData, AesGcm, RSA.Encrypt) that own buffer sizing and disposal. Reach for those before constructing and managing an instance yourself. Two rules sit above every section below:
- Entropy comes from
RandomNumberGenerator(RandomNumberGenerator.GetBytes(n)), the only acceptable source for keys, salts, and nonces.System.Random/Guidare not random in the security sense - never seed crypto from them. - Compare any two secrets with
CryptographicOperations.FixedTimeEquals, never==orSequenceEqual. A short-circuiting comparison leaks how many leading bytes matched through its timing, which is enough to recover a MAC or token byte by byte.
Hashing for integrity only
SHA256.HashData, or SHA384/SHA512 where a longer digest is wanted, answers one question: did these bytes change. Checksums, content addressing, change detection, the hash half of a signature. The async HashDataAsync overload streams a large file without loading it.
A plain hash is not a password store and not a MAC - it has no key and no work factor. Those are the next two sections. If you find yourself salting a SHA-256 by hand to protect a password, stop and reach for PBKDF2.