From b9e7c676ca13e286a371dc1b4996d2c15c0461e8 Mon Sep 17 00:00:00 2001 From: Eugene Kenny Date: Sun, 17 Dec 2017 00:49:02 +0000 Subject: [PATCH] Don't include ellipsis in truncated digest output Using `truncate` to limit the length of the digest has the unwanted side effect of adding an ellipsis when the input is longer than the limit. Also: - Don't instantiate a new object for every digest - Rename the configuration option to `hash_digest_class` - Update the CHANGELOG entry to describe how to use the feature --- activesupport/CHANGELOG.md | 6 +++--- activesupport/lib/active_support/digest.rb | 10 +--------- activesupport/lib/active_support/railtie.rb | 4 ++-- activesupport/test/digest_test.rb | 2 +- 4 files changed, 7 insertions(+), 15 deletions(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index abbadd404fa25..fccaeb5d32dbd 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,7 +1,7 @@ -* Introduced `ActiveSupport::Digest` that allows to specify hash function implementation - and defaults to `Digest::MD5`. +* Allow the hash function used to generate non-sensitive digests, such as the + ETag header, to be specified with `config.active_support.hash_digest_class`. - Replaced calls to `::Digest::MD5.hexdigest` with calls to `ActiveSupport::Digest.hexdigest`. + The object provided must respond to `#hexdigest`, e.g. `Digest::SHA1`. *Dmitri Dolguikh* diff --git a/activesupport/lib/active_support/digest.rb b/activesupport/lib/active_support/digest.rb index d77eeb072bd1d..fba10fbdcf716 100644 --- a/activesupport/lib/active_support/digest.rb +++ b/activesupport/lib/active_support/digest.rb @@ -13,16 +13,8 @@ def hash_digest_class=(klass) end def hexdigest(arg) - new.hexdigest(arg) + hash_digest_class.hexdigest(arg)[0...32] 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 diff --git a/activesupport/lib/active_support/railtie.rb b/activesupport/lib/active_support/railtie.rb index 3488721df9c75..91872e29c8e30 100644 --- a/activesupport/lib/active_support/railtie.rb +++ b/activesupport/lib/active_support/railtie.rb @@ -68,9 +68,9 @@ class Railtie < Rails::Railtie # :nodoc: end initializer "active_support.set_hash_digest_class" do |app| - if app.config.active_support.respond_to?(:use_hash_digest_class) && app.config.active_support.use_hash_digest_class + if app.config.active_support.respond_to?(:hash_digest_class) && app.config.active_support.hash_digest_class ActiveSupport::Digest.hash_digest_class = - app.config.active_support.use_hash_digest_class + app.config.active_support.hash_digest_class end end end diff --git a/activesupport/test/digest_test.rb b/activesupport/test/digest_test.rb index 5dec75b9fe955..83ff2a8d834ae 100644 --- a/activesupport/test/digest_test.rb +++ b/activesupport/test/digest_test.rb @@ -16,7 +16,7 @@ def test_with_custom_hash_digest_class digest = ActiveSupport::Digest.hexdigest("hello friend") assert_equal 32, digest.length - assert_equal ::Digest::SHA1.hexdigest("hello friend").truncate(32), digest + assert_equal ::Digest::SHA1.hexdigest("hello friend")[0...32], digest ensure ActiveSupport::Digest.hash_digest_class = original_hash_digest_class end