GuidesMake a key, token or code
Make a key, token or code
Fill keys into memory you clear, and make tokens, recovery codes and identifiers from the secure generator.
A key, a token or a recovery code is useful only while nobody can guess it, so it must come from a generator that is unpredictable and never from a value that others can read. ISecureRandom is the interface for exactly that: a reproducible source such as a draw never implements it. This page shows how to fill a key into memory you clear, how to make a token or a recovery code, how to make an identifier, how to keep a key that outlives one method, and what to watch for.
Fill a key
Section titled “Fill a key”Fill writes random bytes into any span. Fill a key into a stackalloc span, use it, and clear it in a finally block, so no copy of it stays in memory that the garbage collector can move and leave copies of behind.
Span<byte> key = stackalloc byte[32];try{ random.Fill(key); HMACSHA256.HashData(key, message, mac);}finally{ CryptographicOperations.ZeroMemory(key);}// The key is all zeros again, and mac holds the code.A token or recovery code
Section titled “A token or recovery code”GetString builds a string from an alphabet you choose, drawing each character without bias. Leave out the characters that people misread, such as 0, O, 1 and I, for a code that someone types by hand.
// 16 characters, with no 0, O, 1 or I to misread.var code = random.GetString("ABCDEFGHJKLMNPQRSTUVWXYZ23456789", 16);An identifier
Section titled “An identifier”NextGuid returns a random version 4 GUID, which suits an identifier that must not be guessable, such as a session or a request identifier.
var id = random.NextGuid(); // a version 4 GUIDA key that outlives one method
Section titled “A key that outlives one method”A stackalloc span cannot leave its method. For a key that lives longer, use a pinned buffer that is zeroed when you dispose it, such as BufferFactory.Rent(32, isSensitive: true, out Span<byte> key) from NCode.Buffers. Fill takes any span, so you fill the rented span the same way, and neither package depends on the other.
Pitfalls
Section titled “Pitfalls”- Deriving a secret from a pulse or draw.
- You write a key, a token or a code from the bytes of a beacon pulse or of a
VerifiableRandom. - What happens: everyone who reads the beacon sees the same bytes, so they can compute your secret. ADR-1000 records why a beacon’s randomness is never used alone for a secret.
- Write instead: take the secret from
ISecureRandom.
- You write a key, a token or a code from the bytes of a beacon pulse or of a
- Keeping a key in the
byte[]thatGetBytesreturns.- You write
var key = random.GetBytes(32);and keep it for the life of the process. - What happens: the garbage collector can move the array and leave copies of the key in memory, and you cannot clear them.
- Write instead:
Filla span that you clear, as in the first sample, or a pinned buffer that is zeroed on dispose.
- You write
- Using
Random.Sharedornew Random()for a token.- You write
Random.Shared.Next()to pick the characters of a token. - What happens: the generator is predictable, so someone who sees a few values can work out the next ones.
- Write instead: use
ISecureRandom.
- You write
- Reducing a draw with
%for a range.- You write
random.NextInteger<int>() % 6for a number from 0 to 5. - What happens: the result is biased, because the range does not divide the values evenly, so some numbers come up more often.
- Write instead:
random.NextInteger(0, 6), which samples by rejection and returns every number with the same chance.
- You write
Next steps
Section titled “Next steps”- Draw values shows how to draw integers, shuffle a list and pick from it.
- Which API? helps you pick the API that fits the value you need.