Class: Unpwn
- Inherits:
-
Object
- Object
- Unpwn
- Defined in:
- lib/unpwn.rb,
lib/unpwn/version.rb
Overview
Unpwn checks passwords locally against the top one million passwords, as provided by the nbp project. Then, it uses the haveibeenpwned API to check proposed passwords against the largest corpus of publicly dumped passwords in the world.
Constant Summary collapse
- VERSION =
"1.0.0"
Class Attribute Summary collapse
-
.offline ⇒ Object
Set ‘offline` to true to disable requests to the haveibeenpwned.com API.
Instance Attribute Summary collapse
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#request_options ⇒ Object
readonly
Returns the value of attribute request_options.
Class Method Summary collapse
-
.acceptable?(password) ⇒ Boolean
Check if a password is not already published.
Instance Method Summary collapse
-
#acceptable?(password) ⇒ Boolean
Check if a password meets the requirements and is not pwned.
- #bloom ⇒ Object
-
#initialize(min: 8, max: nil, request_options: nil) ⇒ Unpwn
constructor
Set the options for an Unpwn instance.
- #inspect ⇒ Object (also: #to_s)
-
#pwned?(password) ⇒ Boolean
Checks if a password is pwned, via bloom filter then ‘Pwned`.
Constructor Details
#initialize(min: 8, max: nil, request_options: nil) ⇒ Unpwn
Set the options for an Unpwn instance. ‘request_options` will be passed verbatim to the `Pwned` library.
24 25 26 27 28 29 30 31 |
# File 'lib/unpwn.rb', line 24 def initialize(min: 8, max: nil, request_options: nil) raise ArgumentError if min && min < 8 raise ArgumentError if max && max < 64 @min = min @max = max @request_options = || {} end |
Class Attribute Details
.offline ⇒ Object
Set ‘offline` to true to disable requests to the haveibeenpwned.com API
10 11 12 |
# File 'lib/unpwn.rb', line 10 def offline @offline end |
Instance Attribute Details
#max ⇒ Object (readonly)
Returns the value of attribute max.
20 21 22 |
# File 'lib/unpwn.rb', line 20 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
20 21 22 |
# File 'lib/unpwn.rb', line 20 def min @min end |
#request_options ⇒ Object (readonly)
Returns the value of attribute request_options.
20 21 22 |
# File 'lib/unpwn.rb', line 20 def @request_options end |
Class Method Details
.acceptable?(password) ⇒ Boolean
Check if a password is not already published. To set options like ‘min`, `max`, or on the Pwned API check, create a new instance of your own.
15 16 17 |
# File 'lib/unpwn.rb', line 15 def acceptable?(password) new.acceptable?(password) end |
Instance Method Details
#acceptable?(password) ⇒ Boolean
Check if a password meets the requirements and is not pwned.
34 35 36 37 38 39 |
# File 'lib/unpwn.rb', line 34 def acceptable?(password) return false if min && password.size < min return false if max && password.size > max !pwned?(password) end |
#bloom ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/unpwn.rb', line 53 def bloom @bloom ||= begin require "bloomer" require "bloomer/msgpackable" top = File.read File.("top1000000.msgpack", __dir__) Bloomer.from_msgpack(top) end end |
#inspect ⇒ Object Also known as: to_s
62 63 64 |
# File 'lib/unpwn.rb', line 62 def inspect "<UnPwn bloomed=#{@bloom ? 'yes' : 'no'}>" end |
#pwned?(password) ⇒ Boolean
Checks if a password is pwned, via bloom filter then ‘Pwned`.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/unpwn.rb', line 42 def pwned?(password) pwned = bloom.include?(password) unless self.class.offline require "pwned" pwned ||= Pwned.pwned?(password, ) end pwned end |