tags:

views:

44

answers:

2

I run this script:

define('SECRET', "vJs;ly-W\XDkD_2'-M7S2/ZRRBobxt5");
echo sha1(SECRET . 'zcbkeyky' . '[email protected]') . "\n";

Locally with PHP 5.3.2 (cli) it gives me: 3baa47e50394cd2dce236dcbf2f409fdb9010f2a
On a remote machine with PHP 5.1.6 (cli) it gives: d1bcf4ea83e50593d3df19a8455a5f5cd32d63ef

Why does the same calculation differ?

+2  A: 

Only thing I can think of is the encoding of the files/strings on each server?

My result is 3baa47e50394cd2dce236dcbf2f409fdb9010f2a (Locally)

Lizard
I think your theory about encoding is spot on - but it's nothing to do with files (OP is hashing a string, not a file). I'd check php.ini and/or phpinfo() for more information.
Chris
@Chris: I assume he meant the encoding of the PHP source file.
R. Bemrose
yes that what I meant
Lizard
Ah, that would make sense.
Chris
+9  A: 

I'd say the problem is here:

define('SECRET', "vJs;ly-W\XDkD_2'-M7S2/ZRRBobxt5");
//                        ^^-- escape character

PHP manual says:

\x[0-9A-Fa-f]{1,2} the sequence of characters matching the regular expression is a character in hexadecimal notation

Between PHP 5.2 and 5.3, apparently this was modified to also match \X[0-9A-Fa-f]{1,2} (note the capital X at the beginning). When running in PHP 5.3, you have (unknowingly?) a Carriage Return in your string.

Either a) replace the backslash with another character, or b) use single quotes when defining SECRET, and both versions will return the same hash (tried on 5.2.1 and 5.3.2).

Piskvor
+1: Good catch. I missed that when looking at the code. However, this does illustrate why one should use single quotes on string literals.
R. Bemrose
@R. Bemrose: We had some major un-fun with double quotes and a stubborn developer some time ago; taught me to treat `"` with utmost suspicion. This was the process: Why is SECRET defined in double quotes and the rest is single-quoted? "If the string is enclosed in double-quotes, PHP will interpret more escape sequences for special characters", do we have any of that? Wait a minute, what's that backslash?
Piskvor
mr eagleeye! thanks
baloo
@R. Bemrose: The reason was because it contained a singlequote, but I will escape that next time ;)
baloo