public void HMACRIPEMD160_d(string testName, HMACRIPEMD160 hmac, byte[] input, byte[] result) { hmac.TransformFinalBlock(input, 0, input.Length); Assert.AreEqual(result, hmac.Hash, testName + ".d"); // required or next operation will still return old hash hmac.Initialize(); }
/// <summary> /// /// </summary> /// <param name="message"></param> /// <param name="secret"></param> /// <param name="compareToHashString"></param> /// <returns></returns> public static bool IsHMACRIPEMD160Valid(string message, string secret, string compareToHashString) { try { secret = secret ?? ""; var encoding = new System.Text.ASCIIEncoding(); byte[] keyBytes = encoding.GetBytes(secret); byte[] messageBytes = encoding.GetBytes(message); using (var hmacripemd160 = new HMACRIPEMD160(keyBytes)) { byte[] hashMessage = hmacripemd160.ComputeHash(messageBytes); string hashString = Convert.ToBase64String(hashMessage).ToLower(); bool isValid = hashString == compareToHashString.ToLower(); return(isValid); } } catch (Exception ex) { Debug.WriteLine("IsHMACRIPEMD160Valid : " + ex.Message); throw; } }
public static byte[] ToHmacRIPEMD160(this byte[] s, byte[] key) { using (var hmac = new HMACRIPEMD160(key)) { return(hmac.ComputeHash(s)); } }
public static byte[] ToHmacRIPEMD160(this string s, byte[] key, Encoding encoding) { using (var hmac = new HMACRIPEMD160(key)) { return(hmac.ComputeHash(s.GetBytes(encoding))); } }
} //end main // Computes a keyed hash for a source file and creates a target file with the keyed hash // prepended to the contents of the source file. public static void SignFile(byte[] key, String sourceFile, String destFile) { // Initialize the keyed hash object. using (HMACRIPEMD160 hmac = new HMACRIPEMD160(key)) { using (FileStream inStream = new FileStream(sourceFile, FileMode.Open)) { using (FileStream outStream = new FileStream(destFile, FileMode.Create)) { // Compute the hash of the input file. byte[] hashValue = hmac.ComputeHash(inStream); // Reset inStream to the beginning of the file. inStream.Position = 0; // Write the computed hash value to the output file. outStream.Write(hashValue, 0, hashValue.Length); // Copy the contents of the sourceFile to the destFile. int bytesRead; // read 1K at a time byte[] buffer = new byte[1024]; do { // Read from the wrapping CryptoStream. bytesRead = inStream.Read(buffer, 0, 1024); outStream.Write(buffer, 0, bytesRead); } while (bytesRead > 0); } } } return; } // end SignFile
public void HMACRIPEMD160_b(string testName, HMACRIPEMD160 hmac, byte[] input, byte[] result) { byte[] output = hmac.ComputeHash(input, 0, input.Length); Assert.AreEqual(result, output, testName + ".b.1"); Assert.AreEqual(result, hmac.Hash, testName + ".b.2"); // required or next operation will still return old hash hmac.Initialize(); }
private string performHash(string secret, string message) { HMAC hmac = null; byte[] secretKey = System.Text.Encoding.ASCII.GetBytes(secret); if (string.Equals(_algo, Constants.SHA1, StringComparison.OrdinalIgnoreCase)) { hmac = new HMACSHA1 { Key = secretKey } } ; else if (string.Equals(_algo, Constants.SHA256, StringComparison.OrdinalIgnoreCase)) { hmac = new HMACSHA256 { Key = secretKey } } ; else if (string.Equals(_algo, Constants.SHA384, StringComparison.OrdinalIgnoreCase)) { hmac = new HMACSHA384 { Key = secretKey } } ; else if (string.Equals(_algo, Constants.SHA512, StringComparison.OrdinalIgnoreCase)) { hmac = new HMACSHA512 { Key = secretKey } } ; else if (string.Equals(_algo, Constants.MD5, StringComparison.OrdinalIgnoreCase)) { hmac = new HMACMD5 { Key = secretKey } } ; else if (string.Equals(_algo, Constants.RIPEMD160, StringComparison.OrdinalIgnoreCase)) { hmac = new HMACRIPEMD160 { Key = secretKey } } ; if (hmac == null) { throw new Exception($"Hash algo [{_algo}] not recognised"); } return(performHashGeneric(message, hmac)); }
public static string HMACAsRPEMD160(this string self, string key) { var keyByte = encoding.GetBytes(key); using (var hmacsha256 = new HMACRIPEMD160(keyByte)) { hmacsha256.ComputeHash(encoding.GetBytes(self)); return(hmacsha256.Hash.ConvertString().ToLower()); } }
public void HMACRIPEMD160_c(string testName, HMACRIPEMD160 hmac, byte[] input, byte[] result) { MemoryStream ms = new MemoryStream(input); byte[] output = hmac.ComputeHash(ms); Assert.AreEqual(result, output, testName + ".c.1"); Assert.AreEqual(result, hmac.Hash, testName + ".c.2"); // required or next operation will still return old hash hmac.Initialize(); }
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); }
public void Rfc2286_1() { var key = ByteExtensions.Repeat(0x0b, 20); var data = ByteExtensions.HexToByteArray("4869205468657265"); // "Hi There" var expectedHash = ByteExtensions.HexToByteArray("24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668"); var hmac = new HMACRIPEMD160(key); var actualHash = hmac.ComputeHash(data); Assert.Equal(expectedHash, actualHash); }
public void Rfc2286_2() { var key = ByteExtensions.HexToByteArray("4a656665"); // "Jefe"; var data = ByteExtensions.HexToByteArray("7768617420646f2079612077616e7420666f72206e6f7468696e673f"); // "what do ya want for nothing?" var expectedHash = ByteExtensions.HexToByteArray("dda6c0213a485a9e24f4742064a7f033b43c4069"); var hmac = new HMACRIPEMD160(key); var actualHash = hmac.ComputeHash(data); Assert.Equal(expectedHash, actualHash); }
public static string HmacRipeMd160(this string source, string keyVal) { if (source.IsEmpty()) { return(null); } Encoding encoding = Encoding.UTF8; HMACRIPEMD160 hashAlgorithmObj = new HMACRIPEMD160(encoding.GetBytes(keyVal)); return(HashAlgorithmBase(hashAlgorithmObj, source, encoding)); }
/// <summary> /// HmacRipeMd160 加密 /// </summary> public static string HmacRipeMd160(this string value, string keyVal) { if (value == null) { throw new ArgumentNullException("未将对象引用设置到对象的实例。"); } var encoding = Encoding.UTF8; byte[] keyStr = encoding.GetBytes(keyVal); HMACRIPEMD160 hmacRipeMd160 = new HMACRIPEMD160(keyStr); return HashAlgorithmBase(hmacRipeMd160, value, encoding); }
public static string HmacRipeMd160(this string value, string keyVal) { if (value == null) { throw new ArgumentNullException("不能对空字符串进行HmacRipeMd160加密"); } Encoding encoding = Encoding.UTF8; HMACRIPEMD160 hmacripemd160 = new HMACRIPEMD160(encoding.GetBytes(keyVal)); return(HashAlgorithmBase(hmacripemd160, value, encoding)); }
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 { } }
/// <summary>ハッシュ(キー付き)サービスプロバイダの生成</summary> /// <param name="ekha">ハッシュ(キー付き)サービスプロバイダの列挙型</param> /// <param name="key">キー</param> /// <returns>ハッシュ(キー付き)サービスプロバイダ</returns> private static KeyedHashAlgorithm CreateKeyedHashAlgorithmServiceProvider(EnumKeyedHashAlgorithm ekha, byte[] key) { // ハッシュ(キー付き)サービスプロバイダ KeyedHashAlgorithm kha = null; // HMACSHA1.Create(); だと、全部、HMACSHA1になってしまう現象があったので、 // 全部、= new HMACSHA1(key); のスタイルに変更した。 if (ekha == EnumKeyedHashAlgorithm.Default) { // 既定の暗号化サービスプロバイダ kha = new HMACSHA1(key); // devps(1703) } else if (ekha == EnumKeyedHashAlgorithm.HMACSHA1) { // HMACSHA1サービスプロバイダ kha = new HMACSHA1(key); // devps(1703) } // -- ▼追加▼ -- else if (ekha == EnumKeyedHashAlgorithm.HMACMD5) { // HMACMD5サービスプロバイダ kha = new HMACMD5(key); } else if (ekha == EnumKeyedHashAlgorithm.HMACRIPEMD160) { // HMACRIPEMD160サービスプロバイダ kha = new HMACRIPEMD160(key); } else if (ekha == EnumKeyedHashAlgorithm.HMACSHA256) { // HMACSHA256サービスプロバイダ kha = new HMACSHA256(key); } else if (ekha == EnumKeyedHashAlgorithm.HMACSHA384) { // HMACSHA384サービスプロバイダ kha = new HMACSHA384(key); } else if (ekha == EnumKeyedHashAlgorithm.HMACSHA512) { // HMACSHA512サービスプロバイダ kha = new HMACSHA512(key); } // -- ▲追加▲ -- else if (ekha == EnumKeyedHashAlgorithm.MACTripleDES) { // MACTripleDESサービスプロバイダ kha = new MACTripleDES(key); // devps(1703) } return(kha); }
public void HMACRIPEMD160_e(string testName, HMACRIPEMD160 hmac, byte[] input, byte[] result) { byte[] copy = new byte [input.Length]; for (int i = 0; i < input.Length - 1; i++) { hmac.TransformBlock(input, i, 1, copy, i); } hmac.TransformFinalBlock(input, input.Length - 1, 1); Assert.AreEqual(result, hmac.Hash, testName + ".e"); // required or next operation will still return old hash hmac.Initialize(); }
/// <summary> /// HmacRipeMd160 加密 /// </summary> public static string HmacRipeMd160(this string source, string keyVal) { if (source.IsEmpty()) { return(null); } var encoding = Encoding.UTF8; byte[] keyStr = encoding.GetBytes(keyVal); HMACRIPEMD160 hmacRipeMd160 = new HMACRIPEMD160(keyStr); return(HashAlgorithmBase(hmacRipeMd160, source, encoding)); }
public static String Encrypt(String message, String key) { String result; byte[] keyOfByte = Encoding.UTF8.GetBytes(key); using (HMACRIPEMD160 hmacMd5 = new HMACRIPEMD160(keyOfByte)) { byte[] data = hmacMd5.ComputeHash(Encoding.UTF8.GetBytes(message)); result = Convert.ToBase64String(data); } return(result); }
public void Invariants() { var hmac = new HMACRIPEMD160(); Assert.IsTrue(hmac.CanReuseTransform, "HMACRIPEMD160.CanReuseTransform"); Assert.IsTrue(hmac.CanTransformMultipleBlocks, "HMACRIPEMD160.CanTransformMultipleBlocks"); Assert.AreEqual("RIPEMD160", hmac.HashName, "HMACRIPEMD160.HashName"); Assert.AreEqual(160, hmac.HashSize, "HMACRIPEMD160.HashSize"); Assert.AreEqual(1, hmac.InputBlockSize, "HMACRIPEMD160.InputBlockSize"); Assert.AreEqual(1, hmac.OutputBlockSize, "HMACRIPEMD160.OutputBlockSize"); Assert.AreEqual(64, hmac.Key.Length, "HMACRIPEMD160.Key.Length"); Assert.AreEqual("System.Security.Cryptography.HMACRIPEMD160", hmac.ToString(), "HMACRIPEMD160.ToString()"); }
public string HMAC(string pInput, string pKey, HMACAlgorithms pAlgorithm, HashFormat pFormat) { UTF8Encoding utf8Encoding = new UTF8Encoding(); byte[] keyBytes = utf8Encoding.GetBytes(pKey); byte[] inputBytes = utf8Encoding.GetBytes(pInput); HMAC hmac = null; switch (pAlgorithm) { case HMACAlgorithms.HMACMD5: hmac = new HMACMD5(keyBytes); break; case HMACAlgorithms.HMACRIPEMD160: hmac = new HMACRIPEMD160(keyBytes); break; case HMACAlgorithms.HMACSHA1: hmac = new HMACSHA1(keyBytes); break; case HMACAlgorithms.HMACSHA256: hmac = new HMACSHA256(keyBytes); break; case HMACAlgorithms.HMACSHA384: hmac = new HMACSHA384(keyBytes); break; case HMACAlgorithms.HMACSHA512: hmac = new HMACSHA512(keyBytes); break; } byte[] hmacBytes = hmac.ComputeHash(inputBytes); string result = string.Empty; if (pFormat == HashFormat.Hex) { result = BitConverter.ToString(hmacBytes).Replace("-", string.Empty); } else if (pFormat == HashFormat.Base64) { result = Convert.ToBase64String(hmacBytes); } return(result); }
public static string Ripmed160(string Txt, bool Replace) { byte[] _Ripmed160; using (HashAlgorithm Ripmed160 = new HMACRIPEMD160(Key)) { _Ripmed160 = Ripmed160.ComputeHash(Encoding.ASCII.GetBytes(Txt)); } if (Replace == true) { return(BitConverter.ToString(_Ripmed160).Replace("-", String.Empty).ToLower()); } else { return(BitConverter.ToString(_Ripmed160)); } }
public static String Encrypt(FileInfo message, String key) { String result; byte[] keyOfByte = Encoding.UTF8.GetBytes(key); using (HMACRIPEMD160 hmacMd5 = new HMACRIPEMD160(keyOfByte)) { using (FileStream fs = new FileStream(message.FullName, FileMode.Open, FileAccess.Read, FileShare.None)) { byte[] data = hmacMd5.ComputeHash(fs); result = Convert.ToBase64String(data); } } return(result); }
private static void RunHMACRIPEMD160() { PrintTitle("MD5CryptoServiceProvider"); HMACRIPEMD160 crypto = new HMACRIPEMD160(); // Compute Hash byte[] textOneHash = crypto.ComputeHash(Encoding.UTF8.GetBytes(plainTextOne)); byte[] textTwoHash = crypto.ComputeHash(Encoding.UTF8.GetBytes(plainTextTwo)); string hexOfValueOne = BitConverter.ToString(textOneHash); string hexOfValueTwo = BitConverter.ToString(textTwoHash); // Out put is grouped into pairs, each represents a byte value between 0-255 Console.WriteLine($"String 1 Hash: {hexOfValueOne}"); Console.WriteLine($"String 2 Hash: {hexOfValueTwo}"); }
public void HMACSHA1Test() { string str = "Hello world!"; string encrypt = HMACSHA1.Encrypt(str, "123456"); Console.WriteLine(encrypt); encrypt = HMACSHA256.Encrypt(str, "123456"); Console.WriteLine(encrypt); encrypt = HMACSHA384.Encrypt(str, "123456"); Console.WriteLine(encrypt); encrypt = HMACSHA512.Encrypt(str, "123456"); Console.WriteLine(encrypt); encrypt = HMACMD5.Encrypt(str, "123456"); Console.WriteLine(encrypt); encrypt = HMACRIPEMD160.Encrypt(str, ""); Console.WriteLine(encrypt); }
public static bool TestRIPEMD160() { string password = "******"; byte[] passwordBytes = UTF8Encoding.UTF8.GetBytes(password); Pbkdf2 pbkdf2; HMACRIPEMD160 ripemd160 = new HMACRIPEMD160(); byte[] salt = new byte[] { 0x12, 0x34, 0x56, 0x78 }; pbkdf2 = new Pbkdf2(ripemd160, passwordBytes, salt, 5); byte[] result1 = pbkdf2.GetBytes(4); pbkdf2 = new Pbkdf2(ripemd160, passwordBytes, salt, 5); byte[] result2 = pbkdf2.GetBytes(48); byte[] hash1 = new byte[] { 0x7a, 0x3d, 0x7c, 0x03 }; byte[] hash2 = new byte[] { 0x7a, 0x3d, 0x7c, 0x03, 0xe7, 0x26, 0x6b, 0xf8, 0x3d, 0x78, 0xfb, 0x29, 0xd2, 0x64, 0x1f, 0x56, 0xea, 0xf0, 0xe5, 0xf5, 0xcc, 0xc4, 0x3a, 0x31, 0xa8, 0x84, 0x70, 0xbf, 0xbd, 0x6f, 0x8e, 0x78, 0x24, 0x5a, 0xc0, 0x0a, 0xf6, 0xfa, 0xf0, 0xf6, 0xe9, 0x00, 0x47, 0x5f, 0x73, 0xce, 0xe1, 0x43 }; return(ByteUtils.AreByteArraysEqual(result1, hash1) && ByteUtils.AreByteArraysEqual(result2, hash2)); }
public void Hash(byte[] temp) { using var HMACRIPEMD160 = new HMACRIPEMD160(); // Noncompliant using var HMACRIPEMD160Create = HMACMD5.Create(); // Noncompliant using var HMACRIPEMD160CreateWithParam = HMACMD5.Create("HMACRIPEMD160"); // Noncompliant using var HMACRIPEMD160KeyedHashAlgorithm = KeyedHashAlgorithm.Create("HMACRIPEMD160"); // Noncompliant using var HMACRIPEMD160KeyedHashAlgorithmWithNamespace = KeyedHashAlgorithm.Create("System.Security.Cryptography.HMACRIPEMD160"); // Noncompliant using var HMACRIPEMD160CryptoConfig = (HashAlgorithm)CryptoConfig.CreateFromName("HMACRIPEMD160"); // Noncompliant using var MD5Cng = new MD5Cng(); // Noncompliant using var RIPEMD160Managed = new RIPEMD160Managed(); // Noncompliant using var RIPEMD160Create = RIPEMD160.Create(); // Noncompliant using var RIPEMD160CreateWithParam = RIPEMD160.Create("RIPEMD160"); // Noncompliant using var RIPEMD160HashAlgorithm = HashAlgorithm.Create("RIPEMD160"); // Noncompliant using var RIPEMD160HashAlgorithmWithNamespace = HashAlgorithm.Create("System.Security.Cryptography.RIPEMD160"); // Noncompliant using var RIPEMD160CryptoConfig = (HashAlgorithm)CryptoConfig.CreateFromName("RIPEMD160"); // Noncompliant }
public static string RIPEMD160(string baseStr, string key) { var sourceBytes = utf8Enc.GetBytes(baseStr); var keyBytes = utf8Enc.GetBytes(key); var ripemd160 = new HMACRIPEMD160(keyBytes); var hashBytes = ripemd160.ComputeHash(sourceBytes); ripemd160.Clear(); var hashStr = string.Empty; foreach (var hashByte in hashBytes) { hashStr += string.Format("{0,0:x2}", hashByte); } return(hashStr); }
} // end SignFile // Compares the key in the source file with a new key created for the data portion of the file. If the keys // compare the data has not been tampered with. public static bool VerifyFile(byte[] key, String sourceFile) { bool err = false; // Initialize the keyed hash object. using (HMACRIPEMD160 hmac = new HMACRIPEMD160(key)) { // Create an array to hold the keyed hash value read from the file. byte[] storedHash = new byte[hmac.HashSize / 8]; // Create a FileStream for the source file. using (FileStream inStream = new FileStream(sourceFile, FileMode.Open)) { // Read in the storedHash. inStream.Read(storedHash, 0, storedHash.Length); // Compute the hash of the remaining contents of the file. // The stream is properly positioned at the beginning of the content, // immediately after the stored hash value. byte[] computedHash = hmac.ComputeHash(inStream); // compare the computed hash with the stored value for (int i = 0; i < storedHash.Length; i++) { if (computedHash[i] != storedHash[i]) { err = true; } } } } if (err) { Console.WriteLine("Hash values differ! Signed file has been tampered with!"); return false; } else { Console.WriteLine("Hash values agree -- no tampering occurred."); return true; } } //end VerifyFile