private void ComputeHash(string path) { using (var fileStream = File.OpenRead(path)) { using (var hasher = HMACSHA384.Create()) { _result = hasher.ComputeHash(fileStream); } } }
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); }
private static byte[] GetHash([CanBeNull] string input, EHashType hash) { var inputBytes = Encoding.ASCII.GetBytes(input); switch (hash) { case EHashType.HMAC: return(HMAC.Create().ComputeHash(inputBytes)); #pragma warning disable RECS0030 // Suggests using the class declaring a static function when calling it case EHashType.HMACMD5: // DevSkim: ignore DS126858 return(HMACMD5.Create().ComputeHash(inputBytes)); // DevSkim: ignore DS126858 case EHashType.HMACSHA1: // DevSkim: ignore DS126858 return(HMACSHA1.Create().ComputeHash(inputBytes)); // DevSkim: ignore DS126858 case EHashType.HMACSHA256: return(HMACSHA256.Create().ComputeHash(inputBytes)); case EHashType.HMACSHA384: return(HMACSHA384.Create().ComputeHash(inputBytes)); case EHashType.HMACSHA512: return(HMACSHA512.Create().ComputeHash(inputBytes)); #pragma warning restore RECS0030 // Suggests using the class declaring a static function when calling it case EHashType.MD5: // DevSkim: ignore DS126858 #pragma warning disable SG0006 // Weak hashing function return(MD5.Create().ComputeHash(inputBytes)); // DevSkim: ignore DS126858 #pragma warning restore SG0006 // Weak hashing function case EHashType.SHA1: // DevSkim: ignore DS126858 #pragma warning disable SG0006 // Weak hashing function return(SHA1.Create().ComputeHash(inputBytes)); // DevSkim: ignore DS126858 #pragma warning restore SG0006 // Weak hashing function case EHashType.SHA256: return(SHA256.Create().ComputeHash(inputBytes)); case EHashType.SHA384: return(SHA384.Create().ComputeHash(inputBytes)); case EHashType.SHA512: return(SHA512.Create().ComputeHash(inputBytes)); default: return(inputBytes); } }
private async Task <byte[]> ComputeHashAsync(string path) { using (var fileStream = File.OpenRead(path)) { using (var hasher = HMACSHA384.Create()) { return(await Task.Run(() => { return hasher.ComputeHash(fileStream); })); } } }
private static byte[] GetHash(string Source, HashType hash) { byte[] inputBytes = Encoding.ASCII.GetBytes(Source); switch (hash) { case HashType.HMAC: return(HMAC.Create().ComputeHash(inputBytes)); case HashType.HMACMD5: return(HMACMD5.Create().ComputeHash(inputBytes)); case HashType.HMACSHA1: return(HMACSHA1.Create().ComputeHash(inputBytes)); case HashType.HMACSHA256: return(HMACSHA256.Create().ComputeHash(inputBytes)); case HashType.HMACSHA384: return(HMACSHA384.Create().ComputeHash(inputBytes)); case HashType.HMACSHA512: return(HMACSHA512.Create().ComputeHash(inputBytes)); /* * case HashType.MACTripleDES: * return MACTripleDES.Create().ComputeHash(inputBytes); */ case HashType.MD5: return(MD5.Create().ComputeHash(inputBytes)); /* * case HashType.RIPEMD160: * return RIPEMD160.Create().ComputeHash(inputBytes); */ case HashType.SHA1: return(SHA1.Create().ComputeHash(inputBytes)); case HashType.SHA256: return(SHA256.Create().ComputeHash(inputBytes)); case HashType.SHA384: return(SHA384.Create().ComputeHash(inputBytes)); case HashType.SHA512: return(SHA512.Create().ComputeHash(inputBytes)); default: return(inputBytes); } }
private static byte[] GetHash(string input, EHashType hash) { var inputBytes = Encoding.ASCII.GetBytes(input); switch (hash) { case EHashType.HMAC: return(HMAC.Create().ComputeHash(inputBytes)); case EHashType.HMACMD5: return(HMACMD5.Create().ComputeHash(inputBytes)); case EHashType.HMACSHA1: return(HMACSHA1.Create().ComputeHash(inputBytes)); case EHashType.HMACSHA256: return(HMACSHA256.Create().ComputeHash(inputBytes)); case EHashType.HMACSHA384: return(HMACSHA384.Create().ComputeHash(inputBytes)); case EHashType.HMACSHA512: return(HMACSHA512.Create().ComputeHash(inputBytes)); case EHashType.MACTripleDES: return(MACTripleDES.Create().ComputeHash(inputBytes)); case EHashType.MD5: return(MD5.Create().ComputeHash(inputBytes)); case EHashType.RIPEMD160: return(RIPEMD160.Create().ComputeHash(inputBytes)); case EHashType.SHA1: return(SHA1.Create().ComputeHash(inputBytes)); case EHashType.SHA256: return(SHA256.Create().ComputeHash(inputBytes)); case EHashType.SHA384: return(SHA384.Create().ComputeHash(inputBytes)); case EHashType.SHA512: return(SHA512.Create().ComputeHash(inputBytes)); default: return(inputBytes); } }
private static byte[] GetHash(string input, EHashType hash) { byte[] inputBytes = Encoding.ASCII.GetBytes(input); switch (hash) { case EHashType.HMAC: return(HMAC.Create().ComputeHash(inputBytes)); case EHashType.HMACMD5: return(HMACMD5.Create().ComputeHash(inputBytes)); case EHashType.HMACSHA1: return(HMACSHA1.Create().ComputeHash(inputBytes)); case EHashType.HMACSHA256: return(HMACSHA256.Create().ComputeHash(inputBytes)); case EHashType.HMACSHA384: return(HMACSHA384.Create().ComputeHash(inputBytes)); case EHashType.HMACSHA512: return(HMACSHA512.Create().ComputeHash(inputBytes)); #pragma warning disable CS0618 // Type or member is obsolete case EHashType.MD5: #pragma warning restore CS0618 // Type or member is obsolete return(MD5.Create().ComputeHash(inputBytes)); #pragma warning disable CS0618 // Type or member is obsolete case EHashType.SHA1: #pragma warning restore CS0618 // Type or member is obsolete return(SHA1.Create().ComputeHash(inputBytes)); case EHashType.SHA256: return(SHA256.Create().ComputeHash(inputBytes)); case EHashType.SHA384: return(SHA384.Create().ComputeHash(inputBytes)); case EHashType.SHA512: return(SHA512.Create().ComputeHash(inputBytes)); default: return(inputBytes); } }
private static byte[] GetHash(string input, eHashType hash) { byte[] inputBytes = Encoding.ASCII.GetBytes(input); switch (hash) { case eHashType.HMAC: return(HMAC.Create().ComputeHash(inputBytes)); case eHashType.HMACMD5: return(HMACMD5.Create().ComputeHash(inputBytes)); case eHashType.HMACSHA1: return(HMACSHA1.Create().ComputeHash(inputBytes)); case eHashType.HMACSHA256: return(HMACSHA256.Create().ComputeHash(inputBytes)); case eHashType.HMACSHA384: return(HMACSHA384.Create().ComputeHash(inputBytes)); case eHashType.HMACSHA512: return(HMACSHA512.Create().ComputeHash(inputBytes)); case eHashType.MD5: return(MD5.Create().ComputeHash(inputBytes)); case eHashType.SHA1: return(SHA1.Create().ComputeHash(inputBytes)); case eHashType.SHA256: return(SHA256.Create().ComputeHash(inputBytes)); case eHashType.SHA384: return(SHA384.Create().ComputeHash(inputBytes)); case eHashType.SHA512: return(SHA512.Create().ComputeHash(inputBytes)); default: return(inputBytes); } }