View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-25-2011, 12:46 PM   #1
LQ Newbie
Registered: Apr 2010
Posts: 21

Rep: Reputation: 3
Perl SHA256SUM Clarification

I am writing a perl script to take the SHA256SUM of every image file in a directory. The problem is that the SHA256SUM created by perl is different than the one created by the straight sha256sum utility from the command line.

The perl script returns hashes much quicker than the command line, leading me to believe the image files are not actually being used, but some other value (like their names for instance).

I am using "strict" and "warnings", and no errors are being returned.

PHP Code:
use Digest::SHA;

# Code Omitted

opendir(my $dh$STARTING_DIRECTORY) or die "Cannot open directory: $!";
while (
readdir $dh)
# Strip out the "." and the ".."
unless ($_ eq '.' or $_ eq '..')
# Wrap up the filename for convenience
        # It's equal to the absolute path to the file
my $absolute "$STARTING_DIRECTORY$_";
# Calculate SHA256SUM
my $digest Digest::SHA->new(256);
my $hash $digest->hexdigest();

# Code Omitted 
Because these are image files, I do not need them to be read line-by-line, only in one piece, like the command line utility does it. How can I accomplish this?
Old 02-26-2011, 02:55 AM   #2
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,366

Rep: Reputation: 192Reputation: 192
if you want to slurp a whole file as one string:

local $/ = undef;

my $slurp = <>;

print "[$slurp]";
now your hash should work

or better use Digest::File (I ain't tried it)

also, you can shortcut and use a glob pattern rather than readdir

@list = <*.img *.jpg>

it's easier to use find2perl to traverse directories.
perl programming is about laziness!

Last edited by bigearsbilly; 02-26-2011 at 03:03 AM.
Old 02-28-2011, 08:33 PM   #3
LQ Newbie
Registered: Apr 2010
Posts: 21

Original Poster
Rep: Reputation: 3
It ended up being a simple fix.

All that had to be done was open the file and switch to a binary reading mode.

PHP Code:
open(FILE"$STARTING_DIRECTORY$_") or die "ERROR. $_ could not be opened: $!";
# This is critical to prevent ulcers and create correct SHA256 checksums

# Compute the SHA256 of the file
my $digest Digest::SHA->new(256);
my $hash $digest->hexdigest(); 



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
I need some clarification about MBR ddaas Linux - General 1 11-02-2008 12:49 PM
need some clarification/help superhumanCA Linux - General 4 03-18-2008 12:19 PM
I just need some clarification Duneatreides Linux - Newbie 2 03-12-2007 05:48 AM
Clarification on Nvidia Burgin Debian 1 02-26-2005 11:12 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

All times are GMT -5. The time now is 12:30 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration