From b926877f80e7490447b78da6055285598194f42e Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Sun, 16 Feb 2020 11:20:08 +0000 Subject: random: make OpenSSL::Random.pseudo_bytes alias of .random_bytes The default implementation of RAND_pseudo_bytes() uses the same routine as RAND_bytes(). Note that OpenSSL::Random.pseudo_bytes has been available only when it is compiled with EOL versions of OpenSSL. --- ext/openssl/extconf.rb | 1 - ext/openssl/ossl_rand.c | 34 ++-------------------------------- 2 files changed, 2 insertions(+), 33 deletions(-) diff --git a/ext/openssl/extconf.rb b/ext/openssl/extconf.rb index 344d7596..a60c6af6 100644 --- a/ext/openssl/extconf.rb +++ b/ext/openssl/extconf.rb @@ -148,7 +148,6 @@ have_func("EVP_MD_CTX_new") have_func("EVP_MD_CTX_free") have_func("HMAC_CTX_new") have_func("HMAC_CTX_free") -OpenSSL.check_func("RAND_pseudo_bytes", "openssl/rand.h") # deprecated have_func("X509_STORE_get_ex_data") have_func("X509_STORE_set_ex_data") have_func("X509_STORE_get_ex_new_index") diff --git a/ext/openssl/ossl_rand.c b/ext/openssl/ossl_rand.c index 4a4f9dd5..659dc818 100644 --- a/ext/openssl/ossl_rand.c +++ b/ext/openssl/ossl_rand.c @@ -120,36 +120,6 @@ ossl_rand_bytes(VALUE self, VALUE len) return str; } -#if defined(HAVE_RAND_PSEUDO_BYTES) -/* - * call-seq: - * pseudo_bytes(length) -> string - * - * Generates a String with _length_ number of pseudo-random bytes. - * - * Pseudo-random byte sequences generated by ::pseudo_bytes will be unique if - * they are of sufficient length, but are not necessarily unpredictable. - * - * === Example - * - * OpenSSL::Random.pseudo_bytes(12) - * #=> "..." - */ -static VALUE -ossl_rand_pseudo_bytes(VALUE self, VALUE len) -{ - VALUE str; - int n = NUM2INT(len); - - str = rb_str_new(0, n); - if (RAND_pseudo_bytes((unsigned char *)RSTRING_PTR(str), n) < 1) { - ossl_raise(eRandomError, NULL); - } - - return str; -} -#endif - #ifdef HAVE_RAND_EGD /* * call-seq: @@ -219,8 +189,8 @@ Init_ossl_rand(void) rb_define_module_function(mRandom, "load_random_file", ossl_rand_load_file, 1); rb_define_module_function(mRandom, "write_random_file", ossl_rand_write_file, 1); rb_define_module_function(mRandom, "random_bytes", ossl_rand_bytes, 1); -#if defined(HAVE_RAND_PSEUDO_BYTES) - rb_define_module_function(mRandom, "pseudo_bytes", ossl_rand_pseudo_bytes, 1); +#if OPENSSL_VERSION_NUMBER < 0x10101000 || defined(LIBRESSL_VERSION_NUMBER) + rb_define_alias(rb_singleton_class(mRandom), "pseudo_bytes", "random_bytes"); #endif #ifdef HAVE_RAND_EGD rb_define_module_function(mRandom, "egd", ossl_rand_egd, 1); -- cgit v1.2.3