public static byte[] ComputeHash(byte[] input, byte[] key, string algorithm)
        {
            if (input == null || input.Length == 0)
            {
                throw new ArgumentException();
            }
            if (key == null || key.Length == 0)
            {
                throw new ArgumentException();
            }

            System.Security.Cryptography.KeyedHashAlgorithm hash;
            switch (algorithm.ToUpperInvariant())
            {
            case "MD5":
            case "HMACMD5":
                hash = HMACMD5.Create();
                break;

            case "MD160":
            case "RIPEMD160":
            case "HMACRIPEMD160":
                hash = HMACRIPEMD160.Create();
                break;

            case "SHA":
            case "SHA1":
            case "HMACSHA":
            case "HMACSHA1":
                hash = HMACSHA1.Create();
                break;

            case "SHA256":
            case "HMACSHA256":
                hash = HMACSHA256.Create();
                break;

            case "SHA384":
            case "HMACSHA384":
                hash = HMACSHA384.Create();
                break;

            case "SHA512":
            case "HMACSHA512":
                hash = HMACSHA512.Create();
                break;

            default:
                throw new NotSupportedException();
            }
            hash.Key = key;
            byte[] result = hash.ComputeHash(input);
            hash.Clear();
            return(result);
        }
Beispiel #2
0
 public void HmaCalls()
 {
     using (var hmacripemd160 = HMACRIPEMD160.Create("HMACRIPEMD160")) // Noncompliant
     {
     }
     using (var hmacripemd160 = KeyedHashAlgorithm.Create("HMACRIPEMD160")) // Noncompliant
     {
     }
     using (var hmacripemd160 = (KeyedHashAlgorithm)CryptoConfig.CreateFromName("HMACRIPEMD160")) // Noncompliant
     {
     }
 }
Beispiel #3
0
        public void HmaCalls()
        {
            using (var hmac = new HMACSHA1()) // Noncompliant
            {
            }
            using (var hmac = HMAC.Create()) // Noncompliant
            {
            }
            using (var hmacmd5 = HMACMD5.Create("HMACMD5")) // Noncompliant
            {
            }
            using (var hmacmd5 = KeyedHashAlgorithm.Create("HMACMD5")) // Noncompliant
            {
            }
            using (var hmacmd5 = (KeyedHashAlgorithm)CryptoConfig.CreateFromName("HMACMD5")) // Noncompliant
            {
            }

            using (var hmacripemd160 = HMACRIPEMD160.Create("HMACRIPEMD160")) // Noncompliant
            {
            }
            using (var hmacripemd160 = KeyedHashAlgorithm.Create("HMACRIPEMD160")) // Noncompliant
            {
            }
            using (var hmacripemd160 = (KeyedHashAlgorithm)CryptoConfig.CreateFromName("HMACRIPEMD160")) // Noncompliant
            {
            }

            using (var hmacsha256 = HMACSHA256.Create("HMACSHA256"))
            {
            }
            using (var hmacsha256 = KeyedHashAlgorithm.Create("HMACSHA256"))
            {
            }
            using (var hmacsha256 = (KeyedHashAlgorithm)CryptoConfig.CreateFromName("HMACSHA256"))
            {
            }
        }