1

I’d like to use a set of REST API through JavaScript and I’m reading the documentation explaining how to implement authentication. The following instructions are illustrated in pseudocode but I have some issue on understanding how to implement it in JavaScript (my JS level is quite basic). This is the unclear part:

= FromBytesToBase64String(MD5Hash("{\n    \"data\": {\n        \"type\": \"company\",\n        \"id\": \"879f2dfc-57ea-4dbb-96c7-c546f8812f1e\",\n        \"external_1_value\": \"Updated value\"\n    }\n}"))

Basically I should calculate MD5 hash of the string in question and then I should encode it in base 64 string If I understood well.

The documentation shows the flow broken in sub-steps:

= FromBytesToBase64String(b'eC\xcda\xa3\xa7\xaf\xa53\x93\xb4.\xa2\xb1\xe3]')

And then the final result:

"ZUPNYaOnr6Uzk7QuorHjXQ=="

I tried to do the same by using crypto.js library and I get a MD5 hash string but then how can I get this value "ZUPNYaOnr6Uzk7QuorHjXQ==" ? Any idea on how I could do it?

Thanks so much for helping!

andG
  • 57
  • 4

4 Answers4

0

That final result is the base64 encoded string. The function FromBytesToBase64String is what produces it, but this is not a standard function in JavaScript.

Instead, try using one of the built-in functions documented here. Specifically:

window.btoa(MD5Hash("Your input string"));
MTCoster
  • 5,868
  • 3
  • 28
  • 49
  • Hey thanks a lot for your answer, I tried as you said only thing the string I get is this one: NjU0M2NkNjFhM2E3YWZhNTMzOTNiNDJlYTJiMWUzNWQ= which doesn't correspond to the final value I should get according to API documentation which should be ZUPNYaOnr6Uzk7QuorHjXQ== any idea why? – andG Jul 29 '16 at 13:20
0

window.btoa(MD5Hash("Your input string")); does not work because btoa takes the md5 string and converts that character by character, hence you need to feed it an byte array.I ended up combining ArrayBuffer to base64 encoded string with https://github.com/pvorb/node-md5/issues/25

into :

function md5ToBase64(md5String,boolTrimLast){
    var strRet = arrayBufferToBase64(hexByteStringToByteArray(md5String));
    return boolTrimLast?strRet.slice(0,22):strRet;
}
Dexterial
  • 19
  • 3
-1

use the btoa() function to get a base64 encoded string.

barosus
  • 54
  • 3
-1

Use WindowBase64.btoa():

var encodedData = window.btoa(md5Hash);