Пример #1
0
 /// <summary>
 /// Creates a cryptographically strong string
 /// </summary>
 /// <returns></returns>
 public static string Create16CharacterCryptographicallyStrongString(
     CryptoStringTypeEnum stringType = CryptoStringTypeEnum.LowercaseAlphaNumeric)
 {
     return(CreateCryptographicallyStrongString <Sha256Digest>(
                stringType)
            .Substring(0, 16));
 }
Пример #2
0
        /// <summary>
        /// Creates a cryptographically strong string with the given digest
        /// </summary>
        /// <typeparam name="TDigest">Hash type</typeparam>
        /// <param name="stringType">Filter to apply to string</param>
        /// <returns>A string with only the characters defined by stringType</returns>
        private static string CreateCryptographicallyStrongString <TDigest>(
            CryptoStringTypeEnum stringType)
            where TDigest : IDigest, new()
        {
            var randomBytes = BitConverter.GetBytes(
                new SecureRandom().NextInt());

            var digest = new TDigest();

            digest.BlockUpdate(
                randomBytes,
                0,
                randomBytes.Length);
            var randomDigest = new DigestRandomGenerator(digest);

            var cryptoData = new byte[digest.GetDigestSize()];

            randomDigest.NextBytes(cryptoData);

            var result = Convert.ToBase64String(cryptoData);


            switch (stringType)
            {
            case CryptoStringTypeEnum.Base64AlphaNumeric:
                return(result
                       .Replace('/', '_')
                       .Replace('+', '-'));

            case CryptoStringTypeEnum.LowercaseAlphaNumeric:
                return(result
                       .Replace('/', 'a')
                       .Replace('+', 'b')
                       .ToLower());

            case CryptoStringTypeEnum.Base64:
            default:
                return(result);
            }
        }
Пример #3
0
 public static string Create32CharacterCryptographicallyStrongString(
     CryptoStringTypeEnum stringType = CryptoStringTypeEnum.Base64AlphaNumeric)
 {
     return(CreateCryptographicallyStrongString <Sha256Digest>(
                stringType));
 }