Skip to content

Commit

Permalink
Merge pull request #31490 from eugeneius/hash_digest_class_truncate
Browse files Browse the repository at this point in the history
Don't include ellipsis in truncated digest output
  • Loading branch information
eileencodes committed Dec 18, 2017
2 parents 95117a2 + b9e7c67 commit b2be83f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 15 deletions.
6 changes: 3 additions & 3 deletions activesupport/CHANGELOG.md
@@ -1,7 +1,7 @@
* Introduced `ActiveSupport::Digest` that allows to specify hash function implementation * Allow the hash function used to generate non-sensitive digests, such as the
and defaults to `Digest::MD5`. 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* *Dmitri Dolguikh*


Expand Down
10 changes: 1 addition & 9 deletions activesupport/lib/active_support/digest.rb
Expand Up @@ -13,16 +13,8 @@ def hash_digest_class=(klass)
end end


def hexdigest(arg) def hexdigest(arg)
new.hexdigest(arg) hash_digest_class.hexdigest(arg)[0...32]
end end
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
end end
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/railtie.rb
Expand Up @@ -68,9 +68,9 @@ class Railtie < Rails::Railtie # :nodoc:
end end


initializer "active_support.set_hash_digest_class" do |app| 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 = ActiveSupport::Digest.hash_digest_class =
app.config.active_support.use_hash_digest_class app.config.active_support.hash_digest_class
end end
end end
end end
Expand Down
2 changes: 1 addition & 1 deletion activesupport/test/digest_test.rb
Expand Up @@ -16,7 +16,7 @@ def test_with_custom_hash_digest_class
digest = ActiveSupport::Digest.hexdigest("hello friend") digest = ActiveSupport::Digest.hexdigest("hello friend")


assert_equal 32, digest.length 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 ensure
ActiveSupport::Digest.hash_digest_class = original_hash_digest_class ActiveSupport::Digest.hash_digest_class = original_hash_digest_class
end end
Expand Down

0 comments on commit b2be83f

Please sign in to comment.