Skip to content

Instantly share code, notes, and snippets.

@tompave
Created June 27, 2014 17:08
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 tompave/3b9ed34878893c0b9fc0 to your computer and use it in GitHub Desktop.
Save tompave/3b9ed34878893c0b9fc0 to your computer and use it in GitHub Desktop.
SecureRandom effectiveness
require 'securerandom'
REGEX = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])((?=.*[^\w])|(?=.*_))/
def run
times = 0
word = begin
times += 1
SecureRandom.urlsafe_base64
end until word =~ REGEX
times
end
attempts = (1..1_000_000).map do
run
end
counts = {}
attempts.each do |i|
counts[i] ||= 0
counts[i] += 1
end
=begin
counts:
{2=>249217,
1=>470640,
3=>132049,
4=>69634,
5=>36868,
9=>2948,
10=>1533,
6=>19600,
7=>10347,
8=>5420,
12=>443,
11=>808,
14=>117,
17=>26,
13=>238,
15=>54,
16=>40,
20=>5,
19=>3,
18=>8,
21=>2}
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment