2

Possible Duplicate:
Why is an MD5 hash created by Python different from one created using echo and md5sum in the shell?

I just saw this, and I don't get it. Do I not know something about php's internal... something? Or are there assumptions I'm making about... something? That don't hold true?

$ sha512sum <(echo 'hello')
e7c22b994c59d9cf2b48e549b1e24666636045930d3da7c1acb299d1c3b7f931f94aae41edda2c2b207a36e10f8bcb8d45223e54878f5b316e7ce3b6bc019629  /proc/self/fd/11

and also:

$ echo 'hello' | sha512sum
e7c22b994c59d9cf2b48e549b1e24666636045930d3da7c1acb299d1c3b7f931f94aae41edda2c2b207a36e10f8bcb8d45223e54878f5b316e7ce3b6bc019629  -

but then:

php -a
Interactive shell
php > echo hash('sha512', 'hello');
9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043

∅ cat hashtest.php 
<?php
echo hash('sha512', 'hello');
?>
∅ php hashtest.php 
9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043

What's the deal? I'm on Linux x86_64, and my system text encoding is utf-8, and these are all ascii-range characters. I don't know what I'm overlooking.

Interestingly, for some value of "interesting", Python agrees with PHP:

>>> hashlib.sha512(b'hello').hexdigest()
'9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043'

Honestly, despite my humble opening, I assumed that this was some sort of massive and unknown PHP bug. But I guess not.

Community
  • 1
  • 1
quodlibetor
  • 8,185
  • 4
  • 35
  • 48
  • Indeed a duplicate, it's because of the trailing newline. – Leigh Feb 11 '12 at 00:32
  • Yeah, it is. It didn't show up because when I was searching I was looking for PHP problems, and I wasn't using the other specific terminology of that post. Mine is more general: that one is a dupe of *mine*. – quodlibetor Feb 11 '12 at 02:11

4 Answers4

5
$ php -a
Interactive shell

php > echo hash('sha512', "hello\n");
e7c22b994c59d9cf2b48e549b1e24666636045930d3da7c1acb299d1c3b7f931f94aae41edda2c2b207a36e10f8bcb8d45223e54878f5b316e7ce3b6bc019629

The standard echo command includes a trailing newline. Try echo -n for identical results.

deceze
  • 510,633
  • 85
  • 743
  • 889
2

echo also outputs a newline after the argument. Use the -n option to suppress the newline:

$echo -n 'hello' | sha512sum
9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043  -
phihag
  • 278,196
  • 72
  • 453
  • 469
0

Everything is very simple. echo adds a newline at the end.

$ php -r 'echo hash(sha512, "hello\n");'
e7c22b994c59d9cf2b48e549b1e24666636045930d3da7c1acb299d1c3b7f931f94aae41edda2c2b207a36e10f8bcb8d45223e54878f5b316e7ce3b6bc019629
a sad dude
  • 2,775
  • 17
  • 20
0

Echo adds a newline to its argument, so you're actually hashing the string "hello\n". When I try this string in either python or php with your sample code above, I get the e7c22... hash. Or

$ echo -n 'hello' | sha512sum 
9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043  -
Jan P.
  • 1