aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/digest.rb
diff options
context:
space:
mode:
authorDmitri Dolguikh <dmitri@appliedlogic.ca>2017-11-29 16:27:27 -0800
committerDmitri Dolguikh <dmitri@appliedlogic.ca>2017-12-12 11:23:54 -0800
commit82822a34217503336d51b7baab82cd18cf71e435 (patch)
tree478fd6d03d57db8a45d0bade4eb4702850a003ed /activesupport/lib/active_support/digest.rb
parentd2aca50bbd69c7a12cdcbeaba3dbef2679927b90 (diff)
downloadrails-82822a34217503336d51b7baab82cd18cf71e435.tar.gz
rails-82822a34217503336d51b7baab82cd18cf71e435.tar.bz2
rails-82822a34217503336d51b7baab82cd18cf71e435.zip
Introduced `ActiveSupport::Digest` that allows to specify hash function implementation
and defaults to `Digest::MD5`. Replaced calls to `::Digest::MD5.hexdigest` with calls to `ActiveSupport::Digest.hexdigest`.
Diffstat (limited to 'activesupport/lib/active_support/digest.rb')
-rw-r--r--activesupport/lib/active_support/digest.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/digest.rb b/activesupport/lib/active_support/digest.rb
new file mode 100644
index 0000000000..4dac32d420
--- /dev/null
+++ b/activesupport/lib/active_support/digest.rb
@@ -0,0 +1,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