Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

digest: redefine ::Digest constants with OpenSSL::Digest ones #377

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/openssl/digest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,43 @@ def Digest(name)
module_function :Digest

end # OpenSSL

# The 'digest' standard library providesa ::Digest, a framework for message
# digest algorithms and bundles basic implementations of commonly used
# algorithms.
#
# Ruby/OpenSSL provides a more efficient implementation of those algorithms
# in OpenSSL::Digest, which also inherts from Digest::Class and has the
# same interface as ::Digest.
#
# This redefines the constants in ::Digest with the OpenSSL implementations.
# The original constants are renamed to add "_DEFAULT" to their original name.
module Digest
rename_builtin = proc { |name|
begin
default = const_get(name)
rescue LoadError
else
const_set(:"#{name}_DEFAULT", default)
remove_const(name)
end
}

# OpenSSL::Digest.new("RIPEMD160")
RMD160 = OpenSSL::Digest::RIPEMD160 if rename_builtin.(:RMD160)

# OpenSSL::Digest.new("MD5")
MD5 = OpenSSL::Digest::MD5 if rename_builtin.(:MD5)

# OpenSSL::Digest.new("SHA1")
SHA1 = OpenSSL::Digest::SHA1 if rename_builtin.(:SHA1)

# OpenSSL::Digest.new("SHA256")
SHA256 = OpenSSL::Digest::SHA256 if rename_builtin.(:SHA256)

# OpenSSL::Digest.new("SHA384")
SHA384 = OpenSSL::Digest::SHA384 if rename_builtin.(:SHA384)

# OpenSSL::Digest.new("SHA512")
SHA512 = OpenSSL::Digest::SHA512 if rename_builtin.(:SHA512)
end
7 changes: 7 additions & 0 deletions test/openssl/test_digest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ def test_digest_constants
end
end

def test_toplevel_digest_constants
assert_same(OpenSSL::Digest::RIPEMD160, Digest::RMD160)
%w{MD5 SHA1 SHA256 SHA384 SHA512}.each do |name|
assert_same(OpenSSL::Digest.const_get(name), Digest.const_get(name))
end
end

def test_digest_by_oid_and_name
check_digest(OpenSSL::ASN1::ObjectId.new("MD5"))
check_digest(OpenSSL::ASN1::ObjectId.new("SHA1"))
Expand Down