david-l
david-l

Reputation: 623

C# to Coldfusion for Base64String translation

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)#">

Upvotes: 1

Views: 605

Answers (2)

Leigh
Leigh

Reputation: 28873

The line is taken from Azure's creating authorisation header;

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.

<cfscript>
    savecontent variable="signatureString" {
        WriteOutput("GET#chr(10)##chr(10)##chr(10)##chr(10)#x-ms-date:Mon, 01 Dec 2008 05:17:57 GMT#chr(10)#/accountname/queuename/messages");
    };
    key = binaryDecode("abcd", "base64");
    resultInHex = hmac(signatureString, key,"HMACSHA256");
    // result: wxR7Bt6sWEKVJ9vEjCiuqA8OKCZOKYfbxaXj85whOkM=
    WriteDump(binaryEncode(binaryDecode(resultInHex, "hex"), "base64"));
</cfscript>



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.

<cfset result = hmacX( signatureString, "abcd", "HmacSHA256") />
<cfdump var="#result#" />

<cffunction name="hmacX" returntype="string" hint="">
    <cfargument name="message" type="string" required="true" />
    <cfargument name="keyInBase64" type="any" required="true" />
    <cfargument name="algorithm" type="string" default="HmacSHA256" />
    <cfargument name="encoding" type="string" default="UTF-8" />

    <cfset var dataBytes  = charsetDecode( arguments.message, arguments.encoding ) />
    <cfset var keyBytes  = binaryDecode( arguments.keyInBase64, "base64" ) />
    <cfset var keySpec = createObject("java", "javax.crypto.spec.SecretKeySpec") />
    <cfset var secret = keySpec.init( keyBytes, arguments.algorithm ) />
    <cfset var mac  = createObject("java", "javax.crypto.Mac").getInstance( arguments.algorithm ) />

    <cfset mac.init( secret ) />

    <cfreturn binaryEncode( mac.doFinal(dataBytes), "base64") />
</cffunction>

Upvotes: 1

mbeckish
mbeckish

Reputation: 10579

C# is treating the results as unsigned bytes, while ColdFusion is treating them as signed bytes.

The values under 128 are the same in both C# and ColdFusion, while the values that are over 128 in C# are negative (the two's complement) in ColdFusion.

So, to convert back, add 256 to any negative values.

EDIT

This might not be a problem with the actual bytes returned from BinaryDecode. It could just be a problem with how ColdFusion decides to print out those bytes to the screen during your debugging.

Compare the actual binary values you get from C# and ColdFusion (for example, by saving the values to a binary file rather than printing to the screen).

Upvotes: 1

Related Questions