Ejemplo n.º 1
0
        public static byte[] ToHmac(this byte[] data, byte[] key, HashAlgorithm algorithm = HashAlgorithm.Sha512)
        {
            switch (algorithm)
            {
            case HashAlgorithm.Md5:
                using (System.Security.Cryptography.HMACMD5 hmac = new(key))
                {
                    return(hmac.ComputeHash(data));
                }

            case HashAlgorithm.Sha1:
                using (System.Security.Cryptography.HMACSHA1 hmac = new(key))
                {
                    return(hmac.ComputeHash(data));
                }

            case HashAlgorithm.Sha256:
                using (System.Security.Cryptography.HMACSHA256 hmac = new(key))
                {
                    return(hmac.ComputeHash(data));
                }

            case HashAlgorithm.Sha384:
                using (System.Security.Cryptography.HMACSHA384 hmac = new(key))
                {
                    return(hmac.ComputeHash(data));
                }

            case HashAlgorithm.Sha512:
                using (System.Security.Cryptography.HMACSHA512 hmac = new(key))
                {
                    return(hmac.ComputeHash(data));
                }

            default:
                throw new NotImplementedException(algorithm.ToString());
            }
        }
Ejemplo n.º 2
0
        public static byte[] ToHash(this byte[] data, HashAlgorithm algorithm = HashAlgorithm.Sha512)
        {
            switch (algorithm)
            {
            case HashAlgorithm.Md5:
                return(MD5.Create().ComputeHash(data));

            case HashAlgorithm.Sha1:
                return(SHA1.Create().ComputeHash(data));

            case HashAlgorithm.Sha256:
                return(SHA256.Create().ComputeHash(data));

            case HashAlgorithm.Sha384:
                return(SHA384.Create().ComputeHash(data));

            case HashAlgorithm.Sha512:
                return(SHA512.Create().ComputeHash(data));

            default:
                throw new NotImplementedException(algorithm.ToString());
            }
        }