php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26485 sha1 generates a wrong hash
Submitted: 2003-12-01 07:41 UTC Modified: 2003-12-01 08:47 UTC
From: dietrich at pegestorf dot de Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.3.4 OS: linux
Private report: No CVE-ID: None
 [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"  


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-12-01 07:50 UTC] derick@php.net
I'll have a look at fixing this...
 [2003-12-01 07:55 UTC] derick@php.net
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

 [2003-12-01 08:46 UTC] dietrich at pegestorf dot de
ok,

my vim sucks :(

ls -al /tmp/data
-rw-r--r--    1 root     root            5 Dec  1 14:48 /tmp/data

sorry
 [2003-12-01 08:47 UTC] derick@php.net
Not really, you just don't know how to use it ;-)
It's still a bogus report though...
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jun 24 04:01:33 2025 UTC