I want to generate a 4 character long CSPRN (Cryptographically Secure Psuedo Random Number) string. I know I can create an 8 character one by creating a 5 byte long random array and encoding as base32:
string CSPRN = "";
System.Security.Cryptography.RandomNumberGenerator rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
byte[] tokenData = new byte[5];
rng.GetBytes(tokenData);
CSPRN = Base32.ToBase32String(tokenData); //should produce a string 5bytes*1.6charsperbyte = 8 chars long.
If I now take a substring of the first 4 characters of CSPRN - is it still a CSPRN?
My best guess is that it is, but wondering if there is any "gotcha"s from taking a substring rather than generating a smaller number.
Aucun commentaire:
Enregistrer un commentaire