I have a java code example to make a digest computed using the HMAC-SHA1 algorithm (RFC 2104.), then encoded using Base64 encoding (RFC 2045).
here is the java code
public static String buildDigest(String key, String idString) throws SignatureException {
try {
String algorithm = "HmacSHA1";
Charset charset = Charset.forName("utf-8");
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), algorithm);
Mac mac = Mac.getInstance(algorithm);
mac.init(signingKey);
return new String(Base64.encodeBase64(mac.doFinal(idString.getBytes(charset))), charset);
} catch (Exception e) {
throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
}
}
I found answers here in Stack Overflow so here is the C# code
private string EncodeHMAC(string input, byte[] key)
{
HMACSHA1 myhmacsha1 = new HMACSHA1(key);
byte[] byteArray = Encoding.UTF8.GetBytes(input);
// MemoryStream stream = new MemoryStream(byteArray);
var hashValue = myhmacsha1.ComputeHash(byteArray);
return hashValue.Aggregate("", (s, e) => s + String.Format("{0:x2}", e), s => s);
}
private string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes = System.Text.UTF8Encoding.UTF8.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
I am not getting the right results shown in the tutorial that I am following
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
Try this:
// 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);
}
I don't think you're converting the hashed value to a base 64 string correctly.
I use this function to implement authentication of REST web service calls. It's important that sender and receiver use the same encoding.
Unfortunately it took me a while to find a matching PHP HAMACimplementation to this C# version.
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;
}
Hope that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With