aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2023-06-14 02:42:54 +0900
committerKazuki Yamaguchi <k@rhe.jp>2023-06-14 02:42:54 +0900
commitf1b75c5d442f405412d0689621365284a892bcec (patch)
tree2e7ab4351d84b9373ee0f4434fb234e0ea6e0884
parent24966c6309821816e8bc3bc4301c44e9b20a2045 (diff)
downloadruby-openssl-ky/cipher-inspect.tar.gz
cipher: implement OpenSSL::Cipher#inspectky/cipher-inspect
-rw-r--r--ext/openssl/ossl_cipher.c18
-rw-r--r--test/openssl/test_cipher.rb1
2 files changed, 19 insertions, 0 deletions
diff --git a/ext/openssl/ossl_cipher.c b/ext/openssl/ossl_cipher.c
index 110610e1..96f6d63e 100644
--- a/ext/openssl/ossl_cipher.c
+++ b/ext/openssl/ossl_cipher.c
@@ -176,6 +176,23 @@ ossl_s_ciphers(VALUE self)
}
/*
+ * call-seq:
+ * cipher.inspect -> string
+ *
+ * Returns a human-readable description of the Cipher object.
+ */
+static VALUE
+ossl_cipher_inspect(VALUE self)
+{
+ EVP_CIPHER_CTX *ctx;
+
+ GetCipher(self, ctx);
+ return rb_sprintf("#<%"PRIsVALUE":%p %s>",
+ rb_obj_class(self), (void *)self,
+ EVP_CIPHER_name(EVP_CIPHER_CTX_cipher(ctx)));
+}
+
+/*
* call-seq:
* cipher.reset -> self
*
@@ -1047,6 +1064,7 @@ Init_ossl_cipher(void)
rb_define_method(cCipher, "initialize_copy", ossl_cipher_copy, 1);
rb_define_module_function(cCipher, "ciphers", ossl_s_ciphers, 0);
rb_define_method(cCipher, "initialize", ossl_cipher_initialize, 1);
+ rb_define_method(cCipher, "inspect", ossl_cipher_inspect, 0);
rb_define_method(cCipher, "reset", ossl_cipher_reset, 0);
rb_define_method(cCipher, "encrypt", ossl_cipher_encrypt, -1);
rb_define_method(cCipher, "decrypt", ossl_cipher_decrypt, -1);
diff --git a/test/openssl/test_cipher.rb b/test/openssl/test_cipher.rb
index 1c8610b2..87c307aa 100644
--- a/test/openssl/test_cipher.rb
+++ b/test/openssl/test_cipher.rb
@@ -52,6 +52,7 @@ class OpenSSL::TestCipher < OpenSSL::TestCase
def test_info
cipher = OpenSSL::Cipher.new("DES-EDE3-CBC").encrypt
assert_equal "DES-EDE3-CBC", cipher.name
+ assert_match (/#<OpenSSL::Cipher:0x.*DES-EDE3-CBC/), cipher.inspect
assert_equal 24, cipher.key_len
assert_equal 8, cipher.iv_len
end