Created
November 3, 2008 06:19
-
-
Save code/21812 to your computer and use it in GitHub Desktop.
This file contains 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
# 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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment