|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-12-01 07:41 UTC] dietrich at pegestorf dot de
Description:
------------
I try to generate a sha1 hash with sha1()
$hash = sha1("test");
$hash is calculated wrong to: "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"
I have a file /tmp/data with the word "test" in it (no carriage return at the end)
$hash = sha1_file("/tmp/data");
$hash is calculated right to:
"4e1243bd22c66e76c2ba9eddc1f91394e57f9f83"
let's go tho the command line and enter:
echo test | openssl dgst -sha1
this generate also "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83"
same than
openssl dgst -sha1 /tmp/data
If I generate the hash over "test\n" with the sha1() the hash is calculated right to "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83"
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Tue Feb 10 18:00:02 2026 UTC |
There is no bug here at all. The echo test you do is flawed because echo always appends a \n, see with: echo -n test | openssl dgst -sha1 a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 that that is correct. Your file also has 5 characters most likely (using VIM?). With really only 'test' in it it works fine: derick@kossu:~$ ls -l /tmp/data -rw-r--r-- 1 derick derick 4 Dec 1 13:53 /tmp/data derick@kossu:~$ php-4.3dev -r 'echo sha1_file("/tmp/data");'; a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 and derick@kossu:~$ openssl dgst -sha1 /tmp/data SHA1(/tmp/data)= a94a8fe5ccb19ba61c4c0873d391e987982fbbd3