Skip to content

Instantly share code, notes, and snippets.

@stevehodgkiss
Created September 13, 2012 04:49
Show Gist options
  • Save stevehodgkiss/3711921 to your computer and use it in GitHub Desktop.
Save stevehodgkiss/3711921 to your computer and use it in GitHub Desktop.
Run zxcvbn.js from ruby with therubyracer
# therubyracer gem - https://github.com/cowboyd/therubyracer
require 'v8'
password = ARGV[0] || 'password'
ARGV.clear
puts "Testing password: #{password}"
puts
class PasswordStrength
ZXCVBN_SOURCE = File.read('zxcvbn.js')
ATTRIBUTES = %w[ entropy crack_time crack_time_display score calc_time ]
def initialize
@ctx = V8::Context.new do |ctx|
ctx.eval('exports = {}')
ctx.eval(ZXCVBN_SOURCE)
end
end
def zxcvbn(password)
@ctx.eval("result = exports.zxcvbn('#{password}')")
results = ATTRIBUTES.inject({}) do |hash, attribute|
hash[attribute] = @ctx.eval("result.#{attribute}")
hash
end
results
end
end
password_strength = PasswordStrength.new
results = password_strength.zxcvbn(password)
results.each do |name, value|
puts "#{name}: #{value}"
end
# $ ruby zxcvbn.rb 'correct horse battery staple'
# Testing password: correct horse battery staple
#
# entropy: 62.86
# crack_time: 418398182281428.56
# crack_time_display: centuries
# score: 4
# calc_time: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment