Re: Wrong SHA1 is calculated

[email protected] Tue, 16 May 2017 00:30:54 +1000
Newsgroups perl.win32.vanilla
Message-ID <C8ABCE1277004DF784A7223471785EFF@OwnerPC311012>
From: Martin Puppe
Sent: Monday, May 15, 2017 8:39 PM
To: [email protected]
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