Module: Pwned

Defined in:
lib/pwned.rb,
lib/pwned/error.rb,
lib/pwned/version.rb,
lib/pwned/password.rb,
lib/pwned/password_base.rb,
lib/pwned/hashed_password.rb

Overview

The main namespace for Pwned. Includes convenience methods for getting the results for a password.

Defined Under Namespace

Modules: PasswordBase Classes: Error, HashedPassword, Password, TimeoutError

Constant Summary collapse

VERSION =

The current version of the pwned gem.

'2.4.1'

Class Method Summary collapse

Class Method Details

.default_request_optionsHash

The default request options passed to Net::HTTP.start when calling the API.

Returns:

  • (Hash)

See Also:



33
34
35
# File 'lib/pwned.rb', line 33

def self.default_request_options
  @default_request_options
end

.default_request_options=(request_options) ⇒ Object

Sets the default request options passed to Net::HTTP.start when calling the API.

The default options may be overridden in Pwned::Password#new.

Parameters:

  • request_options (Hash)

See Also:



45
46
47
# File 'lib/pwned.rb', line 45

def self.default_request_options=(request_options)
  @default_request_options = request_options
end

.hash_password(password) ⇒ String

Returns the full SHA1 hash of the given password in uppercase. This can be safely passed around your code before making the pwned request (e.g. dropped into a queue table).

Examples:

Pwned.hash_password("password") #=> 5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8

Parameters:

  • password (String)

    The password you want to check against the API

Returns:

  • (String)

    An uppercase SHA1 hash of the password

Since:

  • 2.1.0



102
103
104
# File 'lib/pwned.rb', line 102

def self.hash_password(password)
  Digest::SHA1.hexdigest(password).upcase
end

.pwned?(password, request_options = {}) ⇒ Boolean

Returns true when the password has been pwned.

Examples:

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

Parameters:

  • password (String)

    The password you want to check against the API.

  • request_options (Hash) (defaults to: {})

    Options that can be passed to Net::HTTP.start when calling the API

Options Hash (request_options):

  • :headers (Symbol) — default: { "User-Agent" => "Ruby Pwned::Password #{Pwned::VERSION}" }

    HTTP headers to include in the request

  • :ignore_env_proxy (Symbol) — default: false

    The library will try to infer an HTTP proxy from the ‘http_proxy` environment variable. If you do not want this behaviour, set this option to true.

Returns:

  • (Boolean)

    Whether the password appears in the data breaches or not.

Since:

  • 1.1.0



66
67
68
# File 'lib/pwned.rb', line 66

def self.pwned?(password, request_options={})
  Pwned::Password.new(password, request_options).pwned?
end

.pwned_count(password, request_options = {}) ⇒ Integer

Returns number of times the password has been pwned.

Examples:

Pwned.pwned_count("password") #=> 3303003
Pwned.pwned_count("pwned::password") #=> 0

Parameters:

  • password (String)

    The password you want to check against the API.

  • request_options (Hash) (defaults to: {})

    Options that can be passed to Net::HTTP.start when calling the API

Options Hash (request_options):

  • :headers (Symbol) — default: { "User-Agent" => "Ruby Pwned::Password #{Pwned::VERSION}" }

    HTTP headers to include in the request

  • :ignore_env_proxy (Symbol) — default: false

    The library will try to infer an HTTP proxy from the ‘http_proxy` environment variable. If you do not want this behaviour, set this option to true.

Returns:

  • (Integer)

    The number of times the password has appeared in the data breaches.

Since:

  • 1.1.0



88
89
90
# File 'lib/pwned.rb', line 88

def self.pwned_count(password, request_options={})
  Pwned::Password.new(password, request_options).pwned_count
end