Skip to content

Commit

Permalink
Deprecate OpenSSL::Digest and OpenSSL::Cipher constants
Browse files Browse the repository at this point in the history
  • Loading branch information
bdewater committed Jun 2, 2020
1 parent 93213b2 commit d0ec5c7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 deletions.
3 changes: 3 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Compatibility notes
[[GitHub #266]](https://github.com/ruby/openssl/pull/266)
* Deprecate `OpenSSL::Config#add_value` and `#[]=` for future removal.
[[GitHub #322]](https://github.com/ruby/openssl/pull/322)
* Deprecate algorithmic constants for `OpenSSL::Cipher` and `OpenSSL::Digest`
for future removal.
[[GitHub #366]](https://github.com/ruby/openssl/pull/366)


Notable changes
Expand Down
7 changes: 6 additions & 1 deletion lib/openssl/cipher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Cipher
}
}
const_set(name, klass)
deprecate_constant(name)
klass
}

%w(128 192 256).each{|keylen|
Expand All @@ -30,7 +32,10 @@ class Cipher
super("aes-#{keylen}-#{mode}".downcase)
}
}
const_set("AES#{keylen}", klass)
name = "AES#{keylen}"
const_set(name, klass)
deprecate_constant(name)
klass
}

# call-seq:
Expand Down
7 changes: 5 additions & 2 deletions lib/openssl/digest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Digest
#
# which is equivalent to:
#
# OpenSSL::Digest.digest('SHA256', "abc")
# OpenSSL::Digest.new("SHA256").digest("abc")

def self.digest(name, data)
super(data, name)
Expand All @@ -42,7 +42,10 @@ def self.digest(name, data)
define_method(:hexdigest) {|data| new.hexdigest(data)}
}

const_set(name.tr('-', '_'), klass)
constant_name = name.tr('-', '_')
const_set(constant_name, klass)
deprecate_constant(constant_name)
klass
end

# Deprecated.
Expand Down
20 changes: 7 additions & 13 deletions test/openssl/test_digest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class OpenSSL::TestDigest < OpenSSL::TestCase
def setup
super
@d1 = OpenSSL::Digest.new("MD5")
@d2 = OpenSSL::Digest::MD5.new
@d2 = EnvUtil.suppress_warning { OpenSSL::Digest::MD5.new }
end

def test_digest
Expand Down Expand Up @@ -54,10 +54,12 @@ def test_reset
end

def test_digest_constants
%w{MD4 MD5 RIPEMD160 SHA1 SHA224 SHA256 SHA384 SHA512}.each do |name|
assert_not_nil(OpenSSL::Digest.new(name))
klass = OpenSSL::Digest.const_get(name.tr('-', '_'))
assert_not_nil(klass.new)
EnvUtil.suppress_warning do
%w{MD4 MD5 RIPEMD160 SHA1 SHA224 SHA256 SHA384 SHA512}.each do |name|
assert_not_nil(OpenSSL::Digest.new(name))
klass = OpenSSL::Digest.const_get(name.tr('-', '_'))
assert_not_nil(klass.new)
end
end
end

Expand Down Expand Up @@ -118,14 +120,6 @@ def test_digest_by_oid_and_name_sha2
check_digest(OpenSSL::ASN1::ObjectId.new("SHA512"))
end

def test_openssl_digest
assert_equal OpenSSL::Digest::MD5, OpenSSL::Digest("MD5")

assert_raise NameError do
OpenSSL::Digest("no such digest")
end
end

private

def check_digest(oid)
Expand Down
3 changes: 0 additions & 3 deletions test/openssl/test_ossl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ def test_fixed_length_secure_compare
assert_raise(ArgumentError) { OpenSSL.fixed_length_secure_compare("aaa", "aa") }

assert OpenSSL.fixed_length_secure_compare("aaa", "aaa")
assert OpenSSL.fixed_length_secure_compare(
OpenSSL::Digest.digest('SHA256', "aaa"), OpenSSL::Digest::SHA256.digest("aaa")
)

assert_raise(ArgumentError) { OpenSSL.fixed_length_secure_compare("aaa", "aaaa") }
refute OpenSSL.fixed_length_secure_compare("aaa", "baa")
Expand Down
2 changes: 1 addition & 1 deletion test/openssl/test_pkcs7.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_detached_sign

def test_enveloped
certs = [@ee1_cert, @ee2_cert]
cipher = OpenSSL::Cipher::AES.new("128-CBC")
cipher = OpenSSL::Cipher.new("AES-128-CBC")
data = "aaaaa\nbbbbb\nccccc\n"

tmp = OpenSSL::PKCS7.encrypt(certs, data, cipher, OpenSSL::PKCS7::BINARY)
Expand Down

0 comments on commit d0ec5c7

Please sign in to comment.