diff --git a/ChangeLog b/ChangeLog index a7260b948e..6360273bfa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Mon Jun 13 11:30:10 2011 Martin Bosslet + + * ext/openssl/ossl_digest.c: allow Digests to be created by sn, ln or + oid. + * test/openssl/test_digest.rb: add tests for this. + [Ruby 1.9 - Feature #4412] [ruby-core:35319] + Mon Jun 13 10:54:03 2011 Martin Bosslet * ext/openssl/pkey_dh.c: corrected documentation. diff --git a/ext/openssl/ossl_digest.c b/ext/openssl/ossl_digest.c index 346b6cfe14..ebfca120da 100644 --- a/ext/openssl/ossl_digest.c +++ b/ext/openssl/ossl_digest.c @@ -36,12 +36,15 @@ const EVP_MD * GetDigestPtr(VALUE obj) { const EVP_MD *md; + ASN1_OBJECT *oid = NULL; if (TYPE(obj) == T_STRING) { const char *name = StringValueCStr(obj); - md = EVP_get_digestbyname(name); - if (!md) + oid = OBJ_txt2obj(name, 0); + md = EVP_get_digestbyobj(oid); + ASN1_OBJECT_free(oid); + if(!md) ossl_raise(rb_eRuntimeError, "Unsupported digest algorithm (%s).", name); } else { EVP_MD_CTX *ctx; @@ -260,8 +263,9 @@ ossl_digest_size(VALUE self) * digest.block_length -> integer * * Returns the block length of the digest algorithm, i.e. the length in bytes - * of an individual block. Most modern partition a message to be digested into - * a sequence of fix-sized blocks that are processed consecutively. + * of an individual block. Most modern algorithms partition a message to be + * digested into a sequence of fix-sized blocks that are processed + * consecutively. * * === Example * digest = OpenSSL::Digest::SHA1.new diff --git a/test/openssl/test_digest.rb b/test/openssl/test_digest.rb index e9b4ad136d..ce0d85fe55 100644 --- a/test/openssl/test_digest.rb +++ b/test/openssl/test_digest.rb @@ -56,6 +56,11 @@ class OpenSSL::TestDigest < Test::Unit::TestCase assert_equal(dig1, dig2, "reset") end + def test_digest_by_oid_and_name + check_digest(OpenSSL::ASN1::ObjectId.new("MD5")) + check_digest(OpenSSL::ASN1::ObjectId.new("SHA1")) + end + if OpenSSL::OPENSSL_VERSION_NUMBER > 0x00908000 def encode16(str) str.unpack("H*").first @@ -77,6 +82,24 @@ class OpenSSL::TestDigest < Test::Unit::TestCase assert_equal(sha384_a, encode16(OpenSSL::Digest::SHA384.digest("a"))) assert_equal(sha512_a, encode16(OpenSSL::Digest::SHA512.digest("a"))) end + + def test_digest_by_oid_and_name_sha2 + check_digest(OpenSSL::ASN1::ObjectId.new("SHA224")) + check_digest(OpenSSL::ASN1::ObjectId.new("SHA256")) + check_digest(OpenSSL::ASN1::ObjectId.new("SHA384")) + check_digest(OpenSSL::ASN1::ObjectId.new("SHA512")) + end + end + + private + + def check_digest(oid) + d = OpenSSL::Digest.new(oid.sn) + assert_not_nil(d) + d = OpenSSL::Digest.new(oid.ln) + assert_not_nil(d) + d = OpenSSL::Digest.new(oid.oid) + assert_not_nil(d) end end