public static byte[] ToHmacRIPEMD160(this byte[] s, byte[] key) { using (var hmac = new HMACRIPEMD160(key)) { return(hmac.ComputeHash(s)); } }
/// <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 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
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 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(); }
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 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 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 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 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 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); }
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 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
} //compute hash from arguments and return hash value as string private static string GetHMACRIPEMD160Hash(string text) { //create variables for computing hash and making it a string UnicodeEncoding UniCode = new UnicodeEncoding(); byte[] HashResult; byte[] msg = UniCode.GetBytes(text); HMACRIPEMD160 hashString = new HMACRIPEMD160(); string Str = ""; //compute hash with HMACRIPEMD160 module and format output as string //convert bytes in HashResult to string values HashResult = hashString.ComputeHash(msg); foreach (byte x in HashResult) { Str += String.Format("{0:x2}", x); } //clear excess resource usage hashString.Clear(); return(Str); } //compute hash from arguments and return hash value as string
/// <summary> /// /// </summary> /// <param name="message"></param> /// <param name="secret"></param> /// <returns></returns> public static string GenerateHMACRIPEMD160(string message, string secret) { 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)) { var hashMessage = hmacripemd160.ComputeHash(messageBytes); string hashString = Convert.ToBase64String(hashMessage); return(hashString); } } catch (Exception ex) { Debug.WriteLine("GenerateHMACRIPEMD160 : " + ex.Message); throw; } }
public override byte[] ByteHashAlgorithm(string value, string key) { HMACRIPEMD160 sha = new HMACRIPEMD160((System.Security.Cryptography.SHA1.Create()).ComputeHash(ASCIIEncoding.ASCII.GetBytes(key))); return(sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value))); }
/// <summary> /// HMACRIPEMD160 加密 /// </summary> public static byte[] HmacRipeMd160(byte[] source, byte[] key) { HMACRIPEMD160 provider = new HMACRIPEMD160(key); return(provider.ComputeHash(source)); }
/// <summary> /// Computes the HMAC-RIPEMD-160 for the key and bytes. /// </summary> public static Byte[] ComputeHMAC(Byte[] key, Byte[] bytes) { HMACRIPEMD160 hmac = new HMACRIPEMD160(key); return(hmac.ComputeHash(bytes)); }
public static string HashData(string data, string secret, eCryptographyType cryptography) { string returnValue = string.Empty; secret = secret ?? ""; var encoding = new System.Text.UTF8Encoding(); byte[] keyByte = encoding.GetBytes(secret); byte[] messageBytes = encoding.GetBytes(data); switch (cryptography) { case eCryptographyType.HMACRIPEMD160: { using (HMACRIPEMD160 hashfunction = new HMACRIPEMD160(keyByte)) { byte[] hashmessage = hashfunction.ComputeHash(messageBytes); returnValue = Convert.ToBase64String(hashmessage); } break; } case eCryptographyType.HMACMD5: { using (HMACMD5 hashfunction = new HMACMD5(keyByte)) { byte[] hashmessage = hashfunction.ComputeHash(messageBytes); returnValue = Convert.ToBase64String(hashmessage); } break; } case eCryptographyType.HMACSHA1: { using (HMACSHA1 hashfunction = new HMACSHA1(keyByte)) { byte[] hashmessage = hashfunction.ComputeHash(messageBytes); returnValue = Convert.ToBase64String(hashmessage); } break; } case eCryptographyType.HMACSHA256: { using (HMACSHA256 hashfunction = new HMACSHA256(keyByte)) { byte[] hashmessage = hashfunction.ComputeHash(messageBytes); returnValue = Convert.ToBase64String(hashmessage); } break; } case eCryptographyType.HMACSHA384: { using (HMACSHA384 hashfunction = new HMACSHA384(keyByte)) { byte[] hashmessage = hashfunction.ComputeHash(messageBytes); returnValue = Convert.ToBase64String(hashmessage); } break; } } return(returnValue); }
/// <summary> /// Computes a keyed hash for a source file based on key provided and the HMAC algorithm selected by the user /// Defaults to HMACSHA1 /// </summary> /// <param name="file">file to get the HMAC of</param> /// <param name="hmacAlgo">HMAC algorithm to use</param> /// <param name="hmacKey">Key supplied by the user</param> public string CalculateHMAC(string file, string hmacAlgo, byte[] hmacKey) { string resultHmac = ""; byte[] hashOfInputFile; try { switch (hmacAlgo) { case "HMACMD5": using (HMACMD5 hmacSha1 = new HMACMD5(hmacKey)) { using (FileStream objFS = new FileStream(file, FileMode.Open)) { // Computing the hash of the input file hashOfInputFile = hmacSha1.ComputeHash(objFS); } } break; case "HMACSHA256": using (HMACSHA256 hmacSha1 = new HMACSHA256(hmacKey)) { using (FileStream objFS = new FileStream(file, FileMode.Open)) { // Computing the hash of the input file hashOfInputFile = hmacSha1.ComputeHash(objFS); } } break; case "HMACSHA384": using (HMACSHA384 hmacSha1 = new HMACSHA384(hmacKey)) { using (FileStream objFS = new FileStream(file, FileMode.Open)) { // Computing the hash of the input file hashOfInputFile = hmacSha1.ComputeHash(objFS); } } break; case "HMACSHA512": using (HMACSHA512 hmacSha1 = new HMACSHA512(hmacKey)) { using (FileStream objFS = new FileStream(file, FileMode.Open)) { // Computing the hash of the input file hashOfInputFile = hmacSha1.ComputeHash(objFS); } } break; case "HMACRIPEMD160": using (HMACRIPEMD160 hmacSha1 = new HMACRIPEMD160(hmacKey)) { using (FileStream objFS = new FileStream(file, FileMode.Open)) { // Computing the hash of the input file hashOfInputFile = hmacSha1.ComputeHash(objFS); } } break; default: // "HMACSHA1": using (HMACSHA1 hmacSha1 = new HMACSHA1(hmacKey)) { using (FileStream objFS = new FileStream(file, FileMode.Open)) { // Computing the hash of the input file hashOfInputFile = hmacSha1.ComputeHash(objFS); } } break; } resultHmac = BitConverter.ToString(hashOfInputFile).Replace("-", String.Empty).ToLower(); return(resultHmac); } catch { resultHmac = HMAC_ERROR; return(resultHmac); } finally { } }