Sha256: f1d58ad4bee43b8ace8ae57ad1107a9b308e4d81997c4970b5fa108e7be51e8c

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module Passweird
  # The Checker class is responsible for checking if a password is blacklisted
  # by generating possible terms from substrings and leet speak conversions.
  #
  # Example usage:
  #   checker = Passweird::Checker.new("password")
  #   checker.blacklisted?
  class Checker
    attr_reader :password

    def self.blacklisted?(password)
      new(password).blacklisted?
    end

    def initialize(password)
      raise ArgumentError, "password must be a String" unless password.is_a?(String)

      @password = password
    end

    # Checks if the password is blacklisted
    #
    # @return [Boolean] true if the password is blacklisted, false otherwise
    def blacklisted?
      @blacklisted ||= BlacklistedTerm.where(term: possible_terms).exists?
    end

    # Retrieves the blacklisted terms that match the possible terms
    #
    # @return [ActiveRecord::Relation] a collection of blacklisted terms
    def blacklisted_terms
      @blacklisted_terms ||= BlacklistedTerm.where(term: possible_terms)
    end

    # Generates all possible terms from substrings and leet speak equivalents
    #
    # @return [Array<String>] an array of unique possible terms
    def possible_terms
      @possible_terms ||= all_substring_case_permutations.uniq
    end

    private

    def all_substring_case_permutations
      @all_substring_case_permutations ||= substrings + unleeted_substrings + downcased_substrings + upcased_substrings
    end

    def unleeted_substrings
      @unleeted_substrings ||= LeetSpeak.unleet_all(substrings)
    end

    def substrings
      @substrings ||= Substringer.substrings(password)
    end

    def downcased_substrings
      @downcased_substrings ||= substrings.map(&:downcase)
    end

    def upcased_substrings
      @upcased_substrings ||= substrings.map(&:upcase)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
passweird-0.1.3 lib/passweird/checker.rb
passweird-0.1.2 lib/passweird/checker.rb