4
$\begingroup$

Hi I'been trying to rebuild the script to get hmac-sha512 but could not get it work

hmac512[key_, message_] :=
Module[
{keyLen = StringLength[key], dkey, opad, ipad, blocksize = 64, 
   method = "SHA512"}, 
  dkey = If[keyLen > blocksize, IntegerString[Hash[key, method], 16], 
    StringJoin[key, 
     Array[FromCharacterCode[0] &, blocksize - StringLength[key]]]];
  {opad, ipad} = 
   FromCharacterCode[
      BitXor[ToCharacterCode@
        StringJoin[
         ConstantArray[FromCharacterCode[FromDigits[#, 16]], 
          blocksize]], ToCharacterCode@dkey]] & /@ {"5c", "36"};
  IntegerString[
   Hash[StringJoin[opad, 
     ExportString[
      IntegerDigits[Hash[StringJoin[ipad, message], method], 256], 
      "Binary"]], method], 16]
];

I've tried to overcome the issue and get the relevant result from the shell, but surprisingly, execution of

echo -n sometext | openssl dgst -sha512 -hmac qwerty

in the shell and scripts in Mathematica 10.0

RunProcess[$SystemShell, "StandardOutput", "
 echo -n sometext | openssl dgst -sha512 -hmac qwerty
 exit
  "]

and

Import["!echo -n sometext |openssl dgst -sha512 -hmac qwerty", "Text"]

gives different result from that from the shell

Thanks in advance!

$\endgroup$
4
  • 1
    $\begingroup$ What happens if you do Import["!/bin/echo -n sometext |openssl dgst -sha512 -hmac qwerty", "Text"]? From the echo(1) man page, "Some shells may provide a builtin echo command which is similar or identical to this utility. Most notably, the builtin echo in sh(1) does not accept the -n option." $\endgroup$
    – ilian
    Jan 6, 2017 at 17:08
  • $\begingroup$ Oh, and for the Mathematica code, change blocksize to be 128. $\endgroup$
    – ilian
    Jan 6, 2017 at 18:11
  • $\begingroup$ the Import["!/bin/echo -n sometext |openssl dgst -sha512 -hmac qwerty", "Text"] worked fine. I knew that i missed something very simple. Thanks a lot! $\endgroup$ Jan 6, 2017 at 19:39
  • $\begingroup$ But the blocksize to be 128 did not helped $\endgroup$ Jan 6, 2017 at 19:40

1 Answer 1

5
$\begingroup$

For the Mathematica implementation, setting the blocksize to be 128 (as appropriate for SHA512) gives the following

hmac512[key_, message_] := 
  Module[{keyLen = StringLength[key], dkey, opad, ipad, 
    blocksize = 128, method = "SHA512"}, 
   dkey = If[keyLen > blocksize, IntegerString[Hash[key, method], 16],
      StringJoin[key, 
      Array[FromCharacterCode[0] &, blocksize - StringLength[key]]]];
   {opad, ipad} = 
    FromCharacterCode[
       BitXor[ToCharacterCode@
         StringJoin[
          ConstantArray[FromCharacterCode[FromDigits[#, 16]], 
           blocksize]], ToCharacterCode@dkey]] & /@ {"5c", "36"};
   IntegerString[
    Hash[StringJoin[opad, 
      ExportString[
       IntegerDigits[Hash[StringJoin[ipad, message], method], 256], 
       "Binary"]], method], 16]];

hmac512["qwerty", "sometext"]

(* "a7c77de3134ba45ad435e16bfc0fabd411a2ed6d02aacdcbc760c1c49e2ce\
a4f91d8486746d0e750b21d8007c81cb96190c32b732157a6e257d7916ae5ee6fc2" *)

which matches with the openssl result

Import["!/bin/echo -n sometext | openssl dgst -sha512 -hmac qwerty", "Text"]

(* "a7c77de3134ba45ad435e16bfc0fabd411a2ed6d02aacdcbc760c1c49e2ce\
a4f91d8486746d0e750b21d8007c81cb96190c32b732157a6e257d7916ae5ee6fc2" *)

The discrepancy between Import and the shell mentioned in the question is due to the shell builtin echo not supporting the -n option, i.e.

Import["!echo -n sometext | openssl dgst -sha512 -hmac qwerty", "Text"]

(* "5ef0a6c12954a93e5f2fdc897222af57ff243675d357f9dfdf3eb1f93f34f\
a6f54914aca71864f7269c30980899ce7d35e23fceb03fa845cf11adc8d8913b780" *)

matches the output of the following shell command

echo "-n sometext" | openssl dgst -sha512 -hmac qwerty

which also matches

hmac512["qwerty", "-n sometext\n"]

(* "5ef0a6c12954a93e5f2fdc897222af57ff243675d357f9dfdf3eb1f93f34\
fa6f54914aca71864f7269c30980899ce7d35e23fceb03fa845cf11adc8d8913b780" *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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