mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Back out the series of changes pending a due discussion.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26743 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
28501bb6e5
commit
d223734555
5 changed files with 72 additions and 95 deletions
22
ChangeLog
22
ChangeLog
|
@ -1,25 +1,3 @@
|
||||||
Wed Feb 24 00:52:42 2010 Akinori MUSHA <knu@iDaemons.org>
|
|
||||||
|
|
||||||
* ext/openssl/lib/openssl/digest.rb (OpenSSL::Digest::hexdigest):
|
|
||||||
No need to define hexdigest() here because the super method is
|
|
||||||
properly defined to use digest() passing through arguments.
|
|
||||||
|
|
||||||
Wed Feb 24 00:51:14 2010 Akinori MUSHA <knu@iDaemons.org>
|
|
||||||
|
|
||||||
* 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@iDaemons.org>
|
|
||||||
|
|
||||||
* ext/openssl/ossl_digest.c (GetDigestPtr): Allow to pass the
|
|
||||||
OpenSSL::Digest class in place of where either an instance of
|
|
||||||
the class or the algorithm name was demanded. For example,
|
|
||||||
OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1, key, data) is now
|
|
||||||
accepted as well as the usual
|
|
||||||
OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, key, data) and
|
|
||||||
OpenSSL::HMAC.digest("SHA1", key, data).
|
|
||||||
|
|
||||||
Wed Feb 24 00:39:17 2010 Yusuke Endoh <mame@tsg.ne.jp>
|
Wed Feb 24 00:39:17 2010 Yusuke Endoh <mame@tsg.ne.jp>
|
||||||
|
|
||||||
* string.c (str_new_empty): String#split, partition, rpartition
|
* string.c (str_new_empty): String#split, partition, rpartition
|
||||||
|
|
|
@ -43,6 +43,7 @@ module OpenSSL
|
||||||
singleton = (class << klass; self; end)
|
singleton = (class << klass; self; end)
|
||||||
singleton.class_eval{
|
singleton.class_eval{
|
||||||
define_method(:digest){|data| Digest.digest(name, data) }
|
define_method(:digest){|data| Digest.digest(name, data) }
|
||||||
|
define_method(:hexdigest){|data| Digest.hexdigest(name, data) }
|
||||||
}
|
}
|
||||||
const_set(name, klass)
|
const_set(name, klass)
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,25 +37,18 @@ GetDigestPtr(VALUE obj)
|
||||||
{
|
{
|
||||||
const EVP_MD *md;
|
const EVP_MD *md;
|
||||||
|
|
||||||
if (TYPE(obj) == T_CLASS) {
|
if (TYPE(obj) == T_STRING) {
|
||||||
EVP_MD_CTX *ctx;
|
|
||||||
VALUE digest = rb_funcall(obj, rb_intern("new"), 0, 0);
|
|
||||||
|
|
||||||
SafeGetDigest(digest, ctx);
|
|
||||||
|
|
||||||
md = EVP_MD_CTX_md(ctx);
|
|
||||||
} else if (rb_obj_is_kind_of(obj, cDigest)) {
|
|
||||||
EVP_MD_CTX *ctx;
|
|
||||||
|
|
||||||
SafeGetDigest(obj, ctx);
|
|
||||||
|
|
||||||
md = EVP_MD_CTX_md(ctx);
|
|
||||||
} else {
|
|
||||||
const char *name = StringValueCStr(obj);
|
const char *name = StringValueCStr(obj);
|
||||||
|
|
||||||
md = EVP_get_digestbyname(name);
|
md = EVP_get_digestbyname(name);
|
||||||
if (!md)
|
if (!md)
|
||||||
ossl_raise(rb_eRuntimeError, "Unsupported digest algorithm (%s).", name);
|
ossl_raise(rb_eRuntimeError, "Unsupported digest algorithm (%s).", name);
|
||||||
|
} else {
|
||||||
|
EVP_MD_CTX *ctx;
|
||||||
|
|
||||||
|
SafeGetDigest(obj, ctx);
|
||||||
|
|
||||||
|
md = EVP_MD_CTX_md(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
return md;
|
return md;
|
||||||
|
|
|
@ -125,11 +125,11 @@ hmac_final(HMAC_CTX *ctx, unsigned char **buf, unsigned int *buf_len)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* hmac.finish -> aString
|
* hmac.digest -> aString
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static VALUE
|
static VALUE
|
||||||
ossl_hmac_finish(VALUE self)
|
ossl_hmac_digest(VALUE self)
|
||||||
{
|
{
|
||||||
HMAC_CTX *ctx;
|
HMAC_CTX *ctx;
|
||||||
unsigned char *buf;
|
unsigned char *buf;
|
||||||
|
@ -143,6 +143,32 @@ ossl_hmac_finish(VALUE self)
|
||||||
return digest;
|
return digest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call-seq:
|
||||||
|
* hmac.hexdigest -> aString
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static VALUE
|
||||||
|
ossl_hmac_hexdigest(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);
|
||||||
|
|
||||||
|
return hexdigest;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* hmac.reset -> self
|
* hmac.reset -> self
|
||||||
|
@ -161,45 +187,10 @@ ossl_hmac_reset(VALUE self)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* hmac.digest_length -> integer
|
* HMAC.digest(digest, key, data) -> aString
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static VALUE
|
static VALUE
|
||||||
ossl_hmac_digest_length(VALUE self)
|
|
||||||
{
|
|
||||||
HMAC_CTX *ctx;
|
|
||||||
|
|
||||||
GetHMAC(self, ctx);
|
|
||||||
|
|
||||||
return INT2FIX(HMAC_size(ctx));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* call-seq:
|
|
||||||
* hmac.block_length -> integer
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
static VALUE
|
|
||||||
ossl_hmac_block_length(VALUE self)
|
|
||||||
{
|
|
||||||
HMAC_CTX *ctx;
|
|
||||||
|
|
||||||
GetHMAC(self, ctx);
|
|
||||||
|
|
||||||
return INT2FIX(EVP_MD_block_size(ctx->md));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* call-seq:
|
|
||||||
* 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_digest(VALUE klass, VALUE digest, VALUE key, VALUE data)
|
ossl_hmac_s_digest(VALUE klass, VALUE digest, VALUE key, VALUE data)
|
||||||
{
|
{
|
||||||
unsigned char *buf;
|
unsigned char *buf;
|
||||||
|
@ -213,24 +204,49 @@ ossl_hmac_s_digest(VALUE klass, VALUE digest, VALUE key, VALUE data)
|
||||||
return rb_str_new((const char *)buf, buf_len);
|
return rb_str_new((const char *)buf, buf_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call-seq:
|
||||||
|
* HMAC.digest(digest, key, data) -> aString
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static VALUE
|
||||||
|
ossl_hmac_s_hexdigest(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;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* INIT
|
* INIT
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Init_ossl_hmac()
|
Init_ossl_hmac()
|
||||||
{
|
{
|
||||||
rb_require("digest");
|
|
||||||
|
|
||||||
#if 0 /* let rdoc know about mOSSL */
|
#if 0 /* let rdoc know about mOSSL */
|
||||||
mOSSL = rb_define_module("OpenSSL");
|
mOSSL = rb_define_module("OpenSSL");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
eHMACError = rb_define_class_under(mOSSL, "HMACError", eOSSLError);
|
eHMACError = rb_define_class_under(mOSSL, "HMACError", eOSSLError);
|
||||||
|
|
||||||
cHMAC = rb_define_class_under(mOSSL, "HMAC", rb_path2class("Digest::Class"));
|
cHMAC = rb_define_class_under(mOSSL, "HMAC", rb_cObject);
|
||||||
|
|
||||||
rb_define_alloc_func(cHMAC, ossl_hmac_alloc);
|
rb_define_alloc_func(cHMAC, ossl_hmac_alloc);
|
||||||
rb_define_singleton_method(cHMAC, "digest", ossl_hmac_s_digest, 3);
|
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_method(cHMAC, "initialize", ossl_hmac_initialize, 2);
|
||||||
rb_define_copy_func(cHMAC, ossl_hmac_copy);
|
rb_define_copy_func(cHMAC, ossl_hmac_copy);
|
||||||
|
@ -238,9 +254,10 @@ Init_ossl_hmac()
|
||||||
rb_define_method(cHMAC, "reset", ossl_hmac_reset, 0);
|
rb_define_method(cHMAC, "reset", ossl_hmac_reset, 0);
|
||||||
rb_define_method(cHMAC, "update", ossl_hmac_update, 1);
|
rb_define_method(cHMAC, "update", ossl_hmac_update, 1);
|
||||||
rb_define_alias(cHMAC, "<<", "update");
|
rb_define_alias(cHMAC, "<<", "update");
|
||||||
rb_define_private_method(cHMAC, "finish", ossl_hmac_finish, 0);
|
rb_define_method(cHMAC, "digest", ossl_hmac_digest, 0);
|
||||||
rb_define_method(cHMAC, "digest_length", ossl_hmac_digest_length, 0);
|
rb_define_method(cHMAC, "hexdigest", ossl_hmac_hexdigest, 0);
|
||||||
rb_define_method(cHMAC, "block_length", ossl_hmac_block_length, 0);
|
rb_define_alias(cHMAC, "inspect", "hexdigest");
|
||||||
|
rb_define_alias(cHMAC, "to_s", "hexdigest");
|
||||||
}
|
}
|
||||||
|
|
||||||
#else /* NO_HMAC */
|
#else /* NO_HMAC */
|
||||||
|
|
|
@ -8,12 +8,11 @@ if defined?(OpenSSL)
|
||||||
|
|
||||||
class OpenSSL::TestHMAC < Test::Unit::TestCase
|
class OpenSSL::TestHMAC < Test::Unit::TestCase
|
||||||
def setup
|
def setup
|
||||||
@digest = OpenSSL::Digest::MD5
|
@digest = OpenSSL::Digest::MD5.new
|
||||||
@key = "KEY"
|
@key = "KEY"
|
||||||
@data = "DATA"
|
@data = "DATA"
|
||||||
@h1 = OpenSSL::HMAC.new(@key, @digest)
|
@h1 = OpenSSL::HMAC.new(@key, @digest)
|
||||||
@h2 = OpenSSL::HMAC.new(@key, @digest.new)
|
@h2 = OpenSSL::HMAC.new(@key, @digest)
|
||||||
@h3 = OpenSSL::HMAC.new(@key, "MD5")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
|
@ -21,19 +20,8 @@ class OpenSSL::TestHMAC < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_hmac
|
def test_hmac
|
||||||
@h1.update(@data)
|
@h1.update(@data)
|
||||||
@h2.update(@data)
|
|
||||||
@h3.update(@data)
|
|
||||||
assert_equal(@h1.digest, @h2.digest)
|
|
||||||
assert_equal(@h2.digest, @h3.digest)
|
|
||||||
|
|
||||||
assert_equal(OpenSSL::HMAC.digest(@digest, @key, @data), @h1.digest, "digest")
|
assert_equal(OpenSSL::HMAC.digest(@digest, @key, @data), @h1.digest, "digest")
|
||||||
assert_equal(OpenSSL::HMAC.hexdigest(@digest, @key, @data), @h1.hexdigest, "hexdigest")
|
assert_equal(OpenSSL::HMAC.hexdigest(@digest, @key, @data), @h1.hexdigest, "hexdigest")
|
||||||
|
|
||||||
assert_equal(OpenSSL::HMAC.digest(@digest.new, @key, @data), @h2.digest, "digest")
|
|
||||||
assert_equal(OpenSSL::HMAC.hexdigest(@digest.new, @key, @data), @h2.hexdigest, "hexdigest")
|
|
||||||
|
|
||||||
assert_equal(OpenSSL::HMAC.digest("MD5", @key, @data), @h3.digest, "digest")
|
|
||||||
assert_equal(OpenSSL::HMAC.hexdigest("MD5", @key, @data), @h3.hexdigest, "hexdigest")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_dup
|
def test_dup
|
||||||
|
|
Loading…
Add table
Reference in a new issue