PHP Password Hash Algorithm Result different from Javascript Result


I i'm trying to convert a password hashing algorithm from PHP to Javascript for use with Node.JS. My results are different though for each version of the code. I think i've converted it correctly but maybe i've missed something.

Tested Password is: Cake99

  • PHP result:

    2392893c9280d10e82fcef37be0e94df

  • JavaScript result:

    73a75e6c999d773460748e269ec324c4

PHP version

function pCrypt($password)
{
    $md5password = 'superSecretMD5Pass';
    $s = md5($password) . md5($md5password);
    $j = 0;
    for ($i = 0; $i < strlen($s); $i++)
    {
        if ($j >= strlen($md5password)) $j = 0;
        $s[$i] = $s[$i] ^ $md5password[$j];
        $j++;
    }
    return md5($s);
}

JavaScript version

function pCrypt(password) {
    var md5password = 'superSecretMD5Pass';
    var s = md5(password) + md5(md5password);
    var j = 0;
    for (i = 0; i < s.length; i++) {
        if (j >= md5password.length) {
            j = 0;
        }
        s[i] = s[i] ^ md5password[j];
        j++;
    }
    return md5(s);
}

http://codahale.com/how-to-safely-store-a-password/

function pCrypt(password) {
    var md5password = 'superSecretMD5Pass';
    var s = md5(password) + md5(md5password);
    var r = '';
    var j = 0;
    for (i = 0; i < s.length; i++) {
        if (j >= md5password.length) {
            j = 0;
        }
        r += String.fromCharCode(s.charCodeAt(i) ^ md5password.charCodeAt(j));
        j++;
    }
    return md5(r);
}

JavaScript does not support bitwise operations using strings:

> "a"^"c"
0

The PHP loop is iterating over bytes, while the JavaScript version is iterating over UTF-16/UCS-2 code-units which are each 2 bytes wide.