Skip to content

Commit b28fb2f

Browse files
bdewaterioquatix
authored andcommitted
Look up digest by name instead of constant
1 parent 2ca54fe commit b28fb2f

31 files changed

+161
-171
lines changed

ext/openssl/ossl.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -739,15 +739,15 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
739739
* To sign a document, a cryptographically secure hash of the document is
740740
* computed first, which is then signed using the private key.
741741
*
742-
* digest = OpenSSL::Digest::SHA256.new
742+
* digest = OpenSSL::Digest.new('SHA256')
743743
* signature = key.sign digest, document
744744
*
745745
* To validate the signature, again a hash of the document is computed and
746746
* the signature is decrypted using the public key. The result is then
747747
* compared to the hash just computed, if they are equal the signature was
748748
* valid.
749749
*
750-
* digest = OpenSSL::Digest::SHA256.new
750+
* digest = OpenSSL::Digest.new('SHA256')
751751
* if key.verify digest, signature, document
752752
* puts 'Valid'
753753
* else
@@ -782,7 +782,7 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
782782
* salt = OpenSSL::Random.random_bytes 16
783783
* iter = 20000
784784
* key_len = cipher.key_len
785-
* digest = OpenSSL::Digest::SHA256.new
785+
* digest = OpenSSL::Digest.new('SHA256')
786786
*
787787
* key = OpenSSL::PKCS5.pbkdf2_hmac(pwd, salt, iter, key_len, digest)
788788
* cipher.key = key
@@ -805,7 +805,7 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
805805
* salt = ... # the one generated above
806806
* iter = 20000
807807
* key_len = cipher.key_len
808-
* digest = OpenSSL::Digest::SHA256.new
808+
* digest = OpenSSL::Digest.new('SHA256')
809809
*
810810
* key = OpenSSL::PKCS5.pbkdf2_hmac(pwd, salt, iter, key_len, digest)
811811
* cipher.key = key
@@ -901,7 +901,7 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
901901
* certificate.
902902
*
903903
* cert.issuer = name
904-
* cert.sign key, OpenSSL::Digest::SHA1.new
904+
* cert.sign key, OpenSSL::Digest.new('SHA1')
905905
*
906906
* open 'certificate.pem', 'w' do |io| io.write cert.to_pem end
907907
*
@@ -977,7 +977,7 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
977977
*
978978
* Root CA certificates are self-signed.
979979
*
980-
* ca_cert.sign ca_key, OpenSSL::Digest::SHA1.new
980+
* ca_cert.sign ca_key, OpenSSL::Digest.new('SHA1')
981981
*
982982
* The CA certificate is saved to disk so it may be distributed to all the
983983
* users of the keys this CA will sign.
@@ -995,7 +995,7 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
995995
* csr.version = 0
996996
* csr.subject = name
997997
* csr.public_key = key.public_key
998-
* csr.sign key, OpenSSL::Digest::SHA1.new
998+
* csr.sign key, OpenSSL::Digest.new('SHA1')
999999
*
10001000
* A CSR is saved to disk and sent to the CA for signing.
10011001
*
@@ -1039,7 +1039,7 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
10391039
* csr_cert.add_extension \
10401040
* extension_factory.create_extension('subjectKeyIdentifier', 'hash')
10411041
*
1042-
* csr_cert.sign ca_key, OpenSSL::Digest::SHA1.new
1042+
* csr_cert.sign ca_key, OpenSSL::Digest.new('SHA1')
10431043
*
10441044
* open 'csr_cert.pem', 'w' do |io|
10451045
* io.write csr_cert.to_pem

