2
$\begingroup$

I am having trouble converting this:

"333"

Into this:

"XjFVd0052XxfnhfBCMKz4EhaQ64069GW9hpvi/cy73GknlcQWUz8c5HbEU7fmfXaPtlu8dbKXlmOhfkb1B5+6w=="

It is for a web based program (I need to get a signature for authentication) and using online converters to check if what I am doing is right, I have managed to successfully convert "333" both into SHA512 and Base64 in Mathematica.

(* String to SHA512 *)
In[1]=  IntegerString[
          Hash["333", "SHA512"],
          16
        ]
Out[1]= 5e3155774d39d97c5f9e17c108c2b3e0485a43ae34ebd196f61a6f8bf732ef71a49e5710594cfc7391db114edf99f5da3ed96ef1d6ca5e598e85f91bd41e7eeb
(* verified correct using http://www.unit-conversion.info/texttools/sha/#data *)

(* String to Base64 *)
In[2]=  ExportString[
          "333",
          "Base64"
        ]
Out[2]= MzMz
(* verified correct using http://www.unit-conversion.info/texttools/base64/#data *)

But when I combine the two, I get this:

(* String to SHA512 Base64 *)
In[3]=  ExportString[
          IntegerString[Hash["333", "SHA512"], 16],
          "Base64"
        ]
Out[3]= NWUzMTU1Nzc0ZDM5ZDk3YzVmOWUxN2MxMDhjMmIzZTA0ODVhNDNhZTM0ZWJkMTk2ZjYxYTZmOGJmNzMyZWY3MWE0OWU1NzEwNTk0Y2ZjNzM5MWRiMTE0ZWRmOTlmNWRhM2VkOTZlZjFkNmNhNWU1OThlODVmOTFiZDQxZTdlZWI=

Instead of:

"XjFVd0052XxfnhfBCMKz4EhaQ64069GW9hpvi/cy73GknlcQWUz8c5HbEU7fmfXaPtlu8dbKXlmOhfkb1B5+6w=="

>_<.

Any ideas on how this could be done?

PS: I found a function for Mathematica 11.2 called "SecuredAuthenticationKey" that could be related to what I am trying to do. I am currently using Mathematica 9.

$\endgroup$
4
$\begingroup$
ExportString[
  FromCharacterCode[
    IntegerDigits[
      Hash["333", "SHA512"],
      256]
    ],
  "Base64"]
(* XjFVd0052XxfnhfBCMKz4EhaQ64069GW9hpvi/cy73GknlcQWUz8c5HbEU7fmfXaPtlu8dbKXlmOhfkb1B5+6w== *)

Your use of IntegerString converts it to base-16 first, but it looks like you want to convert it on a per-bit level, so divide it into bytes (with IntegerDigits), convert to string (FromCharacterCode), and apply ExportString there.

$\endgroup$
  • $\begingroup$ Thank you sir! :D $\endgroup$ – user12572 Dec 27 '17 at 21:43
1
$\begingroup$

This is much easier to do as of the recently released version 11.3.0:

BaseEncode[Hash["333", "SHA512", "ByteArray"]]

(* "XjFVd0052XxfnhfBCMKz4EhaQ64069GW9hpvi/cy73GknlcQWUz8c5HbEU7fmfXaPtlu8dbKXlmOhfkb1B5+6w==" *)
$\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.