Sha256: 08690db5c01c5ba6d2b06fefd614fd7fb5c8e656c6c5cdff9c9869179c8a4959
Contents?: true
Size: 1.14 KB
Versions: 1
Compression:
Stored size: 1.14 KB
Contents
require "digest" require "open-uri" module Pwned class Password API_URL = "https://api.pwnedpasswords.com/range/" HASH_PREFIX_LENGTH = 5 DEFAULT_REQUEST_OPTIONS = { "User-Agent" => "Ruby Pwned::Password #{Pwned::VERSION}" } attr_reader :password def initialize(password, request_options={}) @password = password @request_options = DEFAULT_REQUEST_OPTIONS.merge(request_options) end def hashed_password Digest::SHA1.hexdigest(password).upcase end def pwned? !!match_data end def pwned_count match_data ? match_data[1].to_i : 0 end private def hashes @hashes || get_hashes end def get_hashes begin open("#{API_URL}#{hashed_password[0..(HASH_PREFIX_LENGTH-1)]}", @request_options) do |io| @hashes = io.read end @hashes rescue Timeout::Error => e raise Pwned::TimeoutError.new(e.message, e) rescue => e raise Pwned::Error.new(e.message, e) end end def match_data @match_data ||= hashes.match(/#{hashed_password[HASH_PREFIX_LENGTH..-1]}:(\d+)/) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
pwned-1.0.0 | lib/pwned/password.rb |