public static string Encode(string input, byte[] key) { byte[] byteArray = Encoding.ASCII.GetBytes(input); using(var myhmacsha1 = new HMACSHA1(key)) { var hashArray = myhmacsha1.ComputeHash(byteArray); return hashArray.Aggregate("", (s, e) => s + String.Format("{0:x2}",e), s => s ); } }
HMACSHA1 myhmacsha1 = new HMACSHA1(key); byte[] byteArray = Encoding.ASCII.GetBytes(input); MemoryStream stream = new MemoryStream(byteArray); return myhmacsha1.ComputeHash(stream).Aggregate("", (s, e) => s + String.Format("{0:x2}",e), s => s );
@Test public void testDoFinalWithInput() { HMACSHA1 hmac = initHmac(); assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC)); }
@Override public MAC create() { return new HMACSHA1(); }
// This method will return the base 64 encoded string using the given input and key. private string EncodeHMAC(string input, byte[] key) { HMACSHA1 hmac = new HMACSHA1(key); byte[] stringBytes = Encoding.UTF8.GetBytes(input); byte[] hashedValue = hmac.ComputeHash(stringBytes); return Convert.ToBase64String(hashedValue); }
private BaseMAC initHmac() { BaseMAC hmac = new HMACSHA1(); hmac.init(KEY.getBytes(CHARSET)); return hmac; } }
private bool ValidateHash(String uid, String hash, DataToSign data) { StringBuilder strToSign = new StringBuilder(); strToSign.Append(data.HttpMethod + '\n'); strToSign.Append(data.Date.ToString("r") + '\n'); strToSign.Append(data.Uri); Byte[] secretBytes = UTF8Encoding.UTF8.GetBytes(this._secretKey); HMACSHA1 hmac = new HMACSHA1(secretBytes); Byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(strToSign.ToString()); Byte[] calcHash = hmac.ComputeHash(dataBytes); String calcHashString = Convert.ToBase64String(calcHash); if (calcHashString.Equals(hash)) { if (log.IsDebugEnabled) log.Debug(uid + " - [ValidateHash] HMAC is valid."); return true; } return false; }
@Test public void testResizeTooBigKeys() { BaseMAC hmac = new HMACSHA1(); hmac.init((KEY + "foo").getBytes(CHARSET)); hmac.update(PLAIN_TEXT); assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC)); }
public string Encode(string input, byte [] key) { HMACSHA1 myhmacsha1 = new HMACSHA1(key); byte[] byteArray = Encoding.ASCII.GetBytes( input ); MemoryStream stream = new MemoryStream( byteArray ); byte[] hashValue = myhmacsha1.ComputeHash(stream); return hashValue.ToString(); }
@Override public MAC create() { return new HMACSHA1(); }
@Override public MAC create() { return new HMACSHA1(); }