Skip to content

Commit e8ed759

Browse files
authored
Merge pull request #726 from bdewater/digests
Add OpenSSL::Digest.digests to get a list of available digests
2 parents cecf447 + 79e6dea commit e8ed759

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

ext/openssl/ossl_cipher.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,8 @@ ossl_cipher_final(VALUE self)
442442
* call-seq:
443443
* cipher.name -> string
444444
*
445-
* Returns the name of the cipher which may differ slightly from the original
446-
* name provided.
445+
* Returns the short name of the cipher which may differ slightly from the
446+
* original name provided.
447447
*/
448448
static VALUE
449449
ossl_cipher_name(VALUE self)

ext/openssl/ossl_digest.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ VALUE ossl_digest_update(VALUE, VALUE);
103103
* Digest.new(string [, data]) -> Digest
104104
*
105105
* Creates a Digest instance based on _string_, which is either the ln
106-
* (long name) or sn (short name) of a supported digest algorithm.
106+
* (long name) or sn (short name) of a supported digest algorithm. A list of
107+
* supported algorithms can be obtained by calling OpenSSL::Digest.digests.
107108
*
108109
* If _data_ (a String) is given, it is used as the initial input to the
109110
* Digest instance, i.e.
@@ -162,6 +163,32 @@ ossl_digest_copy(VALUE self, VALUE other)
162163
return self;
163164
}
164165

166+
static void
167+
add_digest_name_to_ary(const OBJ_NAME *name, void *arg)
168+
{
169+
VALUE ary = (VALUE)arg;
170+
rb_ary_push(ary, rb_str_new2(name->name));
171+
}
172+
173+
/*
174+
* call-seq:
175+
* OpenSSL::Digest.digests -> array[string...]
176+
*
177+
* Returns the names of all available digests in an array.
178+
*/
179+
static VALUE
180+
ossl_s_digests(VALUE self)
181+
{
182+
VALUE ary;
183+
184+
ary = rb_ary_new();
185+
OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH,
186+
add_digest_name_to_ary,
187+
(void*)ary);
188+
189+
return ary;
190+
}
191+
165192
/*
166193
* call-seq:
167194
* digest.reset -> self
@@ -245,7 +272,8 @@ ossl_digest_finish(int argc, VALUE *argv, VALUE self)
245272
* call-seq:
246273
* digest.name -> string
247274
*
248-
* Returns the sn of this Digest algorithm.
275+
* Returns the short name of this Digest algorithm which may differ slightly
276+
* from the original name provided.
249277
*
250278
* === Example
251279
* digest = OpenSSL::Digest.new('SHA512')
@@ -412,6 +440,7 @@ Init_ossl_digest(void)
412440

413441
rb_define_alloc_func(cDigest, ossl_digest_alloc);
414442

443+
rb_define_module_function(cDigest, "digests", ossl_s_digests, 0);
415444
rb_define_method(cDigest, "initialize", ossl_digest_initialize, -1);
416445
rb_define_method(cDigest, "initialize_copy", ossl_digest_copy, 1);
417446
rb_define_method(cDigest, "reset", ossl_digest_reset, 0);

test/openssl/test_digest.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_sha2
8888
end
8989

9090
def test_sha512_truncate
91-
pend "SHA512_224 is not implemented" unless digest_available?('SHA512-224')
91+
pend "SHA512_224 is not implemented" unless digest_available?('sha512-224')
9292
sha512_224_a = "d5cdb9ccc769a5121d4175f2bfdd13d6310e0d3d361ea75d82108327"
9393
sha512_256_a = "455e518824bc0601f9fb858ff5c37d417d67c2f8e0df2babe4808858aea830f8"
9494

@@ -100,7 +100,7 @@ def test_sha512_truncate
100100
end
101101

102102
def test_sha3
103-
pend "SHA3 is not implemented" unless digest_available?('SHA3-224')
103+
pend "SHA3 is not implemented" unless digest_available?('sha3-224')
104104
s224 = '6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7'
105105
s256 = 'a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a'
106106
s384 = '0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004'
@@ -126,6 +126,15 @@ def test_openssl_digest
126126
end
127127
end
128128

129+
def test_digests
130+
digests = OpenSSL::Digest.digests
131+
assert_kind_of Array, digests
132+
assert_include digests, "md5"
133+
assert_include digests, "sha1"
134+
assert_include digests, "sha256"
135+
assert_include digests, "sha512"
136+
end
137+
129138
private
130139

131140
def check_digest(oid)
@@ -138,11 +147,8 @@ def check_digest(oid)
138147
end
139148

140149
def digest_available?(name)
141-
begin
142-
OpenSSL::Digest.new(name)
143-
rescue RuntimeError
144-
false
145-
end
150+
@digests ||= OpenSSL::Digest.digests
151+
@digests.include?(name)
146152
end
147153
end
148154

0 commit comments

Comments
 (0)