Forum Moderators: coopster

Message Too Old, No Replies

PHP SHA1 function got different results than any others

PHP SHA1 difference

         

vr2zuf

3:52 am on Aug 5, 2010 (gmt 0)

10+ Year Member



Hi all experts here,

Why does the SHA1 function of PHP generates a different results from any other tools or languages that I tried ?

For example, php -r 'echo sha1 ("12345");' gives
8cb2237d0679ca88db6464eac60da96345513964

while : echo 12345 | sha1sum
or : echo 12345 | openssl sha1

all gives
2672275fe0c456fb671e4f417fb2f9892c7573ba

Regards,
vr2zuf.

coopster

2:54 pm on Aug 5, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, vr2zuf.

Try this from the command line instead ...

sha1sum --string=12345


Or if your version of sha1sum doesn't support the
--string
switch you still echo, you just need to remove newlines from the echo command.

Some *nix implementations of echo have a
-n
switch that tells the command "do not output the trailing newline" which is currently part of the piped input to your sha1sum command causing your unexpected digest output.
man echo
from your command line to see more information on using the echo command line utility on unix.

Otherwise, and for more cross-*nix variation support, use printf:
printf 12345 | sha1sum

vr2zuf

5:37 pm on Aug 5, 2010 (gmt 0)

10+ Year Member



Bingo !

I worked like a charm. The command on Linux should read :
echo -n 12345 | sha1sum or
echo -n 12345 | openssl sha1

Thanks,
vr2zuf