Skip to content

Commit cd90206

Browse files
committed
Deprecate OpenSSL::Digest and OpenSSL::Cipher constants
1 parent c14e2b2 commit cd90206

File tree

7 files changed

+32
-20
lines changed

7 files changed

+32
-20
lines changed

History.md

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Compatibility notes
99
[[GitHub #266]](https://github.com/ruby/openssl/pull/266)
1010
* Deprecate `OpenSSL::Config#add_value` and `#[]=` for future removal.
1111
[[GitHub #322]](https://github.com/ruby/openssl/pull/322)
12+
* Deprecate algorithmic constants for `OpenSSL::Cipher` and `OpenSSL::Digest`
13+
for future removal.
14+
[[GitHub #366]](https://github.com/ruby/openssl/pull/366)
1215

1316

1417
Notable changes

lib/openssl/cipher.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class Cipher
2222
}
2323
}
2424
const_set(name, klass)
25+
deprecate_constant(name)
26+
klass
2527
}
2628

2729
%w(128 192 256).each{|keylen|
@@ -30,7 +32,10 @@ class Cipher
3032
super("aes-#{keylen}-#{mode}".downcase)
3133
}
3234
}
33-
const_set("AES#{keylen}", klass)
35+
name = "AES#{keylen}"
36+
const_set(name, klass)
37+
deprecate_constant(name)
38+
klass
3439
}
3540

3641
# call-seq:

lib/openssl/digest.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Digest
2424
#
2525
# which is equivalent to:
2626
#
27-
# OpenSSL::Digest.digest('SHA256', "abc")
27+
# OpenSSL::Digest.new("SHA256").digest("abc")
2828

2929
def self.digest(name, data)
3030
super(data, name)
@@ -42,7 +42,10 @@ def self.digest(name, data)
4242
define_method(:hexdigest) {|data| new.hexdigest(data)}
4343
}
4444

45-
const_set(name.tr('-', '_'), klass)
45+
constant_name = name.tr('-', '_')
46+
const_set(constant_name, klass)
47+
deprecate_constant(constant_name)
48+
klass
4649
end
4750

4851
# Deprecated.

test/openssl/test_digest.rb

+7-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class OpenSSL::TestDigest < OpenSSL::TestCase
77
def setup
88
super
99
@d1 = OpenSSL::Digest.new("MD5")
10-
@d2 = OpenSSL::Digest::MD5.new
10+
@d2 = silence_warnings { OpenSSL::Digest::MD5.new }
1111
end
1212

1313
def test_digest
@@ -54,10 +54,12 @@ def test_reset
5454
end
5555

5656
def test_digest_constants
57-
%w{MD4 MD5 RIPEMD160 SHA1 SHA224 SHA256 SHA384 SHA512}.each do |name|
58-
assert_not_nil(OpenSSL::Digest.new(name))
59-
klass = OpenSSL::Digest.const_get(name.tr('-', '_'))
60-
assert_not_nil(klass.new)
57+
silence_warnings do
58+
%w{MD4 MD5 RIPEMD160 SHA1 SHA224 SHA256 SHA384 SHA512}.each do |name|
59+
assert_not_nil(OpenSSL::Digest.new(name))
60+
klass = OpenSSL::Digest.const_get(name.tr('-', '_'))
61+
assert_not_nil(klass.new)
62+
end
6163
end
6264
end
6365

@@ -118,14 +120,6 @@ def test_digest_by_oid_and_name_sha2
118120
check_digest(OpenSSL::ASN1::ObjectId.new("SHA512"))
119121
end
120122

121-
def test_openssl_digest
122-
assert_equal OpenSSL::Digest::MD5, OpenSSL::Digest("MD5")
123-
124-
assert_raise NameError do
125-
OpenSSL::Digest("no such digest")
126-
end
127-
end
128-
129123
private
130124

131125
def check_digest(oid)

test/openssl/test_ossl.rb

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ def test_fixed_length_secure_compare
1111
assert_raise(ArgumentError) { OpenSSL.fixed_length_secure_compare("aaa", "aa") }
1212

1313
assert OpenSSL.fixed_length_secure_compare("aaa", "aaa")
14-
assert OpenSSL.fixed_length_secure_compare(
15-
OpenSSL::Digest.digest('SHA256', "aaa"), OpenSSL::Digest::SHA256.digest("aaa")
16-
)
1714

1815
assert_raise(ArgumentError) { OpenSSL.fixed_length_secure_compare("aaa", "aaaa") }
1916
refute OpenSSL.fixed_length_secure_compare("aaa", "baa")

test/openssl/test_pkcs7.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def test_detached_sign
117117

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

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

test/openssl/utils.rb

+10
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ def libressl?(major = nil, minor = nil, fix = nil)
146146
return false unless version
147147
!major || (version.map(&:to_i) <=> [major, minor, fix]) >= 0
148148
end
149+
150+
def silence_warnings(&block)
151+
begin
152+
original_verbosity = $VERBOSE
153+
$VERBOSE = nil
154+
block.call
155+
ensure
156+
$VERBOSE = original_verbosity
157+
end
158+
end
149159
end
150160

151161
class OpenSSL::TestCase < Test::Unit::TestCase

0 commit comments

Comments
 (0)