Skip to content

Commit 5631203

Browse files
committed
[DOC] prefer PKey#private_to_pem and #public_to_pem in RDoc
Suggest the use of OpenSSL::PKey::PKey#private_to_pem and #public_to_pem in the top-level documentation. For new programs, these are recommended over OpenSSL::PKey::RSA#export (also aliased as #to_s and #to_pem) unless there is a specific reason to use it, i.e., unless the PKCS#1 output format specifically is required. The output format of OpenSSL::PKey::RSA#export depends on whether the key is a public key or a private key, which is very counter-intuitive. Additionally, when called with arguments to encrypt a private key, as in this example, OpenSSL's own, non-standard format is used. The man page of PEM_write_bio_PrivateKey_traditional(3) in OpenSSL 1.1.1 or later states that it "should only be used for compatibility with legacy programs".
1 parent 06d6764 commit 5631203

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

ext/openssl/ossl.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,8 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
669669
*
670670
* key = OpenSSL::PKey::RSA.new 2048
671671
*
672-
* open 'private_key.pem', 'w' do |io| io.write key.to_pem end
673-
* open 'public_key.pem', 'w' do |io| io.write key.public_key.to_pem end
672+
* File.write 'private_key.pem', key.private_to_pem
673+
* File.write 'public_key.pem', key.public_to_pem
674674
*
675675
* === Exporting a Key
676676
*
@@ -681,11 +681,9 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
681681
* cipher = OpenSSL::Cipher.new 'aes-256-cbc'
682682
* password = 'my secure password goes here'
683683
*
684-
* key_secure = key.export cipher, password
684+
* key_secure = key.private_to_pem cipher, password
685685
*
686-
* open 'private.secure.pem', 'w' do |io|
687-
* io.write key_secure
688-
* end
686+
* File.write 'private.secure.pem', key_secure
689687
*
690688
* OpenSSL::Cipher.ciphers returns a list of available ciphers.
691689
*
@@ -945,10 +943,10 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
945943
* ca_key = OpenSSL::PKey::RSA.new 2048
946944
* password = 'my secure password goes here'
947945
*
948-
* cipher = OpenSSL::Cipher.new 'aes-256-cbc'
946+
* cipher = 'aes-256-cbc'
949947
*
950948
* open 'ca_key.pem', 'w', 0400 do |io|
951-
* io.write ca_key.export(cipher, password)
949+
* io.write ca_key.private_to_pem(cipher, password)
952950
* end
953951
*
954952
* === CA Certificate

0 commit comments

Comments
 (0)