As I said in the title I need to reproduce this PHP function (in JavaScript):
hash("sha512", "string", true);
The tricky part if for the true at the end which means:
When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits.
This can be important too if I need to create something by myself and if I should take care of the endian:
The tiger algorithm now uses big-endian byte ordering.
Reference: php.net - hash()
EDIT: After some tests and research here is the function I need to reproduce in JavaScript:
hex2bin("string");
Actually witch CryptoJS I'm able to obtain the result in hexa. I don't know exactly how to obtain it in row binary data (hex2bin equivalent).
I found that with NodeJS and the library crypto we can do that easily but I don't use NodeJS. I need to obtain the same result in classic JavaScript (or jQuery or any other lib which will work).
My goal is to send to a Symfony's WSSE protocol a password that will match the one in database (passwordDigest).
Here is the PHP code I need to reproduce:
public function encodePassword($raw, $salt)
{
// ... Some check code for data
$salted = $this->mergePasswordAndSalt($raw, $salt); // Just do $raw."{".$salt."}";
$digest = hash($this->algorithm, $salted, true);
// "stretch" hash
for ($i = 1; $i < $this->iterations; $i++) {
$digest = hash($this->algorithm, $digest.$salted, true);
}
return $this->encodeHashAsBase64 ? base64_encode($digest) :
bin2hex($digest);
}
And here is the code in JavaScript I actually have:
WSSE.encodePassword = function(password, salt) {
try {
var salted = WSSE.mergePasswordAndSalt(password, salt);
var digest = CryptoJS.SHA512(salted).toString();
for (i = 1; i < 5000; i++) {
digest = CryptoJS.SHA512(digest + salted).toString();
}
return WSSE.base64_encode(digest);
} catch(err) {
Log.e("WSSE", err.message);
return null;
}
};
The problem with this code is that the digest variable always contains data in hexa, not in raw binary data.
An idea, code, or lib everyone?
digestto be binary? Javascript treats all number-systems as base10 numbers, for example: console.log(0xff) -> 255, check the question I linked