ruby-changes:14873
From: knu <ko1@a...>
Date: Wed, 24 Feb 2010 00:54:46 +0900 (JST)
Subject: [ruby-changes:14873] Ruby:r26740 (trunk): * ext/openssl/ossl_hmac.c (Init_ossl_hmac): Make OpenSSL::HMAC a
knu 2010-02-24 00:52:19 +0900 (Wed, 24 Feb 2010) New Revision: 26740 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=26740 Log: * ext/openssl/ossl_hmac.c (Init_ossl_hmac): Make OpenSSL::HMAC a subclass of Digest::Class so it can take advantage of all those utility methods such as base64digest. Modified files: trunk/ChangeLog trunk/ext/openssl/ossl_hmac.c Index: ChangeLog =================================================================== --- ChangeLog (revision 26739) +++ ChangeLog (revision 26740) @@ -1,3 +1,9 @@ +Wed Feb 24 00:51:14 2010 Akinori MUSHA <knu@i...> + + * ext/openssl/ossl_hmac.c (Init_ossl_hmac): Make OpenSSL::HMAC a + subclass of Digest::Class so it can take advantage of all those + utility methods such as base64digest. + Wed Feb 24 00:50:09 2010 Akinori MUSHA <knu@i...> * ext/openssl/ossl_digest.c (GetDigestPtr): Allow to pass the Index: ext/openssl/ossl_hmac.c =================================================================== --- ext/openssl/ossl_hmac.c (revision 26739) +++ ext/openssl/ossl_hmac.c (revision 26740) @@ -125,11 +125,11 @@ /* * call-seq: - * hmac.digest -> aString + * hmac.finish -> aString * */ static VALUE -ossl_hmac_digest(VALUE self) +ossl_hmac_finish(VALUE self) { HMAC_CTX *ctx; unsigned char *buf; @@ -145,89 +145,72 @@ /* * call-seq: - * hmac.hexdigest -> aString + * hmac.reset -> self * */ static VALUE -ossl_hmac_hexdigest(VALUE self) +ossl_hmac_reset(VALUE self) { HMAC_CTX *ctx; - unsigned char *buf; - char *hexbuf; - unsigned int buf_len; - VALUE hexdigest; - + GetHMAC(self, ctx); - hmac_final(ctx, &buf, &buf_len); - if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * buf_len) { - OPENSSL_free(buf); - ossl_raise(eHMACError, "Memory alloc error"); - } - OPENSSL_free(buf); - hexdigest = ossl_buf2str(hexbuf, 2 * buf_len); + HMAC_Init_ex(ctx, NULL, 0, NULL, NULL); - return hexdigest; + return self; } /* * call-seq: - * hmac.reset -> self + * hmac.digest_length -> integer * */ static VALUE -ossl_hmac_reset(VALUE self) +ossl_hmac_digest_length(VALUE self) { HMAC_CTX *ctx; GetHMAC(self, ctx); - HMAC_Init_ex(ctx, NULL, 0, NULL, NULL); - return self; + return INT2FIX(HMAC_size(ctx)); } /* * call-seq: - * HMAC.digest(digest, key, data) -> aString + * hmac.block_length -> integer * */ static VALUE -ossl_hmac_s_digest(VALUE klass, VALUE digest, VALUE key, VALUE data) +ossl_hmac_block_length(VALUE self) { - unsigned char *buf; - unsigned int buf_len; - - StringValue(key); - StringValue(data); - buf = HMAC(GetDigestPtr(digest), RSTRING_PTR(key), RSTRING_LEN(key), - (unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data), NULL, &buf_len); + HMAC_CTX *ctx; - return rb_str_new((const char *)buf, buf_len); + GetHMAC(self, ctx); + + return INT2FIX(EVP_MD_block_size(ctx->md)); } /* * call-seq: - * HMAC.digest(digest, key, data) -> aString + * HMAC.digest(digest_class, key, data) -> aString + * HMAC.digest(digest_object, key, data) -> aString + * HMAC.digest(digest_name, key, data) -> aString * + * The last three forms are still supported for backward compatibility, + * and HMAC.digest(data, key, digest_name) is _not_ supported for + * that reason. */ static VALUE -ossl_hmac_s_hexdigest(VALUE klass, VALUE digest, VALUE key, VALUE data) +ossl_hmac_s_digest(VALUE klass, VALUE digest, VALUE key, VALUE data) { unsigned char *buf; - char *hexbuf; unsigned int buf_len; - VALUE hexdigest; StringValue(key); StringValue(data); - buf = HMAC(GetDigestPtr(digest), RSTRING_PTR(key), RSTRING_LEN(key), (unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data), NULL, &buf_len); - if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * buf_len) { - ossl_raise(eHMACError, "Cannot convert buf to hexbuf"); - } - hexdigest = ossl_buf2str(hexbuf, 2 * buf_len); - return hexdigest; + return rb_str_new((const char *)buf, buf_len); } /* @@ -236,17 +219,18 @@ void Init_ossl_hmac() { + rb_require("digest"); + #if 0 /* let rdoc know about mOSSL */ mOSSL = rb_define_module("OpenSSL"); #endif eHMACError = rb_define_class_under(mOSSL, "HMACError", eOSSLError); - cHMAC = rb_define_class_under(mOSSL, "HMAC", rb_cObject); + cHMAC = rb_define_class_under(mOSSL, "HMAC", rb_path2class("Digest::Class")); rb_define_alloc_func(cHMAC, ossl_hmac_alloc); rb_define_singleton_method(cHMAC, "digest", ossl_hmac_s_digest, 3); - rb_define_singleton_method(cHMAC, "hexdigest", ossl_hmac_s_hexdigest, 3); rb_define_method(cHMAC, "initialize", ossl_hmac_initialize, 2); rb_define_copy_func(cHMAC, ossl_hmac_copy); @@ -254,10 +238,9 @@ rb_define_method(cHMAC, "reset", ossl_hmac_reset, 0); rb_define_method(cHMAC, "update", ossl_hmac_update, 1); rb_define_alias(cHMAC, "<<", "update"); - rb_define_method(cHMAC, "digest", ossl_hmac_digest, 0); - rb_define_method(cHMAC, "hexdigest", ossl_hmac_hexdigest, 0); - rb_define_alias(cHMAC, "inspect", "hexdigest"); - rb_define_alias(cHMAC, "to_s", "hexdigest"); + rb_define_private_method(cHMAC, "finish", ossl_hmac_finish, 0); + rb_define_method(cHMAC, "digest_length", ossl_hmac_digest_length, 0); + rb_define_method(cHMAC, "block_length", ossl_hmac_block_length, 0); } #else /* NO_HMAC */ -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/