public
Created — forked from code/password_generator.rb

Password generator

  • Download Gist
password_generator.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# File: password_generator.rb
# Author: Luke Hubbard
# Gist: http://gist.github.com/21812
# License: MIT
 
require 'digest'
class PasswordGenerator
CHARS = (('a'..'z').to_a + ('0'..'9').to_a) - %w(i o 0 1 l 0)
SIZE = 9
DIGEST = Digest::SHA2
SALT = ENV['PASSWORD_GENERATOR_SALT'] || "REPLACE ME WITH SOMETHING RANDOM!"
 
def self.random_password(size = SIZE)
(1..size).collect{|a| CHARS[rand(CHARS.size)] }.join
end
def self.digest_password(master, instance, size=SIZE, digest=DIGEST, salt=SALT)
bytes = digest.digest([salt, master, instance]*'|')
(1..size).to_a.collect{|i| CHARS[ bytes[i] % CHARS.size ] }.join
end
end
 
# Usage: example creating random password
# puts PasswordGenerator.random_password
# Usage: example creating digest password
# puts PasswordGenerator.digest_password("my master password","some.domain.name")

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.