title: Converting a cryptojs MD5 script into PHP
body: <p>I've got a piece of javascript which someone has written to convert an ID into a particular hashed ID used by a program.</p>
  
  <p>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.</p>
  
  <p>My aim is to convert this into PHP, so I can run the conversion in some back-end scripts.</p>
  
  <p>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</p>
  
  <p>What they have (javascript)</p>
  
  <pre><code>var id = bigInt(12345678912345678);
  var parts = [0x42,0x45,0,0,0,0,0,0,0,0];
  
  for (var i = 2; i &lt; 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));
  </code></pre>
  
  <p>What I've got in PHP</p>
  
  <pre><code>$id = 12345678912345678;
  $parts = [0x42,0x45,0,0,0,0,0,0,0,0];
  
  for ($i=2; $i &lt; 10; $i++){
      $quo = (int)($id / 256);
      $rem = $id % 256;
      $id = $quo;
      $parts[$i] = $rem;
  }
  
  $hash = md5($parts);
  echo $hash;
  </code></pre>
  
  <p>This of course, returns an error due to MD5 not taking an array.</p>
  
  <p>I've tried something simple such as join()ing the array, but that doesn't return the same hash.</p>
  
  <p>I've had a look into the cryptoJS library, and I've done tons of google searches, and looked through SO (inc: <a href="https://stackoverflow.com/questions/17283581/generate-tha-same-md5-using-javascript-and-php">Generate the same MD5 using javascript and PHP</a> , <a href="https://stackoverflow.com/questions/23149282/sha512-hashes-differ-on-android-php-and-javascript">SHA512 hashes differ on android, php and javascript</a> etc) but can't seem to find anything which answers this question.</p>
  
  <p>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.</p>
  
  <p>Some output...</p>
  
  <pre><code>ID used: 12345678912345678
  
  using join('',$parts)
  PHP: d378d1609e6a2854b129b012aa6363ea
  JS: 61bb919806ee7c4ad6f781b49a89dfcf
  
  using json_encode($parts)
  PHP: 3e2393c173558194b6ba1bbedc9314e1
  JS: 61bb919806ee7c4ad6f781b49a89dfcf
  </code></pre>