Create the same result as CryptoJS.enc.Base64 using PHP

I have a javascript function that I am trying to convert to PHP, it uses the CryptoJS library, speciafically components/enc-base64-min.jsand rollups/md5.js. They can be found here .

This is a bit of code

// Let say str = 'hello';

var md5 = CryptoJS.MD5(str);
md5 = md5.toString(CryptoJS.enc.Base64);
// md5 outputs "XUFAKrxLKna5cZ2REBfFkg=="

I assumed the variable is strhashed using md5, then encoded in Base64, so I tried this simple code

$md5 = md5($str);
$md5 = base64_encode($md5);
// md5 outputs "MmZjMGE0MzNiMjg4MDNlNWI5NzkwNzgyZTRkNzdmMjI="

Then I tried to check both outputs, it looks like JS output is not even a valid Base64 string.

To understand further, I tried to find the parameter toString()from W3Schools , but for me it does not make sense, according to the parameter reference should be an integer (2, 8 or 16), why is it used instead CryptoJS.enc.Base64?

, base64 JS, PHP.

+4
1

php md5() md5 .

, Base64, $raw_output md5() ( true)

$md5 = md5($str, true);

http://php.net/manual/it/function.md5.php

+4

All Articles