Module: Pwned::PasswordBase

Included in:
HashedPassword, Password
Defined in:
lib/pwned/password_base.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 collapse

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_HEADERS =

The default request headers 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

Instance Method Summary collapse

Instance Attribute Details

#hashed_passwordString (readonly)

Returns the full SHA1 hash of the given password in uppercase.

Returns:

  • (String)

    The full SHA1 hash of the given password.

Since:

  • 1.0.0



64
65
66
# File 'lib/pwned/password_base.rb', line 64

def hashed_password
  @hashed_password
end

Instance Method Details

#pwned?Boolean

Returns true when the password has been pwned.

Examples:

password = Pwned::Password.new("password")
password.pwned? #=> true
password.pwned? #=> true

Returns:

  • (Boolean)

    true when the password has been pwned.

Raises:

Since:

  • 1.0.0



43
44
45
# File 'lib/pwned/password_base.rb', line 43

def pwned?
  pwned_count > 0
end

#pwned_countInteger

Returns the number of times the password has been pwned.

Examples:

password = Pwned::Password.new("password")
password.pwned_count #=> 3303003

Returns:

  • (Integer)

    the number of times the password has been pwned.

Raises:

Since:

  • 1.0.0



56
57
58
# File 'lib/pwned/password_base.rb', line 56

def pwned_count
  @pwned_count ||= fetch_pwned_count
end