Can perl SHA1 module be applied to files?

Discussion in 'Perl Misc' started by Lee Hibberd, Oct 4, 2004.

  1. Lee Hibberd

    Lee Hibberd Guest

    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
     
    Lee Hibberd, Oct 4, 2004
    #1
    1. Advertising

  2. Lee Hibberd

    ko Guest

    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
     
    ko, Oct 5, 2004
    #2
    1. Advertising

  3. Lee Hibberd

    Lee Hibberd Guest

    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
     
    Lee Hibberd, Oct 6, 2004
    #3
  4. Lee Hibberd

    ko Guest

    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
     
    ko, Oct 7, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?q?Elmo_M=E4ntynen?=

    How can I get an sha1 hash in base32?

    =?iso-8859-1?q?Elmo_M=E4ntynen?=, Jul 23, 2005, in forum: Python
    Replies:
    2
    Views:
    1,006
    =?iso-8859-1?q?Elmo_M=E4ntynen?=
    Jul 24, 2005
  2. sha1,256,512 on files

    , Nov 30, 2005, in forum: Python
    Replies:
    1
    Views:
    338
    Paul Rubin
    Nov 30, 2005
  3. LMZ
    Replies:
    5
    Views:
    557
    Martin v. Löwis
    Apr 6, 2008
  4. Adam Tauno Williams
    Replies:
    2
    Views:
    887
    Stefan Behnel
    Dec 30, 2010
  5. Guest

    Applied Perl Source Code

    Guest, Jan 12, 2008, in forum: Perl Misc
    Replies:
    0
    Views:
    117
    Guest
    Jan 12, 2008
Loading...

Share This Page