Perl giving unexpected md5 hash values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl giving unexpected md5 hash values
# 1  
Old 07-15-2014
Perl giving unexpected md5 hash values

I am trying to speed up creating a line by line hash file from a huge file using Perl.

Here is my current (working but too slow) Bash code:
Code:
(while read line; do hash=$(echo -n $line | md5sum); echo ${hash:0:32}; done)

And here is my Perl code:
Code:
perl -MDigest::MD5 -le 'foreach $line ( <STDIN> ) {print  Digest::MD5->md5_hex($line)}'

Problem is the hashes don't match. What am I missing?

Code:
$ echo "xyz" | perl -MDigest::MD5 -le 'foreach $line ( <STDIN> ) {print Digest::MD5->md5_hex($line)}'
d81e30bc9592abd836c2a1fe2eda5489

$ echo -n "xyz" | perl -MDigest::MD5 -le 'foreach $line ( <STDIN> ) {print Digest::MD5->md5_hex($line)}'
71602ba5e39db459d95ab7c6666bb9e3

$ echo "xyz" | md5sum
b6273b589df2dfdbd8fe35b1011e3183 *-

$ echo -n "xyz" | md5sum
d16fb36f0911f878998c136191af705e *-  << This is the correct one!

$ echo "xyz" | perl -MDigest::MD5 -le 'foreach $line ( <STDIN> ) {print Digest::MD5->md5_hex(chomp($line))}'
d6b2c2e82380abb5b6b57029546640e0

$ echo -n "xyz" | perl -MDigest::MD5 -le 'foreach $line ( <STDIN> ) {print Digest::MD5->md5_hex(chomp($line))}'
7cc36a6eaa4809e20cc4db85e7ce3df5

All different hashes so what exact string am I hashing in the Perl code?

Mike

Last edited by Scott; 07-15-2014 at 08:26 PM.. Reason: More code tags
# 2  
Old 07-15-2014
Try this:

Code:
$ echo -n xyz | perl -e 'use Digest::MD5 "md5_hex"; foreach $line ( <STDIN> ) {print md5_hex($line) . "\n"}'
d16fb36f0911f878998c136191af705e

or

Code:
$  echo -n xyz | perl -MDigest::MD5 -le 'foreach $line ( <STDIN> ) {print Digest::MD5::md5_hex($line) }'
d16fb36f0911f878998c136191af705e

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 07-15-2014
Thanks. My Perl sucks. I don't even know enough to be dangerous. I'm actually getting pretty good with AWK but there are no hashes in AWK.

Mike
# 4  
Old 07-15-2014
Hi,
the ways that work fine:
Code:
$ echo -n "xyz" | md5sum
d16fb36f0911f878998c136191af705e  -
$ echo -n "xyz" | perl -MDigest::MD5 -le '$ctx = Digest::MD5->new;$ctx->add(<>);print $ctx->hexdigest'
d16fb36f0911f878998c136191af705e
$ echo -n "xyz" | perl -MDigest::MD5 -le 'print Digest::MD5::md5_hex(<>)'
d16fb36f0911f878998c136191af705e
$ echo -n "xyz" | perl -MDigest::MD5 -le 'use Digest::MD5 qw(md5_hex); print md5_hex(<>)'
d16fb36f0911f878998c136191af705e

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl : Assigning multile hash values to a single array

I know that @food = %fruit; Works. But how do I assign %fruit and %veggies to @food ? (2 Replies)
Discussion started by: popeye
2 Replies

2. Shell Programming and Scripting

Finding unique values in a hash (Perl)

Hi, I have a hash with unique keys associated with some data. my %FINALcontigs = ( 'mira_rep_c765:119reads**', 'ctctactggaagactgac', 'mira_rep_c7454:54reads**', 'atggatactgcgctgttgctaactactgga', 'mira_rep_c6803:12reads**', 'atcgactggatgcagggttgtggtttcta', ... (2 Replies)
Discussion started by: jdilts
2 Replies

3. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

4. Shell Programming and Scripting

Sorting values of hash in ascending order using Perl

I want to sort values of a hash in ascending order. my %records; for my $value (sort values %records){print $value,"\n";} When I use the above code I get values in this order: 1,10,11,2,3,4,5,6,7,8,9. But, I need values in my output in this order: 1,2,3,4,5,6,7,8,9,10,11. Can Someone... (1 Reply)
Discussion started by: koneru_18
1 Replies

5. UNIX for Dummies Questions & Answers

How to convert MD5 hash into shadow format?

I am trying to use John the Ripper but it doesn't take regular MD5 hashes, only shadow MD5 hashes. For example this hash: 900150983cd24fb0d6963f7d28e17f72 (which, decrypted, is 'abc') within a text file, John the Ripper does not detect because it is not in shadow format. How can I convert this MD5... (2 Replies)
Discussion started by: guitarscn
2 Replies

6. Shell Programming and Scripting

perl-extract data from hash values

Hello, I have parsed an xml file using perl to get the hash values and the output looks like this $VAR1 = { 'RT' => { 'List' => { 'String' => ... (1 Reply)
Discussion started by: userscript
1 Replies

7. Shell Programming and Scripting

MD5 hash filename

I am a newbie to shell programming. Can someone help me with the following ? Shell script that accomplishes the following - # Step 1 List all files in the directory # Step 2 Loop through each filename say a.htm and MD5 hash the filename to say b.htm # Step 3 copy a.htm to b.htm # Step 4... (1 Reply)
Discussion started by: bebar
1 Replies

8. Programming

MD5 hash calculation

hi i want to generate MD5 hash of string in unix (hp) i have the algorithm which takes file as argument and returns hash of file but when i tried to generate hash of "a" result was "60b725f10c9c85c70d97880dfe8191b3" hash but actually it should have been "0cc175b9c0f1b6a831c399e269772661" now i... (4 Replies)
Discussion started by: zedex
4 Replies

9. Shell Programming and Scripting

How to create md5 Hash variable?

I have a script that runs the grub-md5-crypt command based on whether the pass_value variable is a non-zero string. The md5 hash is being created in the /opt/hostconfigs/$HOST file, but I can't echo $md5_value. It is blank. Is there a way to create and echo a md5 hash variable? if then... (1 Reply)
Discussion started by: cstovall
1 Replies

10. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies
Login or Register to Ask a Question