?

Log in

No account? Create an account
penrose orange

stephenw32768


/var/log/stephen

cat /var/log/stephen >/dev/eyes


Simple password encryptor
penrose orange
stephenw32768
Unix/Linux admins often have a need to produce an encrypted password to copy and paste into /etc/passwd, or a config file for another program that authenticates using passwords encrypted with crypt(3). As far as I know, there's no standard command-line utility that simply takes a cleartext password and stuffs it through crypt(), ready to be pasted into the appropriate file. So I wrote one. Here's "makepass".

#!/usr/bin/ruby
#
# @(#) $Id: makepass,v 1.4 2003/06/13 22:37:24 stephen Exp $
#
# Cheap and cheerful password encryptor.
# Copyright (c) 2003 Stephen Williams.
#
# 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.  For more
# details, see the GNU General Public License.
# (http://www.fsf.org/licenses/gpl.txt)
#

require "getoptlong"

md5 = true
salt = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
salt = salt.split("")
help = <<END

Usage: makepass [OPTIONS] password

Recognized options:
  -c --crypt use standard Unix crypt(3) algorithm
  -m --md5   (default) use md5crypt algorithm
END

# Parse options
parser = GetoptLong.new(["-h", "--help", GetoptLong::NO_ARGUMENT],
                        ["-c", "--crypt", GetoptLong::NO_ARGUMENT],
                        ["-m", "--md5", GetoptLong::NO_ARGUMENT])
begin
  parser.each() do |opt, arg|
    case opt
    when "-h"
        print("#{help}\n")
        exit 0
    when "-c"
      md5 = false
    when "-m"
      md5 = true
    end
  end
rescue => err
  print("#{help}\n")
  exit 1
end

# Should be at least one argument left
password = ARGV[0]
if password == nil
  print("#{help}\n")
  exit 1
end

# Decide which type of salt to use
if md5
  saltsize = 8
  saltstr = "$1$"
else
  saltsize = 2
  saltstr = ""
end

# Generate appropriate salt
saltsize.times do
  saltstr.concat(salt[rand(salt.length())])
end

# Make encrypted password.  crypt() selects the algorithm based on
# the supplied salt
password = password.crypt(saltstr)
print("#{password}\n")