lib/pwned.rb in pwned-2.0.2 vs lib/pwned.rb in pwned-2.1.0
- old
+ new
@@ -1,10 +1,12 @@
# frozen_string_literal: true
+require "digest"
require "pwned/version"
require "pwned/error"
require "pwned/password"
+require "pwned/hashed_password"
begin
# Load Rails and our custom validator
require "active_model"
require "pwned/not_pwned_validator"
@@ -29,11 +31,11 @@
# Pwned.pwned?("pwned::password") #=> false
#
# @param password [String] The password you want to check against the API.
# @param [Hash] request_options Options that can be passed to +Net::HTTP.start+ when
# calling the API
- # @option request_options [Symbol] :headers ({ "User-Agent" => '"Ruby Pwned::Password #{Pwned::VERSION}" })
+ # @option request_options [Symbol] :headers ({ "User-Agent" => "Ruby Pwned::Password #{Pwned::VERSION}" })
# HTTP headers to include in the request
# @return [Boolean] Whether the password appears in the data breaches or not.
# @since 1.1.0
def self.pwned?(password, request_options={})
Pwned::Password.new(password, request_options).pwned?
@@ -47,14 +49,28 @@
# Pwned.pwned_count("pwned::password") #=> 0
#
# @param password [String] The password you want to check against the API.
# @param [Hash] request_options Options that can be passed to +Net::HTTP.start+ when
# calling the API
- # @option request_options [Symbol] :headers ({ "User-Agent" => '"Ruby Pwned::Password #{Pwned::VERSION}" })
+ # @option request_options [Symbol] :headers ({ "User-Agent" => "Ruby Pwned::Password #{Pwned::VERSION}" })
# HTTP headers to include in the request
# @return [Integer] The number of times the password has appeared in the data
# breaches.
# @since 1.1.0
def self.pwned_count(password, request_options={})
Pwned::Password.new(password, request_options).pwned_count
+ end
+
+ ##
+ # 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).
+ #
+ # @example
+ # Pwned.hash_password("password") #=> 5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8
+ #
+ # @param password [String] The password you want to check against the API
+ # @return [String] An uppercase SHA1 hash of the password
+ # @since 2.1.0
+ def self.hash_password(password)
+ Digest::SHA1.hexdigest(password).upcase
end
end