Trying to convert C# code to CF and im stuck translating the following line
StorageKey = 'abcd';
Convert.FromBase64String(StorageKey)
The above line produces a byte array of 105,183,29
The line is taken from Azure’s creating authorisation header;
System.Security.Cryptography.HMACSHA256 SHA256 = new System.Security.Cryptography.HMACSHA256(Convert.FromBase64String(StorageKey));
I’ve looked up FromBase64String Method on msdn library but its over my head. I hope someone can point me in the right Coldfusion direction.
I’ve tried BinaryDecode(StorageKey,”BASE64″), to me that seemed the most logical translation but im getting back 105-7329 which is not my expected result.
See Leigh’s answer below for CF10+, if you are after a CF7-9 solution this my attempt
var javaMsg = javacast("string", arguments.sigMsg).getBytes("UTF-8");
var javaKey = JavaCast("string", arguments.sigKey);
var myKey = createObject('java', 'javax.crypto.spec.SecretKeySpec' );
var mac = createObject('java', "javax.crypto.Mac");
var myKeyB64 = CreateObject("java", "org.apache.commons.codec.binary.Base64").decodeBase64(javaKey.getBytes());
var secret = myKey.Init(myKeyB64, 'HmacSHA256');
mac = mac.getInstance("HmacSHA256");
mac.init(secret);
</cfscript>
<cfdump var="#mac.doFinal(javaMsg)#">
mbeckish is correct about the sign differences between C# and CF/java. However, I do not think the internal representation should make any difference to your end result, which is usually all you care about. Normally you do not need to match the low level integer values, just the string representation of the bytes in base64 or hex. As long as you compare the final HMAC values in a common format, like base64, they should be exactly the same.
For example, if you take the sample signature string here, both CF10 and C# return the same value. So it seems likely your problem is something else.
EDIT: I know there are a bunch of pre-CF10 hmac functions floating around. But many of them forget about encoding. Here is a generic adaptation that accepts an algorithm (“hmacsha256”, “hmacsha1”, ..) and encoding (“utf-8”). It should work with CF7-9.