Skip to content

Instantly share code, notes, and snippets.

@mastindersingh
Forked from dwclifton/shachk.php
Created April 18, 2017 03:49
Show Gist options
  • Save mastindersingh/f756527293849c29235ccd15acfe15db to your computer and use it in GitHub Desktop.
Save mastindersingh/f756527293849c29235ccd15acfe15db to your computer and use it in GitHub Desktop.
PHP/Perl/Python scripts to calculate or compare the SHA1 digest for a file
#!/usr/bin/env php
<?php
/* ~/bin/shachk.php -- calculate or compare the SHA1 digest for a file
gory details are at: http://www.isi.edu/in-notes/rfc3174.txt
(cc) Copyright 2004-2008, Douglas W. Clifton, some rights reserved.
for more copyright information visit the following URI:
http://loadaveragezero.com/info/copyright.php */
error_reporting(0);
$script = basename(array_shift($argv));
if ($argv) {
echo $script.': ';
$name = array_shift($argv);
if ($file = fopen($name, 'r')) {
$data = fread($file, filesize($name));
fclose($file);
$digest = sha1($data);
if ($argv) {
echo $name.' ';
$match = ($argv[0] == $digest) ? 'matches'
: 'does not match';
echo $match.' digest'."\n";
}
else echo 'digest for '.$name.': '.$digest."\n";
}
else {
$error = error_get_last();
print 'Can\'t open '.$name.': '.$error['message']."\n";
}
}
else echo <<<_U
Usage: $script [file] <digest>
+ with no arguments print this help
+ with [file] prints the SHA1 digest for the file
+ with [file] and <digest> compares against [file]
_U;
// ~/bin/shachk.php
?>
#!/usr/bin/env perl
=item
~/bin/shachk.pl -- calculate or compare the SHA1 digest for a file
gory details are at: http://www.isi.edu/in-notes/rfc3174.txt
(cc) Copyright 2004-2008, Douglas W. Clifton, some rights reserved.
for more copyright information visit the following URI:
http://loadaveragezero.com/info/copyright.php
=cut
use File::Basename;
$script = basename $0;
if ($name = shift) {
print $script.': ';
if (open(FILE, $name)) {
read(FILE, $data, -s FILE);
close(FILE);
use Digest::SHA;
$sha = Digest::SHA->new(1);
$sha->add($data);
$digest = $sha->hexdigest;
if (@ARGV) {
print $name.' ';
$match = ($ARGV[0] eq $digest) ? 'matches'
: 'does not match';
print "$match digest\n";
}
else {
print "digest for $name: $digest\n";
}
}
else {
print "Can't open $name: $!\n";
}
}
else {
print <<_U;
$script [file] <digest>
+ with no arguments print this help
+ with [file] prints the SHA1 digest for the file
+ with [file] and <digest> compares against [file]
_U
}
__END__
~/bin/shachk.pl
#!/usr/bin/env python
""" ~/bin/shachk.py -- calculate or compare the SHA1 digest for a file
NIST/FIPS details are at: http://www.isi.edu/in-notes/rfc3174.txt
(cc) Copyright 2004-2008, Douglas W. Clifton, some rights reserved.
for more copyright information visit the following URI:
http://loadaveragezero.com/info/copyright.php"""
from os import path,strerror
from sys import argv,stdout
import sha
script = path.basename(argv.pop(0))
if len(argv):
name = argv.pop(0)
try:
f = open(name)
except IOError,e:
print "%s: Can't open %s: %s" % (script,name,strerror(e.errno))
else:
stdout.write(script+': ')
digest = sha.new(f.read()).hexdigest()
f.close()
if len(argv):
stdout.write(name+' ')
match = (((argv[0] == digest) and 'matches')
or 'does not match')
print match+' digest value'
else:
print 'digest for %s: %s' % (name,digest)
else:
print """
Usage: %s [file] <digest>
+ with no arguments print this help
+ with [file] prints the SHA1 digest for the file
+ with [file] and <digest> compares against [file]
""" % (script)
### ~/bin/shachk.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment