D
D
dllweb2016-10-26 22:12:17
PHP
dllweb, 2016-10-26 22:12:17

Why does the js hash function counterpart in php give different hashes?

Good time friends, I brought you some horror from js that I wanted to translate into php . The
question is, why are the hashes different? cite the code, first js function then its analogue in php
function on JAVASCRIPT
pastebin.com/RL21GShg
function on the PHP
pastebin.com/mZsYhHap
On js hash: c4cf282458ea415fc58cd9a2a98d9b284a1a494e99fce5e96bc117b924361e00
in PHP hash: 0000000000000000000000000000000000000000000000000000000000000000
Why is it so? I don't understand what went wrong!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey, 2016-10-26
@alsopub

Confuses this moment:
JS:

if (s[0] = v, s[16] = s[1] = s[2] = s[3] = s[4] = s[5] = s[6] = s[7] = s[8] = s[9] = s[10] = s[11] = s[12] = s[13] = s[14] = s[15] = 0) { ... }

PHP:
if ($s[0] = $v and $s[16] = $s[1] = $s[2] = $s[3] = $s[4] = $s[5] = $s[6] = $s[7] = $s[8] = $s[9] = $s[10] = $s[11] = $s[12] = $s[13] = $s[14] = $s[15] = 0) { ... }

It seems that "comma" in JS and "and" in PHP will work differently.
Try
if ($s[0] = $v and $s[16] = $s[1] = $s[2] = $s[3] = $s[4] = $s[5] = $s[6] = $s[7] = $s[8] = $s[9] = $s[10] = $s[11] = $s[12] = $s[13] = $s[14] = $s[15] = 0) {

Replaced by
$s[0] = $v;
if ($s[16] = $s[1] = $s[2] = $s[3] = $s[4] = $s[5] = $s[6] = $s[7] = $s[8] = $s[9] = $s[10] = $s[11] = $s[12] = $s[13] = $s[14] = $s[15] = 0) {

PS. Below in the comments I showed a line from a program with bit operations, which gives different results on js and php, which is an additional problem that introduces an error.

E
Elena Stepanova, 2016-10-26
@Insolita

phpunit + xdebug to help
check the charCodeAt function first

U
Uno, 2016-10-27
@Noizefan

Am I the only one confused by the assignment operator in the conditions instead of the == operator, or am I misunderstanding something?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question