コード例 #1
0
ファイル: RandomUtil.cs プロジェクト: bowmark/allauth.lib
        /// <summary>
        /// Generates a random string.
        /// </summary>
        /// <param name="length"></param>
        /// <param name="generatorOptions"></param>
        /// <returns></returns>
        public static string GenerateRandomString(int length, StringGeneratorOptions generatorOptions)
        {
            if (Convert.ToInt32(generatorOptions) == 0)
                throw new Exception("At least one generator option must be specified");

            var charString = "";
            if (generatorOptions.HasFlag(StringGeneratorOptions.UpperCase))
                charString += CharsetUpperCase;
            if (generatorOptions.HasFlag(StringGeneratorOptions.LowerCase))
                charString += CharsetLowerCase;
            if (generatorOptions.HasFlag(StringGeneratorOptions.Digits))
                charString += CharsetDigits;
            if (generatorOptions.HasFlag(StringGeneratorOptions.Special))
                charString += CharsetSpecial;

            var chars = charString.ToCharArray();
            var data = new byte[1];
            var result = new StringBuilder(length);

            SecureRandomBc.NextBytes(data);
            data = new byte[length];
            SecureRandomBc.NextBytes(data);

            foreach (var dataByte in data)
            {
                result.Append(chars[dataByte % chars.Length]);
            }

            return result.ToString();
        }
コード例 #2
0
ファイル: RandomUtil.cs プロジェクト: bowmark/allauth.lib
        /// <summary>
        /// Generates a random string.
        /// </summary>
        /// <param name="length"></param>
        /// <param name="generatorOptions"></param>
        /// <returns></returns>
        public static string GenerateRandomString(int length, StringGeneratorOptions generatorOptions)
        {
            if (Convert.ToInt32(generatorOptions) == 0)
            {
                throw new Exception("At least one generator option must be specified");
            }

            var charString = "";

            if (generatorOptions.HasFlag(StringGeneratorOptions.UpperCase))
            {
                charString += CharsetUpperCase;
            }
            if (generatorOptions.HasFlag(StringGeneratorOptions.LowerCase))
            {
                charString += CharsetLowerCase;
            }
            if (generatorOptions.HasFlag(StringGeneratorOptions.Digits))
            {
                charString += CharsetDigits;
            }
            if (generatorOptions.HasFlag(StringGeneratorOptions.Special))
            {
                charString += CharsetSpecial;
            }

            var chars  = charString.ToCharArray();
            var data   = new byte[1];
            var result = new StringBuilder(length);

            SecureRandomBc.NextBytes(data);
            data = new byte[length];
            SecureRandomBc.NextBytes(data);

            foreach (var dataByte in data)
            {
                result.Append(chars[dataByte % chars.Length]);
            }

            return(result.ToString());
        }