ext/openssl/ossl_digest.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ ossl_digest_reset(VALUE self)
192192
* be passed individually to the Digest instance.
193193
*
194194
* === Example
195-
* digest = OpenSSL::Digest::SHA256.new
195+
* digest = OpenSSL::Digest.new('SHA256')
196196
* digest.update('First input')
197197
* digest << 'Second input' # equivalent to digest.update('Second input')
198198
* result = digest.digest
@@ -248,7 +248,7 @@ ossl_digest_finish(int argc, VALUE *argv, VALUE self)
248248
* Returns the sn of this Digest algorithm.
249249
*
250250
* === Example
251-
* digest = OpenSSL::Digest::SHA512.new
251+
* digest = OpenSSL::Digest.new('SHA512')
252252
* puts digest.name # => SHA512
253253
*
254254
*/
@@ -270,7 +270,7 @@ ossl_digest_name(VALUE self)
270270
* final message digest result.
271271
*
272272
* === Example
273-
* digest = OpenSSL::Digest::SHA1.new
273+
* digest = OpenSSL::Digest.new('SHA1')
274274
* puts digest.digest_length # => 20
275275
*
276276
*/
@@ -294,7 +294,7 @@ ossl_digest_size(VALUE self)
294294
* consecutively.
295295
*
296296
* === Example
297-
* digest = OpenSSL::Digest::SHA1.new
297+
* digest = OpenSSL::Digest.new('SHA1')
298298
* puts digest.block_length # => 64
299299
*/
300300
static VALUE
@@ -348,15 +348,19 @@ Init_ossl_digest(void)
348348
* the integrity of a signed document, it suffices to re-compute the hash
349349
* and verify that it is equal to that in the signature.
350350
*
351-
* Among the supported message digest algorithms are:
352-
* * SHA, SHA1, SHA224, SHA256, SHA384 and SHA512
353-
* * MD2, MD4, MDC2 and MD5
354-
* * RIPEMD160
351+
* You can get a list of all digest algorithms supported on your system by
352+
* running this command in your terminal:
355353
*
356-
* For each of these algorithms, there is a sub-class of Digest that
357-
* can be instantiated as simply as e.g.
354+
* openssl list -digest-algorithms
358355
*
359-
* digest = OpenSSL::Digest::SHA1.new
356+
* Among the OpenSSL 1.1.1 supported message digest algorithms are:
357+
* * SHA224, SHA256, SHA384, SHA512, SHA512-224 and SHA512-256
358+
* * SHA3-224, SHA3-256, SHA3-384 and SHA3-512
359+
* * BLAKE2s256 and BLAKE2b512
360+
*
361+
* Each of these algorithms can be instantiated using the name:
362+
*
363+
* digest = OpenSSL::Digest.new('SHA256')
360364
*
361365
* === Mapping between Digest class and sn/ln
362366
*
@@ -406,15 +410,15 @@ Init_ossl_digest(void)
406410
* === Hashing a file
407411
*
408412
* data = File.read('document')
409-
* sha256 = OpenSSL::Digest::SHA256.new
413+
* sha256 = OpenSSL::Digest.new('SHA256')
410414
* digest = sha256.digest(data)
411415
*
412416
* === Hashing several pieces of data at once
413417
*
414418
* data1 = File.read('file1')
415419
* data2 = File.read('file2')
416420
* data3 = File.read('file3')
417-
* sha256 = OpenSSL::Digest::SHA256.new
421+
* sha256 = OpenSSL::Digest.new('SHA256')
418422
* sha256 << data1
419423
* sha256 << data2
420424
* sha256 << data3
@@ -423,7 +427,7 @@ Init_ossl_digest(void)
423427
* === Reuse a Digest instance
424428
*
425429
* data1 = File.read('file1')
426-
* sha256 = OpenSSL::Digest::SHA256.new
430+
* sha256 = OpenSSL::Digest.new('SHA256')
427431
* digest1 = sha256.digest(data1)
428432
*
429433
* data2 = File.read('file2')

ext/openssl/ossl_hmac.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ Init_ossl_hmac(void)
353353
* data1 = File.read("file1")
354354
* data2 = File.read("file2")
355355
* key = "key"
356-
* digest = OpenSSL::Digest::SHA256.new
356+
* digest = OpenSSL::Digest.new('SHA256')
357357
* hmac = OpenSSL::HMAC.new(key, digest)
358358
* hmac << data1
359359
* hmac << data2

