Skip to content

Instantly share code, notes, and snippets.

@chsh
Last active November 23, 2021 15:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chsh/6633785 to your computer and use it in GitHub Desktop.
Save chsh/6633785 to your computer and use it in GitHub Desktop.
Generate apache compatible htpasswd file using Ruby.
# generate apache compatible htpasswd file using Ruby.
require 'webrick'
if ARGV.size < 3
puts "usage: #{$0} password-file user password"
exit 1
end
# create htpassword instance
passwordfile = ARGV.shift
passwd = WEBrick::HTTPAuth::Htpasswd.new(passwordfile)
# register password
user = ARGV.shift
password = ARGV.shift
passwd.set_passwd(nil, user, password)
passwd.flush # save
puts IO.read(passwordfile)
# get crypted password
enc_pass = passwd.get_passwd(nil, user, false)
puts enc_pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment