demarches-normaliennes/app/services/zxcvbn_service.rb
2024-09-16 15:28:14 +02:00

39 lines
825 B
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
class ZxcvbnService
@tester_mutex = Mutex.new
class << self
# Returns an Zxcvbn instance cached between classes instances and between threads.
#
# The tester weights ~20 Mo, and we'd like to save some memory so rather
# that storing it in a per-thread accessor, we prefer to use a mutex
# to cache it between threads.
def tester
@tester_mutex.synchronize do
@tester ||= Zxcvbn::Tester.new
end
end
end
def initialize(password)
@password = password
end
def complexity
wxcvbn = compute_zxcvbn
score = wxcvbn.score
length = @password.blank? ? 0 : @password.length
[score, length]
end
def score
compute_zxcvbn.score
end
private
def compute_zxcvbn
self.class.tester.test(@password)
end
end