/// <summary> /// Creates a cryptographically strong random verification code. /// </summary> /// <param name="format">The desired format of the verification code.</param> /// <param name="length">The length of the code. /// When <paramref name="format"/> is <see cref="VerificationCodeFormat.IncludedInCallback"/>, /// this is the length of the original byte array before base64 encoding rather than the actual /// length of the final string.</param> /// <returns>The verification code.</returns> public static string CreateVerificationCode(VerificationCodeFormat format, int length) { Requires.InRange(length >= 0, "length"); switch (format) { case VerificationCodeFormat.IncludedInCallback: return(MessagingUtilities.GetCryptoRandomDataAsBase64(length)); case VerificationCodeFormat.AlphaNumericNoLookAlikes: return(MessagingUtilities.GetRandomString(length, MessagingUtilities.AlphaNumericNoLookAlikes)); case VerificationCodeFormat.AlphaUpper: return(MessagingUtilities.GetRandomString(length, MessagingUtilities.UppercaseLetters)); case VerificationCodeFormat.AlphaLower: return(MessagingUtilities.GetRandomString(length, MessagingUtilities.LowercaseLetters)); case VerificationCodeFormat.Numeric: return(MessagingUtilities.GetRandomString(length, MessagingUtilities.Digits)); default: throw new ArgumentOutOfRangeException("format"); } }
private void TestCode(VerificationCodeFormat format, int length, string allowableCharacters) { string code = ServiceProvider.CreateVerificationCode(format, length); TestUtilities.TestLogger.InfoFormat("{0} of length {2}: {1}", format, code, length); Assert.AreEqual(length, code.Length); foreach (char ch in code) { Assert.IsTrue(allowableCharacters.Contains(ch)); } }
/// <summary> /// Creates a cryptographically strong random verification code. /// </summary> /// <param name="format">The desired format of the verification code.</param> /// <param name="length">The length of the code. /// When <paramref name="format"/> is <see cref="VerificationCodeFormat.IncludedInCallback"/>, /// this is the length of the original byte array before base64 encoding rather than the actual /// length of the final string.</param> /// <returns>The verification code.</returns> public static string CreateVerificationCode(VerificationCodeFormat format, int length) { Requires.InRange(length >= 0, "length"); switch (format) { case VerificationCodeFormat.IncludedInCallback: return MessagingUtilities.GetCryptoRandomDataAsBase64(length); case VerificationCodeFormat.AlphaNumericNoLookAlikes: return MessagingUtilities.GetRandomString(length, MessagingUtilities.AlphaNumericNoLookAlikes); case VerificationCodeFormat.AlphaUpper: return MessagingUtilities.GetRandomString(length, MessagingUtilities.UppercaseLetters); case VerificationCodeFormat.AlphaLower: return MessagingUtilities.GetRandomString(length, MessagingUtilities.LowercaseLetters); case VerificationCodeFormat.Numeric: return MessagingUtilities.GetRandomString(length, MessagingUtilities.Digits); default: throw new ArgumentOutOfRangeException("format"); } }