Skip to content

Instantly share code, notes, and snippets.

@rubyconvict
Created December 12, 2015 18:47
Show Gist options
  • Save rubyconvict/2a24d2c0606f91e3c3ff to your computer and use it in GitHub Desktop.
Save rubyconvict/2a24d2c0606f91e3c3ff to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
# USAGE:
# sudo port install pwgen
# bundle exec ruby script/dev/bash_friendly_password.rb nil 49
def bash_friendly_password(password=nil, length=128)
password ||= `pwgen -syv #{length} 1`
password.size.times do |i|
password = password.
gsub(/\$/, Random.rand(1..9).to_s).
gsub(/\\/, Random.rand(1..9).to_s).
gsub(/\(/, Random.rand(1..9).to_s).
gsub(/\)/, Random.rand(1..9).to_s).
gsub(/\"/, Random.rand(1..9).to_s).
gsub(/\'/, Random.rand(1..9).to_s).
gsub(/\|/, Random.rand(1..9).to_s).
gsub(/\;/, Random.rand(1..9).to_s).
gsub(/\!/, Random.rand(1..9).to_s).
gsub(/\`/, Random.rand(1..9).to_s).
gsub(/\&/, Random.rand(1..9).to_s).
gsub(/\</, Random.rand(1..9).to_s).
gsub(/\>/, Random.rand(1..9).to_s).
gsub(/\s/, Random.rand(1..9).to_s)
end
password
end
def test_bash_friendly_password
password = "$ ` \\ !! \" ' | ; ` () & <> \n"
npwd = bash_friendly_password(password, 28)
npwd.size == password.size && (password.split & npwd.split).empty? && npwd.match(/\s/).nil?
end
if test_bash_friendly_password
puts bash_friendly_password(ARGV[0] != 'nil' ? ARGV[0] : nil, ARGV[1] ? ARGV[1].to_i : nil)
exit 0
else
puts 'fix the test!'
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment