ISecureRandom from AddRandomness()
Keys, tokens, salts, nonces, passwords and any value an attacker must not predict, in an application with dependency injection.
- Safe for secrets: yes
- Reproducible by others: no
- Needs dependency injection: yes
public sealed class TokenService(ISecureRandom random){ private const string Alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
public string NewRecoveryCode() => random.GetString(Alphabet, 16);
public int RollDie() => random.NextInteger(1, 7);}- Watch for
- Fill a key into a span you clear; never keep it in the
byte[]GetBytesreturns.