aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/digest.rb
blob: d77eeb072bd1dddc1f07600c964f6ce57d492d1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true

module ActiveSupport
  class Digest #:nodoc:
    class <<self
      def hash_digest_class
        @hash_digest_class ||= ::Digest::MD5
      end

      def hash_digest_class=(klass)
        raise ArgumentError, "#{klass} is expected to implement hexdigest class method" unless klass.respond_to?(:hexdigest)
        @hash_digest_class = klass
      end

      def hexdigest(arg)
        new.hexdigest(arg)
      end
    end

    def initialize(digest_class: nil)
      @digest_class = digest_class || self.class.hash_digest_class
    end

    def hexdigest(arg)
      @digest_class.hexdigest(arg).truncate(32)
    end
  end
end