We are calling a service from salesforce. This service is expecting a specific hash in the httpHeaders collection of the HTTP Request.
The service is written in .NET and the hash is computed using SHA384 in C#. Here is the sample C# code.
SHA384 shaM = new SHA384Managed();
byte[] bye = Encoding.ASCII.GetBytes("IAMSTUMPED");
byte[] buffer = shaM.ComputeHash(bye);
StringBuilder sb = new StringBuilder();
for (int i = 0; buffer != null && i < buffer.Length; i++)
{
sb.AppendFormat("{0:x2}", buffer[i]);
}
Console.WriteLine(sb.ToString());
Output of this is:
199978467e452a6a937f90936e058dd9556412c0479fc3471d27f29135ecbcec534f8b29cd8ef6a38de9250237f603e8
How can I get Apex to produce the same output as the .NET code? I don't want to hard-code the hash since the key in the example can be changed multiple times.
I am going through the documentation on Crypto class and the method that looks most promising is generateDigest. This does not seem to support SHA384.
Here is my apex code.
Blob urlBlob = Blob.valueOf('IAMSTUMPED');
Blob encryptedData = Crypto.generateDigest('MD5', urlBlob);
string token = EncodingUtil.base64Encode(encryptedData);
System.Debug(token);
//Outputs: CKl2baWkEngT4+gXasfpeA==
encryptedData = Crypto.generateDigest('SHA1', urlBlob);
token = EncodingUtil.base64Encode(encryptedData);
System.Debug(token);
//Outputs: Ak/2rdtJWyVe2/lXgoBnojmSGqM=
encryptedData = Crypto.generateDigest('SHA-256', urlBlob);
token = EncodingUtil.base64Encode(encryptedData);
System.Debug(token);
//Outputs: 4Oq4exMWa9kNcNVj3Gu4Ce3+9/rUVE5yrqRpc7NZCdw=
encryptedData = Crypto.generateDigest('SHA-512', urlBlob);
token = EncodingUtil.base64Encode(encryptedData);
System.Debug(token);
//Ouputs: 290Z3HYWsWtEqqtIgMvr2UxilQmJVnwAQKbeOxAZ6ousKC9ZbvoTbzOXfzA71SRYTNLQXos9xJT2ZnSst7i3hw==
I have tried using every single algorithm that is listed in the documentation. How can I generate the same hash that the C# code is generating?
SHA-384
itself? Just because it is not documented does not mean it's not worth testing.IAMNOWSTUMPED
yet in the apex code you are using a different valueIAMSTUMPED
.