Can perl SHA1 module be applied to files?

L

Lee Hibberd

Hello all

I have used sha1_file in php and want to know if there is an
equivalent in perl. i.e. I want to produce a sha1 value based on the
composition of the file and not the filename. I have used the perl
sha1 digest, but I'm unsure if this is working correctly. One reason
for my uncertainty is that the sha1 values produced by php sha1_file
differ. The second reason is I guess I would need to feed the
bit-stream through the sha1 in perl as if it were a line of text, and
I'm not sure that my code does that:

my $fn="$file";
my $fh = new IO::File;
$fh->open($fn) or @row = $dbh->do( "
UPDATE All_Locations SET SHA1Date = Date(),SHA1Time =
Time(),ErrorCode=\'NoFile\' WHERE FileID=$row[0]
" );
$digest = new Digest::SHA1;
$digest->addfile($fh);
$fh->close;
$sha1var = $digest->hexdigest;
print $file.": ".$sha1var."\n";

Any help would be greatly appreciated.

Lee Hibberd
National Library of Scotland
 
K

ko

Lee said:
Hello all

I have used sha1_file in php and want to know if there is an
equivalent in perl. i.e. I want to produce a sha1 value based on the
composition of the file and not the filename. I have used the perl
sha1 digest, but I'm unsure if this is working correctly. One reason
for my uncertainty is that the sha1 values produced by php sha1_file
differ. The second reason is I guess I would need to feed the
bit-stream through the sha1 in perl as if it were a line of text, and
I'm not sure that my code does that:

my $fn="$file";
my $fh = new IO::File;
$fh->open($fn) or @row = $dbh->do( "
UPDATE All_Locations SET SHA1Date = Date(),SHA1Time =
Time(),ErrorCode=\'NoFile\' WHERE FileID=$row[0]
" );
$digest = new Digest::SHA1;
$digest->addfile($fh);
$fh->close;
$sha1var = $digest->hexdigest;
print $file.": ".$sha1var."\n";

Any help would be greatly appreciated.

Lee Hibberd
National Library of Scotland

The documentation for addfile() states that the filehandle should be in
binmode:

use Digest::SHA1;

open my $fh, shift or die $!;
binmode($fh);

print Digest::SHA1->new->addfile($fh)->hexdigest, "\n";
close $fh;

Digest::MD5 has an interface similar to Digest::SHA1 and example code
you might want to look at.

HTH - keith
 
L

Lee Hibberd

Thanks for your reply Keith. I am now seeing the same value generated
for three different .jpg files I am working with so I guess something
is still wrong with my code. Could you give me a final nudge in the
right direction? Here's the modified code:

#!/usr/local/bin/perl
use Digest::SHA1;

my $datafile = "c:\testerforanette.jpg";
my $file_is_binary = ! -T $datafile;

open ((MYNEWFILE, $datafile) || die ("Can't find file"));
if ($file_is_binary) {
binmode(MYNEWFILE);
print Digest::SHA1->new->addfile(*MYNEWFILE)->hexdigest, "\n";
close (MYNEWFILE);
}

I've noticed if I replace addfile(*MYNEWFILE) with addfile(MYNEWFILE).
I also get the same result. Thanks - Lee

ko said:
Lee said:
Hello all

I have used sha1_file in php and want to know if there is an
equivalent in perl. i.e. I want to produce a sha1 value based on the
composition of the file and not the filename. I have used the perl
sha1 digest, but I'm unsure if this is working correctly. One reason
for my uncertainty is that the sha1 values produced by php sha1_file
differ. The second reason is I guess I would need to feed the
bit-stream through the sha1 in perl as if it were a line of text, and
I'm not sure that my code does that:

my $fn="$file";
my $fh = new IO::File;
$fh->open($fn) or @row = $dbh->do( "
UPDATE All_Locations SET SHA1Date = Date(),SHA1Time =
Time(),ErrorCode=\'NoFile\' WHERE FileID=$row[0]
" );
$digest = new Digest::SHA1;
$digest->addfile($fh);
$fh->close;
$sha1var = $digest->hexdigest;
print $file.": ".$sha1var."\n";

Any help would be greatly appreciated.

Lee Hibberd
National Library of Scotland

The documentation for addfile() states that the filehandle should be in
binmode:

use Digest::SHA1;

open my $fh, shift or die $!;
binmode($fh);

print Digest::SHA1->new->addfile($fh)->hexdigest, "\n";
close $fh;

Digest::MD5 has an interface similar to Digest::SHA1 and example code
you might want to look at.

HTH - keith
 
K

ko

Lee said:
Thanks for your reply Keith. I am now seeing the same value generated
for three different .jpg files I am working with so I guess something
is still wrong with my code. Could you give me a final nudge in the
right direction? Here's the modified code:

#!/usr/local/bin/perl

use strict;
use warnings;
use Digest::SHA1;

my $datafile = "c:\testerforanette.jpg";
my $file_is_binary = ! -T $datafile;

open ((MYNEWFILE, $datafile) || die ("Can't find file"));

Is this *really* the code you're using? Enable strict and warnings and
see what happens. Even without strict and warnings, you get a diagnostic
message informing you that no filehandle is being passed to addfile().
if ($file_is_binary) {
binmode(MYNEWFILE);
print Digest::SHA1->new->addfile(*MYNEWFILE)->hexdigest, "\n";
close (MYNEWFILE);
}

I've noticed if I replace addfile(*MYNEWFILE) with addfile(MYNEWFILE).
I also get the same result. Thanks - Lee

A lexical filehandle as posted previously (need Perl 5.6.0 or
higher) allows you to pass the filehandle directly to addfile() without
the typeglob. For details:

perldoc perlopentut, the 'Indirect Filehandles' section
perldoc -q "How do I pass filehandles between subroutines?"

Or, if you're running an older version, use IO::File to get the lexical
filehandle:

use IO::File;
my $datafile = 'YOUR_JPG';
my $fh = new IO::File $datafile;

if (defined $fh) {
binmode($fh);
print Digest::SHA1->new->addfile($fh)->hexdigest, " $datafile\n";
$fh->close;
}

Can't make any other suggestions, since the code as posted doesn't read
any files.

keith
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top