diff options
author | Christoph Enzmann <christoph.enzmann@confer.ch> | 2013-12-04 17:07:02 +0100 |
---|---|---|
committer | Christoph Enzmann <christoph.enzmann@confer.ch> | 2013-12-04 17:07:02 +0100 |
commit | aefa4764cc1e858da39bc3af4e6a385d924967a2 (patch) | |
tree | 48c046d1b3c3a5ec34f211dd08075680c62493d8 | |
parent | 278f0916b88304c7d427723f43df93e8b9327a6a (diff) | |
download | TwoStepsAuthenticator-aefa4764cc1e858da39bc3af4e6a385d924967a2.zip TwoStepsAuthenticator-aefa4764cc1e858da39bc3af4e6a385d924967a2.tar.gz TwoStepsAuthenticator-aefa4764cc1e858da39bc3af4e6a385d924967a2.tar.bz2 |
Use RNGCryptoServiceProvider Random Generator
-rw-r--r-- | TwoStepsAuthenticator/Authenticator.cs | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/TwoStepsAuthenticator/Authenticator.cs b/TwoStepsAuthenticator/Authenticator.cs index 0f0e298..3e263e6 100644 --- a/TwoStepsAuthenticator/Authenticator.cs +++ b/TwoStepsAuthenticator/Authenticator.cs @@ -10,15 +10,16 @@ namespace TwoStepsAuthenticator public class Authenticator { private static Lazy<UsedCodesManager> usedCodes = new Lazy<UsedCodesManager>(); + private static readonly RNGCryptoServiceProvider Random = new RNGCryptoServiceProvider(); // Is Thread-Safe + private static readonly int KeyLength = 16; + private static readonly string AvailableKeyChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; public string GenerateKey() { - var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; - var random = new Random(); - var keyChars = new char[16]; - for (int i = 0; i < 16; i++) + var keyChars = new char[KeyLength]; + for (int i = 0; i < keyChars.Length; i++) { - keyChars[i] = chars[random.Next(chars.Length)]; + keyChars[i] = AvailableKeyChars[RandomInt(AvailableKeyChars.Length)]; } return new String(keyChars); } @@ -85,6 +86,12 @@ namespace TwoStepsAuthenticator return false; } + public int RandomInt(int max) { + var randomBytes = new byte[4]; + Random.GetBytes(randomBytes); + + return Math.Abs((int)BitConverter.ToUInt32(randomBytes, 0) % max); + } } |