Created
April 24, 2012 05:47
-
-
Save chrisbloom7/2476882 to your computer and use it in GitHub Desktop.
Generate a random password containing at least one number and one special character
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
# Generate a password that is 8 - 20 characters in length, and which contains at least one number and one special character | |
# Requires Ruby >= 1.9 | |
def random_password | |
specials = ((32..47).to_a + (58..64).to_a + (91..96).to_a + (123..126).to_a).pack('U*').chars.to_a | |
numbers = (0..9).to_a | |
alpha = ('a'..'z').to_a + ('A'..'Z').to_a | |
%w{i I l L 1 O o 0}.each{ |ambiguous_character| | |
alpha.delete ambiguous_character | |
} | |
characters = (alpha + specials + numbers) | |
password = Random.new.rand(8..18).times.map{characters.sample} | |
password << specials.sample unless password.join =~ Regexp.new(Regexp.escape(specials.join)) | |
password << numbers.sample unless password.join =~ Regexp.new(Regexp.escape(numbers.join)) | |
password.shuffle.join | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Jebaar26105427@#$