ext/openssl/ossl_kdf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ Init_ossl_kdf(void)
272272
* # store this with the generated value
273273
* salt = OpenSSL::Random.random_bytes(16)
274274
* iter = 20_000
275-
* hash = OpenSSL::Digest::SHA256.new
275+
* hash = OpenSSL::Digest.new('SHA256')
276276
* len = hash.digest_length
277277
* # the final value to be stored
278278
* value = OpenSSL::KDF.pbkdf2_hmac(pass, salt: salt, iterations: iter,

ext/openssl/ossl_ns_spki.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ ossl_spki_verify(VALUE self, VALUE key)
350350
* spki = OpenSSL::Netscape::SPKI.new
351351
* spki.challenge = "RandomChallenge"
352352
* spki.public_key = key.public_key
353-
* spki.sign(key, OpenSSL::Digest::SHA256.new)
353+
* spki.sign(key, OpenSSL::Digest.new('SHA256'))
354354
* #send a request containing this to a server generating a certificate
355355
* === Verifying an SPKI request
356356
* request = #...

ext/openssl/ossl_ocsp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1719,7 +1719,7 @@ Init_ossl_ocsp(void)
17191719
* subject certificate so the CA knows which certificate we are asking
17201720
* about:
17211721
*
1722-
* digest = OpenSSL::Digest::SHA1.new
1722+
* digest = OpenSSL::Digest.new('SHA1')
17231723
* certificate_id =
17241724
* OpenSSL::OCSP::CertificateId.new subject, issuer, digest
17251725
*

ext/openssl/ossl_pkey.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ ossl_pkey_public_to_pem(VALUE self)
430430
*
431431
* == Example
432432
* data = 'Sign me!'
433-
* digest = OpenSSL::Digest::SHA256.new
433+
* digest = OpenSSL::Digest.new('SHA256')
434434
* pkey = OpenSSL::PKey::RSA.new(2048)
435435
* signature = pkey.sign(digest, data)
436436
*/
@@ -484,7 +484,7 @@ ossl_pkey_sign(VALUE self, VALUE digest, VALUE data)
484484
*
485485
* == Example
486486
* data = 'Sign me!'
487-
* digest = OpenSSL::Digest::SHA256.new
487+
* digest = OpenSSL::Digest.new('SHA256')
488488
* pkey = OpenSSL::PKey::RSA.new(2048)
489489
* signature = pkey.sign(digest, data)
490490
* pub_key = pkey.public_key

ext/openssl/ossl_pkey_dsa.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ ossl_dsa_to_public_key(VALUE self)
513513
* === Example
514514
* dsa = OpenSSL::PKey::DSA.new(2048)
515515
* doc = "Sign me"
516-
* digest = OpenSSL::Digest::SHA1.digest(doc)
516+
* digest = OpenSSL::Digest.digest('SHA1', doc)
517517
* sig = dsa.syssign(digest)
518518
*
519519
*
@@ -558,7 +558,7 @@ ossl_dsa_sign(VALUE self, VALUE data)
558558
* === Example
559559
* dsa = OpenSSL::PKey::DSA.new(2048)
560560
* doc = "Sign me"
561-
* digest = OpenSSL::Digest::SHA1.digest(doc)
561+
* digest = OpenSSL::Digest.digest('SHA1', doc)
562562
* sig = dsa.syssign(digest)
563563
* puts dsa.sysverify(digest, sig) # => true
564564
*

ext/openssl/ossl_ts.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ Init_ossl_ts(void)
12811281
* #Assumes ts.p12 is a PKCS#12-compatible file with a private key
12821282
* #and a certificate that has an extended key usage of 'timeStamping'
12831283
* p12 = OpenSSL::PKCS12.new(File.open('ts.p12', 'rb'), 'pwd')
1284-
* md = OpenSSL::Digest::SHA1.new
1284+
* md = OpenSSL::Digest.new('SHA1')
12851285
* hash = md.digest(data) #some binary data to be timestamped
12861286
* req = OpenSSL::Timestamp::Request.new
12871287
* req.algorithm = 'SHA1'
@@ -1498,8 +1498,8 @@ Init_ossl_ts(void)
14981498
* Must be an Array of String or OpenSSL::Digest subclass instances.
14991499
*
15001500
* call-seq:
1501-
* factory.allowed_digests = ["sha1", OpenSSL::Digest::SHA256.new] -> [ "sha1", OpenSSL::Digest::SHA256.new ]
1502-
* factory.allowed_digests -> array or nil
1501+
* factory.allowed_digests = ["sha1", OpenSSL::Digest.new('SHA256').new] -> [ "sha1", OpenSSL::Digest) ]
1502+
* factory.allowed_digests -> array or nil
15031503
*
15041504
*/
15051505
cTimestampFactory = rb_define_class_under(mTimestamp, "Factory", rb_cObject);

ext/openssl/ossl_x509cert.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ Init_ossl_x509cert(void)
788788
* root_ca.add_extension(ef.create_extension("keyUsage","keyCertSign, cRLSign", true))
789789
* root_ca.add_extension(ef.create_extension("subjectKeyIdentifier","hash",false))
790790
* root_ca.add_extension(ef.create_extension("authorityKeyIdentifier","keyid:always",false))
791-
* root_ca.sign(root_key, OpenSSL::Digest::SHA256.new)
791+
* root_ca.sign(root_key, OpenSSL::Digest.new('SHA256'))
792792
*
793793
* The next step is to create the end-entity certificate using the root CA
794794
* certificate.
@@ -807,7 +807,7 @@ Init_ossl_x509cert(void)
807807
* ef.issuer_certificate = root_ca
808808
* cert.add_extension(ef.create_extension("keyUsage","digitalSignature", true))
809809
* cert.add_extension(ef.create_extension("subjectKeyIdentifier","hash",false))
810-
* cert.sign(root_key, OpenSSL::Digest::SHA256.new)
810+
* cert.sign(root_key, OpenSSL::Digest.new('SHA256'))
811811
*
812812
*/
813813
cX509Cert = rb_define_class_under(mX509, "Certificate", rb_cObject);

0 commit comments

Comments
 (0)