This is my C# code.
private static string CEncrypt()
{
const string key = "SECRET KEY";
const string message = "SECRET MESSAGE";
var encoding = new UTF8Encoding();
byte[] keyByte = encoding.GetBytes(key);
var hmacsha1 = new HMACSHA1(keyByte);
byte[] messageBytes = encoding.GetBytes(message);
byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);
return BitConverter.ToString(hashmessage).Replace("-","").ToLower();
}
This is my Apex Code.
@RestResource(urlMapping='/Encrypt/*')
global with sharing class EncryptTest
{
@HttpGet
global static String GetTest()
{
string privateKey = 'SECRET KEY';
Blob cryptoKey = EncodingUtil.base64Decode(privateKey);
String message = 'SECRET MESSAGE';
Blob urlBlob = EncodingUtil.base64Decode(message);
Blob encryptedData = Crypto.generateMac('hmacSHA1', urlBlob, cryptoKey);
return EncodingUtil.convertToHex(encryptedData);
}
}
When I compare the output of the two methods, I am expecting to see the exact same hash. The values from the two methods are: **
Salesforce Hash: "92599d3f792af33a7335c802bb42593f1c59dc2d"
CSharp Hash 08e21bc5d10b096727c5b91ce32c5384082126c8
**