Skip to content

Commit

Permalink
Merge pull request #549 from dbussink/openssl-digest
Browse files Browse the repository at this point in the history
Move to OpenSSL Digest classes
  • Loading branch information
radar authored Jan 6, 2021
2 parents 714c376 + 658e15f commit ae0c6df
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/i18n/backend/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
#
# The cache_key implementation by default assumes you pass values that return
# a valid key from #hash (see
# http://www.ruby-doc.org/core/classes/Object.html#M000337). However, you can
# https://www.ruby-doc.org/core/classes/Object.html#M000337). However, you can
# configure your own digest method via which responds to #hexdigest (see
# http://ruby-doc.org/stdlib/libdoc/digest/rdoc/index.html):
# https://ruby-doc.org/stdlib/libdoc/openssl/rdoc/OpenSSL/Digest.html):
#
# I18n.cache_key_digest = Digest::MD5.new
# I18n.cache_key_digest = OpenSSL::Digest::SHA256.new
#
# If you use a lambda as a default value in your translation like this:
#
Expand Down
4 changes: 2 additions & 2 deletions lib/i18n/backend/cache_file.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'digest/sha2'
require 'openssl'

module I18n
module Backend
Expand All @@ -19,7 +19,7 @@ def load_file(filename)
key = I18n::Backend::Flatten.escape_default_separator(normalized_path(filename))
old_mtime, old_digest = initialized && lookup(:i18n, key, :load_file)
return if (mtime = File.mtime(filename).to_i) == old_mtime ||
(digest = Digest::SHA2.file(filename).hexdigest) == old_digest
(digest = OpenSSL::Digest::SHA256.file(filename).hexdigest) == old_digest
super
store_translations(:i18n, load_file: { key => [mtime, digest] })
end
Expand Down
8 changes: 4 additions & 4 deletions test/backend/cache_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'test_helper'
require 'digest/md5'
require 'openssl'

begin
require 'active_support'
Expand Down Expand Up @@ -74,11 +74,11 @@ def teardown
end

test "cache_key uses configured digest method" do
md5 = Digest::MD5.new
digest = OpenSSL::Digest::SHA256.new
options = { :bar => 1 }
options_hash = options.inspect
with_cache_key_digest(md5) do
assert_equal "i18n//en/#{md5.hexdigest(:foo.to_s)}/#{md5.hexdigest(options_hash)}", I18n.backend.send(:cache_key, :en, :foo, options)
with_cache_key_digest(digest) do
assert_equal "i18n//en/#{digest.hexdigest(:foo.to_s)}/#{digest.hexdigest(options_hash)}", I18n.backend.send(:cache_key, :en, :foo, options)
end
end

Expand Down

0 comments on commit ae0c6df

Please sign in to comment.