deepakg has asked for the wisdom of the Perl Monks concerning the following question:

Hi Folks, Here is a small code snippet:
use Digest::SHA1;

$text = "deepak.gulati";
$hash1 = `echo -n $text | openssl dgst -sha1`;
chomp ($hash1);
print "$hash1\n";

$hash2 = Digest::SHA1::sha1_hex($text);
print "$hash2\n";
I expected $hash1 and $hash2 to be equal, but that is not the case. I suspect using grave accent has some caveats. What am I missing here? Incidentally running echo -n "deepak.gulati" | openssl dgst -sha1 from shell gives the same value as $hash2. Is `echo -n $text | openssl dgst -sha1` different from issuing the same command on shell?

Thanks!
Deepak

Replies are listed 'Best First'.
Re: Grave accent caveats
by derby (Abbot) on Jan 19, 2009 at 17:17 UTC

    They are equal for me. Maybe you're picking up a different echo (one that ignores the -n arg)? What's the following give you?

    #!/usr/bin/perl use Data::Dumper; $text = "deepak.gulati"; $hash1 = `echo -n $text` print Dumper( $hash1 );
    Are you seeing the newline in the dumper? What has the hash given by both?

    -derby
      I think it's really probable that his echo does not have -n and the backquotes are giving him the hash for "-n deepak.gulati\n"
      []s, HTH, Massa (κς,πμ,πλ)

        And Massa hit the nail on the head:

        my $text= "-n deepak.gulati\n"; my $hash2 = Digest::SHA1::sha1_hex($text); print "$hash2\n"; Output: 98bed27374f41bdef95f4dbc6cb3ff3728721837
      Thanks derby,

      Here is my output (on the Mac machine)

      $VAR1 = 'deepak.gulati';
Re: Grave accent caveats
by almut (Canon) on Jan 19, 2009 at 17:17 UTC

    Just FYI: I can't replicate the problem. I get fb723667fcd81966afcd60ec6b9d6eab364e0a31 in both cases ($hash1 and $hash2).

      Thanks for checking!

      I get:

      98bed27374f41bdef95f4dbc6cb3ff3728721837 and
      fb723667fcd81966afcd60ec6b9d6eab364e0a31

      I am on Mac OS X with perl v5.8.8, now I am beginning to wonder if it is caused by the Mac environment. I just fired a test on a Linux VM, and I get identical results for $hash1 and $hash2 too! Any perl users on Mac here?

        Try bypassing the backtick operator and use IPC::Open2

        #!/usr/bin/perl use Digest::SHA1; use IPC::Open2; use strict; use warnings; my $text = "deepak.gulati"; my $hash1 = do_openssl( $text ); my $hash2 = Digest::SHA1::sha1_hex( $text ); print $hash1, "\n"; print $hash2, "\n"; sub do_openssl { my $text = shift; open2( my $rfh, my $wfh, 'openssl dgst -sha1' ) || die "cannot open: + $!\n"; print $wfh $text; close( $wfh ); my $value = <$rfh>; close( $rfh ); chomp( $value ); return $value; }

        -derby
Re: Grave accent/Backtick/`` caveats
by Your Mother (Archbishop) on Jan 19, 2009 at 17:53 UTC

    I confirm this. It's fine on 5.10 on OS X but wrong on 5.8.8.

    # 5.10, OS X 10.4, openssl 1.19.1, Digest::SHA1 2.11 fb723667fcd81966afcd60ec6b9d6eab364e0a31 fb723667fcd81966afcd60ec6b9d6eab364e0a31 # 5.8.8, OS X 10.5, openssl 1.19.2, Digest::SHA1 2.11 98bed27374f41bdef95f4dbc6cb3ff3728721837 fb723667fcd81966afcd60ec6b9d6eab364e0a31