Skip to content

Instantly share code, notes, and snippets.

@maugern
Created December 12, 2017 11:12
Show Gist options
  • Save maugern/04dd361689157b3a2ed4e209b7909a01 to your computer and use it in GitHub Desktop.
Save maugern/04dd361689157b3a2ed4e209b7909a01 to your computer and use it in GitHub Desktop.
Hashor, perl script to hash file line by line.
#!/usr/bin/perl
# Hashor, hashing file line by line and print output
# Copyright (C) 2017 Nicolas Mauger
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use strict;
use warnings;
use Crypt::Digest::MD5 qw(md5_hex);
use Crypt::Digest::SHA1 qw(sha1_hex);
use Crypt::Digest::SHA256 qw(sha256_hex);
use Crypt::Digest::SHA512 qw(sha512_hex);
my $file;
my $info;
if ( $#ARGV != 1 ) {
print "Error, need 2 arguments\n";
help();
} elsif ( -e $ARGV[1] ) {
$file = $ARGV[1];
open my $info, $file or die "Could not open $file: $!";
if ( $ARGV[0] eq "md5" ) {
while ( my $line = <$info> ) {
print md5_hex($line);
print"\n";
}
} elsif ( $ARGV[0] eq "sha" || $ARGV[1] eq "sha1" ) {
while ( my $line = <$info> ) {
print sha1_hex($line);
print"\n";
}
} elsif ( $ARGV[0] eq "sha256" ) {
while ( my $line = <$info> ) {
print sha256_hex($line);
print"\n";
}
} elsif ( $ARGV[0] eq "sha512" ) {
while ( my $line = <$info> ) {
print sha512_hex($line);
print"\n";
}
} else {
print "Error, unknow digest algorithm.\n";
print "Accepted digest : md5, sha1, sha256, sha512.\n";
}
close $info
} else {
print "Error, file $ARGV does not existe, or is unreadable\n";
}
sub help {
print "Usage : hashor DIGEST FILE\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment