module Dicemypass

Constants

VERSION

Public Class Methods

check_pwned(passphrase) click to toggle source
# File lib/dicemypass.rb, line 24
def self.check_pwned(passphrase)
  # If the passphrase is an array generated by gen_passphrase we convert
  # the passphrase to an unified string, if it's a string already then
  # no changes are applied to the passphrase variable.
  passphrase = passphrase.join(' ') if passphrase.is_a?(Array)

  # We encode our passphrase to SHA-1, and save or prefix consisting
  # in 5 characters to the variable sha1_excerpt and the suffix to
  # the variable sha1_to_look_for.
  sha1_pass = Digest::SHA1.hexdigest(passphrase)
  sha1_excerpt = sha1_pass[0...5]
  sha1_to_look_for = sha1_pass[5..-1]

  # We make the API call with our SHA-1 prefix and store the response to
  # the variable api_request
  api_url = URI("https://api.pwnedpasswords.com/range/#{sha1_excerpt}")
  api_request = Net::HTTP.get(api_url)


  # The response is text instead of JSON, needs to format the response
  # to a dictionary so the rest of the hash can be located easier.
  # => String '0018A45C4D1DEF81644B54AB7F969B88D65:21'
  # => Array ['0018A45C4D1DEF81644B54AB7F969B88D65:21', ...]
  # => 2D Array [['0018A45C4D1DEF81644B54AB7F969B88D65', '21'], ...]
  # => Hash {'0018A45C4D1DEF81644B54AB7F969B88D65': 21, ...}
  striped_list = api_request.split("\r\n")
  pass_list = striped_list.map { |hash| hash.split(':') }
  hash_list = Hash[*pass_list.flatten!]
  hash_list[sha1_to_look_for.upcase]
end
gen_passphrase(pass_length = 7) click to toggle source

The default passphrase length should be 7

# File lib/dicemypass.rb, line 11
def self.gen_passphrase(pass_length = 7)

  # Read filename eff_long_wordlist and save it as a list.
  wordlist = File.readlines(@eff_wordlist)

  # Strip the '\n' out of every line.
  wordlist.map(&:strip!)

  # Shuffle the list and return a list up to pass_length words
  # which in the case would be equal to 7 words.
  wordlist.shuffle[0...pass_length]
end