Wrong SHA1 is calculated

2017-05-15 Thread Martin Puppe
Hello,

I am debugging a problem with SHA1 checksums. I have found the following handy 
one-liner on Stack Overflow [^1], which serves well as a minimal example:

```
perl -MDigest::SHA1=sha1_hex -le "print sha1_hex <>" secure.txt
```

The problem is, that the result is simply not correct. Doing the same on a Unix 
system gives the correct result. And I also get a correct result when I 
generate the SHA1 sum with the 7-zip file manager.

Any ideas what the problem might be?

[^1]: http://stackoverflow.com/a/2812212

Best regards,
Martin Puppe
JAM Software






JAM Software GmbH
Managing Director: Joachim Marder
Am Wissenschaftspark 26 * 54296 Trier * Germany
Phone: +49 (0)651-145 653 -0 * Fax: +49 (0)651-145 653 -29
Commercial register number HRB 4920 (AG Wittlich) http://www.jam-software.com






JAM Software GmbH
Gesch?ftsf?hrer: Joachim Marder
Am Wissenschaftspark 26 * 54296 Trier * Germany
Tel: 0651-145 653 -0 * Fax: 0651-145 653 -29
Handelsregister Nr. HRB 4920 (AG Wittlich) http://www.jam-software.de


Re: Wrong SHA1 is calculated

2017-05-15 Thread sisyphus1


From: Martin Puppe
Sent: Monday, May 15, 2017 8:39 PM
To: win32-vanilla@perl.org
Subject: Wrong SHA1 is calculated


Hello,

I am debugging a problem with SHA1 checksums. I have found the following 
handy one-liner on Stack Overflow [^1], which serves well as a minimal 
example:


perl -MDigest::SHA1=sha1_hex -le "print sha1_hex <>" secure.txt

The problem is, that the result is simply not correct.Martin Puppe


The same web page presents a program which should provide the same "not 
correct" result.
I've inserted "binmode $fh;" into it - which then allows it to return the 
"correct" value:


#
use warnings;
use strict;

use Digest::SHA1;

die "Usage: $0 file ..\n" unless @ARGV;

foreach my $file (@ARGV) {
 my $fh;
 unless (open $fh, $file) {
   warn "$0: open $file: $!";
   next;
 }

 binmode $fh; # inserted by sisyphus
 my $sha1 = Digest::SHA1->new;
 $sha1->addfile($fh);
 print $sha1->hexdigest, "  $file\n";

 close $fh;
}
###

If 'secure.txt' is a plain text file with Unix line endings, then that 
binmode() should not be necessary - but if the text file has Windows line 
endings then binmode() will prevent their translation to Unix endings (and 
the program will return the result that you expect).


Unfortunately, I don't know how to get that binmode() into the one-liner's 
angle brackets :-(


Maybe someone else here can chime in.

Cheers,
Rob 


Re: Wrong SHA1 is calculated

2017-05-15 Thread sisyphus1
-Original Message- 
From: Christian Millour

Sent: Tuesday, May 16, 2017 9:14 AM
To: win32-vanilla@perl.org
Subject: Re: Wrong SHA1 is calculated


Le 15/05/2017 à 16:30, sisyph...@optusnet.com.au a écrit :
Unfortunately, I don't know how to get that binmode() into the 
one-liner's angle brackets :-(


$ PERLIO=unix perl -MDigest::SHA1=sha1_hex -le "print sha1_hex <>" 
secure.txt

19576d392b021ac25efdca6f1886b5ce5b1090c4
$


Yes, I think that should give the OP the result he was seeking.
Nice.

Cheers,
Rob 


Re: Wrong SHA1 is calculated

2017-05-15 Thread Christian Millour

Le 15/05/2017 à 16:30, sisyph...@optusnet.com.au a écrit :

Unfortunately, I don't know how to get that binmode() into the
one-liner's angle brackets :-(


you might consider playing with the PERLIO environment variable :

$ perl -E "print qq{hello\nworld\n}" > secure.txt
$ od -c secure.txt
000   h   e   l   l   o  \r  \n   w   o   r   l   d  \r  \n
016
$ perl -MDigest::SHA1=sha1_hex -le "print sha1_hex <>" secure.txt
58853e8a5e8272b1012f9a52a80758b27bd0d3cb
$ PERLIO=unix perl -MDigest::SHA1=sha1_hex -le "print sha1_hex <>" 
secure.txt

19576d392b021ac25efdca6f1886b5ce5b1090c4
$

--Christian