Reputation: 1
I'm trying to read a text file line by line and then printing out the SHA256 value in terminal afterwards.
#!/usr/bin/perl
use strict;
use warnings;
...
use Digest::SHA qw(sha256_hex);
while ( my $line = <$fh> ) {
print $line;
print sha256_hex($line), "\n";
print "Next", "\n";
}
close $fh;
Sample output:
test
f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2
Next
When I tried using sha256_hex('test');
instead of sha256_hex($line);
the hash value is 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
.
What did I do wrong? I am a newbie in perl, so excuse if it is something silly.
Thanks.
Upvotes: 0
Views: 296
Reputation: 123441
If you read a line from a file using <$fh>
the line end from the input is included in the result. Thus, what you did was to hash test\n
instead of test
:
use Digest::SHA 'sha256_hex';
print sha256_hex("test\n"),"\n"; # f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2
print sha256_hex("test"),"\n"; # 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
Upvotes: 3