Search code examples
javascriptmd5cryptojs

Cannot replicate md5 encoded string with CryptoJs


I want to play with a Rest API. This requires the creation of an X-Auth key for authentication.

The documentation said:

The X-Auth-Key header should be constructed using the following algorithm: md5(api_key + md5(url + X-Auth-User + api_key + X-Auth-Expires)).

For example, consider a GET request to https://<server_ip>/api/live_events/1?clean=true by the user 'admin' with the api_key '1acpJN7oEDn3BDDYhQ' that expires on June 1, 2011 UTC. In this case the url parameter is '/live_events/1' and the X-Auth-Expires value is '1306886400'. Thus the value of X-Auth-Key should be computed as follows:

md5('1acpJN7oEDn3BDDYhQ' + md5('/live_events/1'+'admin'+'1acpJN7oEDn3BDDYhQ'+'1306886400')) => md5('1acpJN7oEDn3BDDYhQ' + md5('/live_events/1admin1acpJN7oEDn3BDDYhQ1306886400')) => '180c88df8d0d4182385f6eb7e7045a42'

I have tried to implement this with , but unfortunately I can't get the values of the example:

<script>
var md5xx =  CryptoJS.MD5('/live_events/1admin1acpJN7oEDn3BDDYhQ1306886400')
var md5yy = CryptoJS.MD5(('1acpJN7oEDn3BDDYhQ') + String(md5xx));
console.log(md5yy.toString());
// => 17222238c238b7ac9f76ea8d0fe1e330
</script>

Solution

  • The md5 hash you obtained 17222238c238b7ac9f76ea8d0fe1e330 is correct.
    The given 180c88df8d0d4182385f6eb7e7045a42 example with reverse lookup gives a different link /jobs/1admin1acpJN7oEDn3BDDYhQ1306886400 instead of /live_events/1admin1acpJN7oEDn3BDDYhQ1306886400.
    You can cross check with
    https://md5.gromweb.com/?md5=180c88df8d0d4182385f6eb7e7045a42 and
    https://md5.gromweb.com/?md5=a39ee4e3aa79939249cb6b5e7faead28

    //the hash you actually expected
    var md5xx =  CryptoJS.MD5('/jobs/1admin1acpJN7oEDn3BDDYhQ1306886400')
    var md5yy = CryptoJS.MD5(('1acpJN7oEDn3BDDYhQ') + String(md5xx));
    console.log(md5yy.toString());
    
    //correct hash according to the given link
    var md5xx =  CryptoJS.MD5('/live_events/1admin1acpJN7oEDn3BDDYhQ1306886400')
    var md5yy = CryptoJS.MD5(('1acpJN7oEDn3BDDYhQ') + String(md5xx));
    console.log(md5yy.toString());
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>