Last active
November 27, 2022 15:25
-
-
Save l2ol33rt/00f068b2b21aad19411c to your computer and use it in GitHub Desktop.
Generate /etc/shadow Hash Under Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'unix_crypt' | |
require 'io/console' | |
require 'optparse' | |
##### | |
# | |
# Script for generating a users password hash defaults to SHA512. Will prompt for password twice | |
# to make sure that it has been entered correctly. | |
# | |
# Requires https://github.com/mogest/unix-crypt | |
# or | |
# gem install unix-crypt | |
# | |
# Created by Robert Hernandez | |
# | |
##### | |
# Get the option flags that are passed in | |
optparse = OptionParser.new do |opts| | |
opts.banner = "Usage: %s [SHA512 (default), SHA256, MD5, DES]" % File.basename(__FILE__) | |
opts.on( '-h', '--help', 'Display this screen' ) do | |
puts opts | |
exit | |
end | |
end | |
# Remove ARGV and parse commandline flags | |
optparse.parse! | |
# Get the algorithm value from ARGV | |
if ARGV.size == 0 | |
hash_alg = 'SHA512' | |
elsif ARGV.size == 1 | |
hash_alg = ARGV[0].upcase | |
elsif ARGV.size > 1 | |
puts optparse | |
abort | |
end | |
build_string = "UnixCrypt::%s" % hash_alg | |
hash = nil | |
while true | |
print "Enter Password: " | |
password = STDIN.noecho(&:gets).chomp | |
puts | |
hash_tmp = eval(build_string).build(password, | |
if hash.nil? then hash | |
elsif hash_alg == 'DES' then hash[0..1] | |
else hash.split('$')[2] end) # Assumes defualt rounds | |
if hash_tmp == hash | |
puts hash | |
break | |
else | |
hash = hash_tmp | |
next | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment