Skip to content

Instantly share code, notes, and snippets.

@acook
Created June 3, 2016 23:30
Show Gist options
  • Save acook/9e28534d16d70e34e479757cfe427e9a to your computer and use it in GitHub Desktop.
Save acook/9e28534d16d70e34e479757cfe427e9a to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class Passwerd
CHARSETS = {
lower: (?a..?z).to_a,
upper: (?A..?Z).to_a,
number: (?0..?9).to_a,
space: [' '],
sym1: (?!..?/).to_a,
sym2: (?:..?@).to_a,
sym3: (?[..?`).to_a,
sym4: (?{..?~).to_a
}
def self.usage
warn "usage: #{$0} password_candidate"
end
def initialize candidate
@candidate = candidate
end
attr :candidate
def debug text=?\n
if $VERBOSE | $DEBUG then
warn text
end
end
def calculate1
charset = Array.new
len = 0
candidate.each_char do |char|
len += 1
debug "character: #{char}"
CHARSETS.each do |name, chars|
if chars.include? char then
charset = charset | chars
debug "char type: #{name}"
end
end
debug "length: #{len}"
debug "entropy: #{(Math.log2(charset.length)*len).round(2)}"
debug
end
puts "length: #{candidate.length}"
debug "charset: #{charset.inspect}"
puts "entropy: #{(Math.log2(charset.length)*candidate.length).round(2)}"
end
def calculate2
charset = Array.new
candidate.each_char do |char|
charset = charset | [char]
end
puts "length: #{candidate.length}"
debug "charset: #{charset.inspect}"
puts "entropy: #{(Math.log2(charset.length)*candidate.length).round(2)}"
end
end
if ARGV[0] then
p = Passwerd.new ARGV[0]
p.calculate2
else
warn "no password candidate provided"
Passwerd.usage
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment