command to make SHA512 password hash from stdin as crypt(3).
-s
: specifying salt.-c
: checking specified hash is valid for stdin.
unix-crypt
: unix-crypt-sha512.rb depends on it. To install, dogen install unix-crypt
.
#! /usr/bin/env python3.7 | |
import sys | |
import crypt | |
import re | |
def check(pass_hash): | |
m = re.match(r'^\$6\$(.+)\$.{86}$', pass_hash) | |
if not m: | |
print("invalid hash: %s" % pass_hash, file=sys.stderr) | |
exit(1) | |
salt = m.group(1) | |
if crypt.crypt(sys.stdin.read(), "$6$%s" % salt) == pass_hash: | |
print("True", file=sys.stderr) | |
exit(0) | |
else: | |
print("False", file=sys.stderr) | |
exit(1) | |
def main(argv): | |
if len(argv) == 3: | |
if argv[1] == '-s': | |
print(crypt.crypt(sys.stdin.read(), '$6$%s' % argv[2])) | |
elif argv[1] == '-c': | |
check(argv[2]) | |
elif len(argv) == 1: | |
print(crypt.crypt(sys.stdin.read())) | |
else: | |
print("option can be only '-s' or '-c'", file=sys.stderr) | |
exit(1) | |
if __name__ == '__main__': | |
assert crypt.methods[0] == crypt.METHOD_SHA512, \ | |
'METHOD_SHA512 cannot be used.' | |
main(sys.argv) |
#! /usr/bin/env ruby | |
require 'unix_crypt' | |
def check(pass_hash) | |
m = /^\$6\$(.+)\$.{86}$/.match(pass_hash) | |
if not m then | |
puts ("invalid hash: %s" % pass_hash) | |
exit 1 | |
end | |
salt = m.captures[0] | |
if UnixCrypt::SHA512.build($stdin.read, salt) == pass_hash then | |
STDERR.puts "True" | |
exit 0 | |
else | |
STDERR.puts "False" | |
exit 1 | |
end | |
end | |
def main(argv) | |
if argv.length == 0 then | |
pass = UnixCrypt::SHA512.build($stdin.read) | |
puts pass | |
elsif argv.length == 2 then | |
if argv[0] == "-s" then | |
salt = argv[1] | |
pass = UnixCrypt::SHA512.build($stdin.read, salt) | |
puts pass | |
elsif argv[0] == "-c" then | |
check(argv[1]) | |
else | |
STDERR.puts "option can be only '-s' or '-c'" | |
exit 1 | |
end | |
else | |
STDERR.puts "invalid numbers of args are passed." | |
exit 1 | |
end | |
end | |
if __FILE__ == $0 | |
main ARGV | |
end |