konstantin.saurbier.net

Password Generation Script

Download script

#!/usr/bin/env ruby

# Copyright (c) 2006,2008,2009 Konstantin Saurbier 
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.


require "getoptlong.rb"

$SVERSION = "20090627"

# Array with letters a-z, A-Z and numbers 0-9 for random-salt
@letters = Array.new
@letters << ("a".."z").to_a
@letters << ("A".."Z").to_a
@letters << (0..9).to_a
@letters.flatten!

length = 12

# Get arguments
options = GetoptLong.new
options.set_options(
  ['--length',        '-l', GetoptLong::OPTIONAL_ARGUMENT],
  ['--help',          '-h', GetoptLong::NO_ARGUMENT],
  ['--version',       '-v', GetoptLong::NO_ARGUMENT])
options.each_option do |name, arg|
  case(name)
    when("--length")
      if(arg == '')
        length = 12
      else
        length = arg.to_i
      end

    when("--help")
      # Display help message
      puts "Usage: genpass.rb [option...]"
      puts "Options:"
      puts "  -l NUMBER  --length NUMBER     Generate password of length NUMBER"
      puts "  -h  --help                     Output this help, then exit"
      puts "  -v  --version                  Output version number, then exit"
      Kernel.exit!

    when("--version")
      # Display Version
      puts "Genpass #{$SVERSION}"
      puts "  (c)2006, 2008, 2009 Konstantin Saurbier"
      Kernel.exit!
  end
end
options.terminate

def salt()
	srand
	temp = String.new
	temp << @letters[rand(@letters.length-1)]
	temp << @letters[rand(@letters.length-1)]
	return temp
end

def generate(len)
	passwd = String.new

  ((len/11)+1).times do
	  passwd << "#{Time.now.to_f}".reverse.crypt(salt()).reverse.chop.chop.reverse
  end

	return passwd[0,len]
end

puts  "Generated Password:\n"
puts generate(length)
puts "\n"

Permalink

Letzte Aktualisierung: 2013-07-28 08:17:12 CEST