Skip to content

Instantly share code, notes, and snippets.

@Demeter
Forked from code/password_generator.rb
Created October 14, 2011 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Demeter/1287877 to your computer and use it in GitHub Desktop.
Save Demeter/1287877 to your computer and use it in GitHub Desktop.
Password generator
# 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