Class: Pwned::Password
- Inherits:
-
Object
- Object
- Pwned::Password
- Defined in:
- lib/pwned/password.rb
Overview
This class represents a password. It does all the work of talking to the Pwned Passwords API to find out if the password has been pwned.
Constant Summary
- API_URL =
The base URL for the Pwned Passwords API
"https://api.pwnedpasswords.com/range/"
- HASH_PREFIX_LENGTH =
The number of characters from the start of the hash of the password that are used to search for the range of passwords.
5
- SHA1_LENGTH =
The total length of a SHA1 hash
40
- DEFAULT_REQUEST_OPTIONS =
The default request options that are used to make HTTP requests to the API. A user agent is provided as requested in the documentation.
{ "User-Agent" => "Ruby Pwned::Password #{Pwned::VERSION}" }.freeze
Instance Attribute Summary collapse
-
#password ⇒ String
readonly
The password that is being checked.
Instance Method Summary collapse
-
#hashed_password ⇒ String
Returns the full SHA1 hash of the given password in uppercase.
-
#initialize(password, request_options = {}) ⇒ Boolean
constructor
Creates a new password object.
-
#pwned? ⇒ Boolean
true
when the password has been pwned. -
#pwned_count ⇒ Integer
The number of times the password has been pwned.
Constructor Details
#initialize(password, request_options = {}) ⇒ Boolean
Creates a new password object.
54 55 56 57 58 |
# File 'lib/pwned/password.rb', line 54 def initialize(password, ={}) raise TypeError, "password must be of type String" unless password.is_a? String @password = password @request_options = DEFAULT_REQUEST_OPTIONS.merge() end |
Instance Attribute Details
#password ⇒ String (readonly)
Returns the password that is being checked.
36 37 38 |
# File 'lib/pwned/password.rb', line 36 def password @password end |
Instance Method Details
#hashed_password ⇒ String
Returns the full SHA1 hash of the given password in uppercase.
64 65 66 |
# File 'lib/pwned/password.rb', line 64 def hashed_password @hashed_password ||= Digest::SHA1.hexdigest(password).upcase end |
#pwned? ⇒ Boolean
Returns true
when the password has been pwned.
77 78 79 |
# File 'lib/pwned/password.rb', line 77 def pwned? pwned_count > 0 end |
#pwned_count ⇒ Integer
Returns the number of times the password has been pwned.
90 91 92 |
# File 'lib/pwned/password.rb', line 90 def pwned_count @pwned_count ||= fetch_pwned_count end |