I've got a piece of javascript which someone has written to convert an ID into a particular hashed ID used by a program.
Note that the javascript output cannot be changed. It replicates the hashing done by the above mentioned 3rd-party program. I am trying to likewise replicate the hashing in PHP.
My aim is to convert this into PHP, so I can run the conversion in some back-end scripts.
However, it uses the cryptoJS library, and I can't find a way to convert it to PHP properly, as the MD5 in cryptoJS seems to have an array passed to it, and the PHP MD5 is passed a string. See below for some code examples
What they have (javascript)
var id = bigInt(12345678912345678);
var parts = [0x42,0x45,0,0,0,0,0,0,0,0];
for (var i = 2; i < 10; i++) {
var res = id.divmod(256);
id = res.quotient;
parts[i] = res.remainder.toJSNumber();
}
var wordArray = CryptoJS.lib.WordArray.create(new Uint8Array(parts));
$("#newid").val(CryptoJS.MD5(wordArray));
What I've got in PHP
$id = 12345678912345678;
$parts = [0x42,0x45,0,0,0,0,0,0,0,0];
for ($i=2; $i < 10; $i++){
$quo = (int)($id / 256);
$rem = $id % 256;
$id = $quo;
$parts[$i] = $rem;
}
$hash = md5($parts);
echo $hash;
This of course, returns an error due to MD5 not taking an array.
I've tried something simple such as join()ing the array, but that doesn't return the same hash.
I've had a look into the cryptoJS library, and I've done tons of google searches, and looked through SO (inc: Generate the same MD5 using javascript and PHP , SHA512 hashes differ on android, php and javascript etc) but can't seem to find anything which answers this question.
I have a feeling that the issue is me not understanding exactly what the wordArray is passing into the MD5 (and thus not being able to reproduce properly in PHP)... but I'm not familiar enough with the cryptoJS library to be sure.
Some output...
ID used: 12345678912345678
using join('',$parts)
PHP: d378d1609e6a2854b129b012aa6363ea
JS: 61bb919806ee7c4ad6f781b49a89dfcf
using json_encode($parts)
PHP: 3e2393c173558194b6ba1bbedc9314e1
JS: 61bb919806ee7c4ad6f781b49a89dfcf