2
$\begingroup$

I want to sign a string with HMAC-SHA1 algorithm.As the instruction I should use this two string:

encodedPutPolicy = "eyJzY29wZSI6Im15LWJ1Y2tldDpzdW5mbG93ZXIuanBnIiwiZGVhZGxpbmUiOjE0NTE0OTEyMDAsInJldHVybkJvZHkiOiJ7XCJuYW1lXCI6JChmbmFtZSksXCJzaXplXCI6JChmc2l6ZSksXCJ3XCI6JChpbWFnZUluZm8ud2lkdGgpLFwiaFwiOiQoaW1hZ2VJbmZvLmhlaWdodCksXCJoYXNoXCI6JChldGFnKX0ifQ==";
SecretKey ="MY_SECRET_KEY";

To get such signed string

signedString = "c10e287f2b1e7f547b20a9ebce2aada26ab20ef2"

I don't know this is which programming language in that instructional web-page:

sign = hmac_sha1(encodedPutPolicy, SecretKey )

From this relevant post,I get a such function for my case:

hash[string_String, method_String : "SHA"] := 
  FromCharacterCode@
   Interpreter["HexInteger"]@
    StringPartition[IntegerString[Hash[string, method], 16], 2];

HMAC[key_String, message_String, method_String: "SHA", 
   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]]];

But it will give messy code like following:

HMAC[SecretKey, encodedPutPolicy]

"Á.0e(+.1eT{ ©ëÎ*­¢j.b2.0eò"


And as the pseudocode provided by WIKI,

I give following try:

hmac[key_, message_] := 
 Module[{keyLen = StringLength[key], dkey, opad, ipad, blocksize = 64,
    method = "SHA"}, 
  dkey = If[keyLen > blocksize, IntegerString[Hash[key, method], 16], 
    StringPadRight[key, blocksize]]; {opad, ipad} = 
   FromCharacterCode[
      BitXor[ToCharacterCode@
        StringRepeat[FromCharacterCode[FromDigits[#, 16]], 
         blocksize], ToCharacterCode@dkey]] & /@ {"5c", "36"}; 
  IntegerString[Hash[StringJoin[opad, ipad, message], method], 16]]

But as you see,the following rusult different with my expected and the exmaple of Wiki.

hmac[SecretKey, encodedPutPolicy]
hmac["key", "The quick brown fox jumps over the lazy dog"]
hmac["", ""]

"4e44cbeea3fcfea721bcbdd7f40853e62fc34a7c"

"a13add58c73437e1c876ffd0e6f1acc80f143f7e"

"629d1b940615d4072d48027a898cee7942a889b0"

Anybody can give some help?

$\endgroup$
2
$\begingroup$

The default padding string of StringPadRight is a space, but what we need here is 0x00.

And the last line of the pseudocode is return hash(o_key_pad ∥ hash(i_key_pad ∥ message)). There are two hashs.

hmac[key_, message_] := 
 Module[{keyLen = StringLength[key], dkey, opad, ipad, blocksize = 64,
    method = "SHA"}, 
  dkey = If[keyLen > blocksize, IntegerString[Hash[key, method], 16], 
    StringPadRight[key, blocksize, FromCharacterCode[0]]]; {opad, 
    ipad} = FromCharacterCode[
      BitXor[ToCharacterCode@
        StringRepeat[FromCharacterCode[FromDigits[#, 16]], blocksize],
        ToCharacterCode@dkey]] & /@ {"5c", "36"};
  IntegerString[
   Hash[StringJoin[opad, 
     ExportString[
      IntegerDigits[Hash[StringJoin[ipad, message], method], 256], 
      "Binary"]], method], 16]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.