Class: HibpCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/hibp_check.rb,
lib/hibp_check/version.rb

Constant Summary collapse

API_URL =
'https://api.pwnedpasswords.com/range/'
VERSION =
"0.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ HibpCheck

Returns a new instance of HibpCheck.



11
12
13
# File 'lib/hibp_check.rb', line 11

def initialize(params={})
  @params = params
end

Instance Attribute Details

#hashesObject (readonly)

Returns the value of attribute hashes.



9
10
11
# File 'lib/hibp_check.rb', line 9

def hashes
  @hashes
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/hibp_check.rb', line 9

def params
  @params
end

#prefixObject (readonly)

Returns the value of attribute prefix.



9
10
11
# File 'lib/hibp_check.rb', line 9

def prefix
  @prefix
end

#remainderObject (readonly)

Returns the value of attribute remainder.



9
10
11
# File 'lib/hibp_check.rb', line 9

def remainder
  @remainder
end

#requestObject (readonly)

Returns the value of attribute request.



9
10
11
# File 'lib/hibp_check.rb', line 9

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



9
10
11
# File 'lib/hibp_check.rb', line 9

def response
  @response
end

Instance Method Details

#password_used(password) ⇒ Object



15
16
17
18
# File 'lib/hibp_check.rb', line 15

def password_used(password)
  return nil if password.nil?
  sha1_used Digest::SHA1.hexdigest(password.to_s)
end

#sha1_used(hashed) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hibp_check.rb', line 20

def sha1_used(hashed)
  return nil unless hashed =~ /^[0-9a-f]{40}$/i
  @prefix = hashed.slice(0..4).upcase
  @remainder = hashed.slice(5..-1).upcase
  @hashes = fetch_hashes_by_prefix prefix
  if hashes
    return $1.to_i if hashes.match(/#{remainder}:(\d+)/)
    return 0
  end
  nil
end