Skip to content

@Demeter /password_generator.rb forked from code/password_generator.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.