Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Can perl SHA1 module be applied to files?

Reply
Thread Tools

Can perl SHA1 module be applied to files?

 
 
Lee Hibberd
Guest
Posts: n/a
 
      10-04-2004
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
 
Reply With Quote
 
 
 
 
ko
Guest
Posts: n/a
 
      10-05-2004
Lee Hibberd wrote:
> 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
 
Reply With Quote
 
 
 
 
Lee Hibberd
Guest
Posts: n/a
 
      10-06-2004
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 <> wrote in message news:<>...
> Lee Hibberd wrote:
> > 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

 
Reply With Quote
 
ko
Guest
Posts: n/a
 
      10-07-2004
Lee Hibberd wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Digitally Signing a XML Document (using SHA1+RSA or SHA1+DSA) Adam Tauno Williams Python 2 12-30-2010 10:23 AM
Form sha1.hexdigest to sha1.digest LMZ Python 5 04-06-2008 09:50 PM
Book: Applied Perl - sourcecode required mydaj [ROR] Perl Misc 3 02-29-2008 08:16 PM
Applied Perl Source Code Perl Misc 0 01-12-2008 02:28 AM
How can I get an sha1 hash in base32? =?iso-8859-1?q?Elmo_M=E4ntynen?= Python 2 07-24-2005 12:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57