Skip to main content

binary - HMAC implementation in pure Mathematica


I am trying to implement the HMAC algorithm in Mathematica. The algorithm is a relatively simple way of signing messages with a key.


Here is some pseudocode from Wikipedia:


enter image description here


My attempt:


hash[string_String, method_String : "SHA256"] := FromCharacterCode @ 

Interpreter["HexInteger"] @ StringPartition[IntegerString[Hash[string, method], 16], 2];

HMAC[key_String, message_String, method_String : "SHA256", blockSize_Integer : 64] :=
Module[
{char54, char92, key2, ipad, opad},
{char54, char92} = FromCharacterCode /@ {54, 92};
key2 = Switch[StringLength @ key,
blockSize, key,
l_ /; l > blockSize, hash[key, method],
_, StringPadRight[key, blockSize, FromCharacterCode @ 0]];

ipad = FromCharacterCode[BitXor @@ Map[ToCharacterCode, {StringRepeat[char54, blockSize], key2}]];
opad = FromCharacterCode[BitXor @@ Map[ToCharacterCode, {StringRepeat[char92, blockSize], key2}]];
hash @ StringJoin[opad, hash @ StringJoin[ipad, message]]];

A Java analog for testing:


Needs["JLink`"];
LoadJavaClass["javax.crypto.Mac"];

javaHMAC[key_, message_] := Module[
{mac, keySpec, bytes},

JavaBlock[
mac = Mac`getInstance["HmacSHA256"];
keySpec = JavaNew["javax.crypto.spec.SecretKeySpec", ToCharacterCode[key], "HmacSHA256"];
mac @ init[keySpec];
bytes = mac @ doFinal[ToCharacterCode[message]] + 256;
FromCharacterCode @ bytes]];

In my particular case I am using this to communicate with Amazon Web Services. At the bottom of this page they provide a test vector which involves repeated HMAC computations. For these inputs (using outputs as the key for the next input)


key = "AWS4" <> "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY";
messages = {"20120215", "us-east-1", "iam", "aws4_request"};


we should obtain


"kSecret  = '41575334774a616c725855746e46454d492f4b374d44454e472b62507852666943594558414d504c454b4559'
kDate = '969fbb94feb542b71ede6f87fe4d5fa29c789342b0f407474670f0c2489e0a0d'
kRegion = '69daa0209cd9c5ff5c8ced464a696fd4252e981430b10e3d3fd8e2f197d7a70c'
kService = 'f72cfd46f26bc4643f06a11eabb6c0ba18780c19a8da0c31ace671265e3c87fa'
kSigning = 'f4780e2d9f65fa895f9c67b32ce1baf0b0d8a43505a000a1a9e090d414db404d'";
StringSplit[StringSplit[%, "\n"], "= "][[All, 2]];
correct = StringReplace[%, "'" -> ""];


My function passes this test. Swapping it out for the Java method will show that the same results are obtained.


FoldList[HMAC, key, messages];
StringJoin /@ IntegerString[ToCharacterCode @ %, 16, 2];
MapThread[SequenceAlignment, {%, correct}];
% // Column


{41575334774a616c725855746e46454d492f4b374d44454e472b62507852666943594558414d504c454b4559}


{969fbb94feb542b71ede6f87fe4d5fa29c789342b0f407474670f0c2489e0a0d}


{69daa0209cd9c5ff5c8ced464a696fd4252e981430b10e3d3fd8e2f197d7a70c}



{f72cfd46f26bc4643f06a11eabb6c0ba18780c19a8da0c31ace671265e3c87fa}


{f4780e2d9f65fa895f9c67b32ce1baf0b0d8a43505a000a1a9e090d414db404d}



But on a slightly different set of messages, from the bottom of this page, my function fails on one of the steps.


messages2 = {"20110909", "us-east-1", "iam", "aws4_request"};

FoldList[HMAC, key, messages2];
hex2 = StringJoin /@ IntegerString[ToCharacterCode @ %, 16, 2];

FoldList[javaHMAC, key, messages2];

javaHex2 = StringJoin /@ IntegerString[ToCharacterCode @ %, 16, 2];

MapThread[SequenceAlignment, {hex2, javaHex2}] // Column


{41575334774a616c725855746e46454d492f4b374d44454e472b62507852666943594558414d504c454b4559}


{a83ed188be5f4b074d7f66349f5077fbcdf797bf3471fb9d5f32730f936d41a5}


{957b5875f33834a85374b750011dc2d6f0e1d6896eeb891d36a73c711961ad6e}


{{,0},116249d060bff83faa1a627e85a4c6f83ce50d89c334765878dcf76e28bfc6,{,e}}


{{a4b,98f1},d,{,889f},e,{1,c},4,{b2f,f44},21,{2,adc},5,{8145,22b},a,{,b0ce1},f,{6,},8,{1,2e6},9,{d7,29},c,{4e4,262},e,{17a88,d},1,{31,5e},5a9,{f85,4c9},0,{33,ef},d,{b,},1,{d858,},e,{6,},3,{f52,b0e},7,{51,}}




In this case the Java output matches the answer provided by AWS.


I cannot get this implementation to work either.



Answer



Try something like


hash[string_String, method_String: "SHA256"] :=
FromCharacterCode@IntegerDigits[Hash[string, method], 256, 32]

The original method you used will drop leading zeros. You can even see that in your example data set:


{{,0},116249d060bff83faa1a627e85a4c6f83ce50d89c334765878dcf76e28bfc6,{,e}}


ETA: I should have mentioned that passing in the algorithm but then hard-coding the length of the expected hash to 32 bytes is extremely sloppy of me. Do not do that in production code.


Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...