Skip to content

Instantly share code, notes, and snippets.

@chrisbloom7
Created April 24, 2012 05:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbloom7/2476882 to your computer and use it in GitHub Desktop.
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
# 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
@abdujebbaaraliyyi
Copy link

Jebaar26105427@#$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment