summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-10-11 17:25:45 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-10-11 17:25:45 +0000
commit368c8038f402db5c30eef7fc41ac09b6e6829ef4 (patch)
treec8f8d08bdb6d46fb3236647b0d62d915ca199239 /ext
parentb3f7970ef9dbbb8e1470c10747cf45574b6fc399 (diff)
* ext/digest: Merge from trunk; metadata location changed,
Digest::Base#reset() added, Digest::Base#equal() changed, and digest/hmac added with some modifications for ruby 1.8. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@11137 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/digest/depend6
-rw-r--r--ext/digest/digest.c70
-rw-r--r--ext/digest/digest.h6
-rw-r--r--ext/digest/extconf.rb4
-rw-r--r--ext/digest/lib/digest/hmac.rb12
-rw-r--r--ext/digest/md5/md5.c6
-rw-r--r--ext/digest/md5/md5.h2
-rw-r--r--ext/digest/md5/md5init.c8
-rw-r--r--ext/digest/md5/md5ossl.c15
-rw-r--r--ext/digest/md5/md5ossl.h3
-rw-r--r--ext/digest/rmd160/rmd160.c7
-rw-r--r--ext/digest/rmd160/rmd160.h2
-rw-r--r--ext/digest/rmd160/rmd160init.c8
-rw-r--r--ext/digest/rmd160/rmd160ossl.c14
-rw-r--r--ext/digest/rmd160/rmd160ossl.h1
-rw-r--r--ext/digest/sha1/sha1.c6
-rw-r--r--ext/digest/sha1/sha1.h2
-rw-r--r--ext/digest/sha1/sha1init.c8
-rw-r--r--ext/digest/sha1/sha1ossl.c15
-rw-r--r--ext/digest/sha1/sha1ossl.h5
-rw-r--r--ext/digest/sha2/sha2.c18
-rw-r--r--ext/digest/sha2/sha2.h6
-rw-r--r--ext/digest/sha2/sha2init.c8
23 files changed, 86 insertions, 146 deletions
diff --git a/ext/digest/depend b/ext/digest/depend
index 1fd76db..43601a2 100644
--- a/ext/digest/depend
+++ b/ext/digest/depend
@@ -1,8 +1,2 @@
digest.o: digest.c digest.h $(hdrdir)/ruby.h $(topdir)/config.h \
$(hdrdir)/defines.h $(hdrdir)/intern.h
-
-install-so: install-h
-site-install-so: install-h
-
-install-h:
- $(INSTALL_DATA) $(srcdir)/digest.h $(RUBYARCHDIR)
diff --git a/ext/digest/digest.c b/ext/digest/digest.c
index 38ba36f..b8144f3 100644
--- a/ext/digest/digest.c
+++ b/ext/digest/digest.c
@@ -16,7 +16,7 @@
#include "digest.h"
static VALUE mDigest, cDigest_Base;
-static ID id_metadata, id_new, id_update, id_digest;
+static ID id_metadata, id_new, id_initialize, id_update, id_digest;
/*
* Digest::Base
@@ -28,11 +28,11 @@ get_digest_base_metadata(VALUE klass)
VALUE obj;
algo_t *algo;
- if (rb_cvar_defined(klass, id_metadata) == Qfalse) {
+ if (rb_ivar_defined(klass, id_metadata) == Qfalse) {
return NULL;
}
- obj = rb_cvar_get(klass, id_metadata);
+ obj = rb_ivar_get(klass, id_metadata);
Data_Get_Struct(obj, algo_t, algo);
@@ -151,8 +151,7 @@ rb_digest_base_alloc(VALUE klass)
return Data_Wrap_Struct(klass, 0, free, 0);
}
- /* XXX: An uninitialized buffer may lead ALGO_Equal() to fail */
- pctx = xcalloc(algo->ctx_size, 1);
+ pctx = xmalloc(algo->ctx_size);
algo->init_func(pctx);
obj = Data_Wrap_Struct(klass, 0, free, pctx);
@@ -207,8 +206,10 @@ rb_digest_base_copy(VALUE copy, VALUE obj)
rb_check_frozen(copy);
algo = get_digest_base_metadata(rb_obj_class(copy));
- if (algo == NULL)
+ if (algo == NULL) {
+ /* initialize_copy() is undefined or something */
rb_notimplement();
+ }
/* get_digest_base_metadata() may return a NULL */
if (algo != get_digest_base_metadata(rb_obj_class(obj))) {
@@ -222,6 +223,28 @@ rb_digest_base_copy(VALUE copy, VALUE obj)
}
static VALUE
+rb_digest_base_reset(VALUE self)
+{
+ algo_t *algo;
+ void *pctx;
+
+ algo = get_digest_base_metadata(rb_obj_class(self));
+
+ if (algo == NULL) {
+ rb_funcall(self, id_initialize, 0);
+
+ return self;
+ }
+
+ Data_Get_Struct(self, void, pctx);
+
+ memset(pctx, 0, algo->ctx_size);
+ algo->init_func(pctx);
+
+ return self;
+}
+
+static VALUE
rb_digest_base_update(VALUE self, VALUE str)
{
algo_t *algo;
@@ -287,8 +310,10 @@ rb_digest_base_digest(VALUE self)
algo = get_digest_base_metadata(rb_obj_class(self));
- if (algo == NULL)
+ if (algo == NULL) {
+ /* subclasses must define update() */
rb_notimplement();
+ }
Data_Get_Struct(self, void, pctx1);
@@ -349,27 +374,24 @@ rb_digest_base_equal(VALUE self, VALUE other)
VALUE str1, str2;
klass = rb_obj_class(self);
- algo = get_digest_base_metadata(klass);
if (rb_obj_class(other) == klass) {
- void *pctx1, *pctx2;
-
- Data_Get_Struct(self, void, pctx1);
- Data_Get_Struct(other, void, pctx2);
-
- return algo->equal_func(pctx1, pctx2) ? Qtrue : Qfalse;
+ str1 = rb_digest_base_digest(self);
+ str2 = rb_digest_base_digest(other);
+ } else {
+ StringValue(other);
+ str2 = other;
+
+ algo = get_digest_base_metadata(klass);
+
+ if (RSTRING_LEN(str2) == algo->digest_len)
+ str1 = rb_digest_base_digest(self);
+ else
+ str1 = rb_digest_base_hexdigest(self);
}
- StringValue(other);
- str2 = other;
-
- if (RSTRING_LEN(str2) == algo->digest_len)
- str1 = rb_digest_base_digest(self);
- else
- str1 = rb_digest_base_hexdigest(self);
-
if (RSTRING_LEN(str1) == RSTRING_LEN(str2)
- && rb_str_cmp(str1, str2) == 0)
+ && rb_str_cmp(str1, str2) == 0)
return Qtrue;
return Qfalse;
@@ -407,6 +429,7 @@ Init_digest(void)
rb_define_method(cDigest_Base, "initialize", rb_digest_base_init, -1);
rb_define_method(cDigest_Base, "initialize_copy", rb_digest_base_copy, 1);
+ rb_define_method(cDigest_Base, "reset", rb_digest_base_reset, 0);
rb_define_method(cDigest_Base, "update", rb_digest_base_update, 1);
rb_define_method(cDigest_Base, "<<", rb_digest_base_lshift, 1);
rb_define_method(cDigest_Base, "digest", rb_digest_base_digest, 0);
@@ -418,6 +441,7 @@ Init_digest(void)
id_metadata = rb_intern("metadata");
id_new = rb_intern("new");
+ id_initialize = rb_intern("initialize");
id_update = rb_intern("update");
id_digest = rb_intern("digest");
}
diff --git a/ext/digest/digest.h b/ext/digest/digest.h
index d9e5c70..ee3c680 100644
--- a/ext/digest/digest.h
+++ b/ext/digest/digest.h
@@ -1,12 +1,12 @@
/************************************************
- digest.c -
+ digest.h - header file for ruby digest modules
$Author$
created at: Fri May 25 08:54:56 JST 2001
- Copyright (C) 2001 Akinori MUSHA
+ Copyright (C) 2001-2006 Akinori MUSHA
$RoughId: digest.h,v 1.3 2001/07/13 15:38:27 knu Exp $
$Id$
@@ -18,7 +18,6 @@
typedef void (*hash_init_func_t)(void *);
typedef void (*hash_update_func_t)(void *, unsigned char *, size_t);
typedef void (*hash_finish_func_t)(void *, unsigned char *);
-typedef int (*hash_equal_func_t)(void *, void *);
typedef struct {
size_t digest_len;
@@ -26,5 +25,4 @@ typedef struct {
hash_init_func_t init_func;
hash_update_func_t update_func;
hash_finish_func_t finish_func;
- hash_equal_func_t equal_func;
} algo_t;
diff --git a/ext/digest/extconf.rb b/ext/digest/extconf.rb
index cd51262..cf9127e 100644
--- a/ext/digest/extconf.rb
+++ b/ext/digest/extconf.rb
@@ -3,4 +3,8 @@
require "mkmf"
+$INSTALLFILES = {
+ "digest.h" => "$(RUBYARCHDIR)"
+}
+
create_makefile("digest")
diff --git a/ext/digest/lib/digest/hmac.rb b/ext/digest/lib/digest/hmac.rb
index e6870f9..746a9ce 100644
--- a/ext/digest/lib/digest/hmac.rb
+++ b/ext/digest/lib/digest/hmac.rb
@@ -51,12 +51,14 @@ module Digest
@@algo = superclass
def initialize(text = nil)
- ipad = Array.new(BLOCK_LENGTH).fill(0x36)
- opad = Array.new(BLOCK_LENGTH).fill(0x5c)
+ ipad = Array.new(@@algo::BLOCK_LENGTH).fill(0x36)
+ opad = Array.new(@@algo::BLOCK_LENGTH).fill(0x5c)
- KEY.bytes.each_with_index { |c, i|
+ i = 0
+ self.class::KEY.each_byte { |c|
ipad[i] ^= c
opad[i] ^= c
+ i += 1
}
@ipad = ipad.inject('') { |s, c| s << c.chr }
@@ -78,11 +80,11 @@ module Digest
end
def self.inspect
- sprintf('#<%s.hmac(%s)>', @@algo.name, KEY.inspect);
+ sprintf('#<%s.hmac(%s)>', @@algo.name, self::KEY.inspect);
end
def inspect
- sprintf('#<%s.hmac(%s): %s>', @@algo.name, KEY.inspect, hexdigest());
+ sprintf('#<%s.hmac(%s): %s>', @@algo.name, self.class::KEY.inspect, hexdigest());
end
}
end
diff --git a/ext/digest/md5/md5.c b/ext/digest/md5/md5.c
index 8f49476..993bc47 100644
--- a/ext/digest/md5/md5.c
+++ b/ext/digest/md5/md5.c
@@ -418,9 +418,3 @@ MD5_Finish(MD5_CTX *pms, uint8_t *digest)
for (i = 0; i < 16; ++i)
digest[i] = (uint8_t)(pms->state[i >> 2] >> ((i & 3) << 3));
}
-
-int MD5_Equal(MD5_CTX* pctx1, MD5_CTX* pctx2) {
- return memcmp(pctx1->count, pctx2->count, sizeof(pctx1->count)) == 0
- && memcmp(pctx1->state, pctx2->state, sizeof(pctx1->state)) == 0
- && memcmp(pctx1->buffer, pctx2->buffer, sizeof(pctx1->buffer)) == 0;
-}
diff --git a/ext/digest/md5/md5.h b/ext/digest/md5/md5.h
index a5de6fd..f4580ef 100644
--- a/ext/digest/md5/md5.h
+++ b/ext/digest/md5/md5.h
@@ -67,13 +67,11 @@ typedef struct md5_state_s {
#define MD5_Init rb_Digest_MD5_Init
#define MD5_Update rb_Digest_MD5_Update
#define MD5_Finish rb_Digest_MD5_Finish
-#define MD5_Equal rb_Digest_MD5_Equal
#endif
void MD5_Init _((MD5_CTX *pms));
void MD5_Update _((MD5_CTX *pms, const uint8_t *data, size_t nbytes));
void MD5_Finish _((MD5_CTX *pms, uint8_t *digest));
-int MD5_Equal _((MD5_CTX *pctx1, MD5_CTX *pctx2));
#define MD5_BLOCK_LENGTH 64
#define MD5_DIGEST_LENGTH 16
diff --git a/ext/digest/md5/md5init.c b/ext/digest/md5/md5init.c
index 5fddb21..6fdf6e0 100644
--- a/ext/digest/md5/md5init.c
+++ b/ext/digest/md5/md5init.c
@@ -14,7 +14,6 @@ static algo_t md5 = {
(hash_init_func_t)MD5_Init,
(hash_update_func_t)MD5_Update,
(hash_finish_func_t)MD5_Finish,
- (hash_equal_func_t)MD5_Equal,
};
void
@@ -29,6 +28,9 @@ Init_md5()
cDigest_MD5 = rb_define_class_under(mDigest, "MD5", cDigest_Base);
- rb_cvar_set(cDigest_MD5, rb_intern("metadata"),
- Data_Wrap_Struct(rb_cObject, 0, 0, &md5), Qtrue);
+ rb_define_const(cDigest_MD5, "DIGEST_LENGTH", INT2NUM(MD5_DIGEST_LENGTH));
+ rb_define_const(cDigest_MD5, "BLOCK_LENGTH", INT2NUM(MD5_BLOCK_LENGTH));
+
+ rb_ivar_set(cDigest_MD5, rb_intern("metadata"),
+ Data_Wrap_Struct(rb_cObject, 0, 0, &md5));
}
diff --git a/ext/digest/md5/md5ossl.c b/ext/digest/md5/md5ossl.c
index 963243c..d94ae2c 100644
--- a/ext/digest/md5/md5ossl.c
+++ b/ext/digest/md5/md5ossl.c
@@ -1,24 +1,9 @@
/* $Id$ */
#include "md5ossl.h"
-#include <sys/types.h>
-#include <stdio.h>
-#include <string.h>
void
MD5_Finish(MD5_CTX *pctx, unsigned char *digest)
{
MD5_Final(digest, pctx);
}
-
-int
-MD5_Equal(MD5_CTX* pctx1, MD5_CTX* pctx2) {
- return pctx1->num == pctx2->num
- && pctx1->A == pctx2->A
- && pctx1->B == pctx2->B
- && pctx1->C == pctx2->C
- && pctx1->D == pctx2->D
- && pctx1->Nl == pctx2->Nl
- && pctx1->Nh == pctx2->Nh
- && memcmp(pctx1->data, pctx2->data, sizeof(pctx1->data)) == 0;
-}
diff --git a/ext/digest/md5/md5ossl.h b/ext/digest/md5/md5ossl.h
index bccbe05..1680c4f 100644
--- a/ext/digest/md5/md5ossl.h
+++ b/ext/digest/md5/md5ossl.h
@@ -6,7 +6,8 @@
#include <stddef.h>
#include <openssl/md5.h>
+#define MD5_BLOCK_LENGTH MD5_CBLOCK
+
void MD5_Finish(MD5_CTX *pctx, unsigned char *digest);
-int MD5_Equal(MD5_CTX *pctx1, MD5_CTX *pctx2);
#endif
diff --git a/ext/digest/rmd160/rmd160.c b/ext/digest/rmd160/rmd160.c
index 64ec0fb..8891872 100644
--- a/ext/digest/rmd160/rmd160.c
+++ b/ext/digest/rmd160/rmd160.c
@@ -454,11 +454,4 @@ RMD160_Finish(RMD160_CTX *context, uint8_t digest[20])
}
}
-int RMD160_Equal(RMD160_CTX* pctx1, RMD160_CTX* pctx2) {
- return pctx1->buflen == pctx2->buflen
- && memcmp(pctx1->length, pctx2->length, sizeof(pctx1->length)) == 0
- && memcmp(pctx1->state, pctx2->state, sizeof(pctx1->state)) == 0
- && memcmp(pctx1->bbuffer, pctx2->bbuffer, sizeof(pctx1->bbuffer)) == 0;
-}
-
/************************ end of file rmd160.c **********************/
diff --git a/ext/digest/rmd160/rmd160.h b/ext/digest/rmd160/rmd160.h
index ac52fb0..54d1ca9 100644
--- a/ext/digest/rmd160/rmd160.h
+++ b/ext/digest/rmd160/rmd160.h
@@ -40,7 +40,6 @@ typedef struct {
#define RMD160_Transform rb_Digest_RMD160_Transform
#define RMD160_Update rb_Digest_RMD160_Update
#define RMD160_Finish rb_Digest_RMD160_Finish
-#define RMD160_Equal rb_Digest_RMD160_Equal
#endif
__BEGIN_DECLS
@@ -48,7 +47,6 @@ void RMD160_Init _((RMD160_CTX *));
void RMD160_Transform _((uint32_t[5], const uint32_t[16]));
void RMD160_Update _((RMD160_CTX *, const uint8_t *, size_t));
void RMD160_Finish _((RMD160_CTX *, uint8_t[20]));
-int RMD160_Equal _((RMD160_CTX *, RMD160_CTX *));
__END_DECLS
#define RMD160_BLOCK_LENGTH 64
diff --git a/ext/digest/rmd160/rmd160init.c b/ext/digest/rmd160/rmd160init.c
index b56211f..585ba05 100644
--- a/ext/digest/rmd160/rmd160init.c
+++ b/ext/digest/rmd160/rmd160init.c
@@ -14,7 +14,6 @@ static algo_t rmd160 = {
(hash_init_func_t)RMD160_Init,
(hash_update_func_t)RMD160_Update,
(hash_finish_func_t)RMD160_Finish,
- (hash_equal_func_t)RMD160_Equal,
};
void
@@ -30,8 +29,11 @@ Init_rmd160()
cDigest_RMD160 = rb_define_class_under(mDigest, "RMD160", cDigest_Base);
+ rb_define_const(cDigest_RMD160, "DIGEST_LENGTH", INT2NUM(RMD160_DIGEST_LENGTH));
+ rb_define_const(cDigest_RMD160, "BLOCK_LENGTH", INT2NUM(RMD160_BLOCK_LENGTH));
+
id_metadata = rb_intern("metadata");
- rb_cvar_set(cDigest_RMD160, id_metadata,
- Data_Wrap_Struct(rb_cObject, 0, 0, &rmd160), Qtrue);
+ rb_ivar_set(cDigest_RMD160, id_metadata,
+ Data_Wrap_Struct(rb_cObject, 0, 0, &rmd160));
}
diff --git a/ext/digest/rmd160/rmd160ossl.c b/ext/digest/rmd160/rmd160ossl.c
index 247fd04..5d8c5ba 100644
--- a/ext/digest/rmd160/rmd160ossl.c
+++ b/ext/digest/rmd160/rmd160ossl.c
@@ -2,21 +2,7 @@
#include "defs.h"
#include "rmd160ossl.h"
-#include <assert.h>
-#include <stdlib.h>
void RMD160_Finish(RMD160_CTX *ctx, char *buf) {
RIPEMD160_Final(buf, ctx);
}
-
-int RMD160_Equal(RMD160_CTX* pctx1, RMD160_CTX* pctx2) {
- return pctx1->num == pctx2->num
- && pctx1->A == pctx2->A
- && pctx1->B == pctx2->B
- && pctx1->C == pctx2->C
- && pctx1->D == pctx2->D
- && pctx1->E == pctx2->E
- && pctx1->Nl == pctx2->Nl
- && pctx1->Nh == pctx2->Nh
- && memcmp(pctx1->data, pctx2->data, sizeof(pctx1->data)) == 0;
-}
diff --git a/ext/digest/rmd160/rmd160ossl.h b/ext/digest/rmd160/rmd160ossl.h
index ea6bd57..3df38a0 100644
--- a/ext/digest/rmd160/rmd160ossl.h
+++ b/ext/digest/rmd160/rmd160ossl.h
@@ -15,6 +15,5 @@
#define RMD160_DIGEST_LENGTH RIPEMD160_DIGEST_LENGTH
void RMD160_Finish(RMD160_CTX *ctx, char *buf);
-int RMD160_Equal(RMD160_CTX *pctx1, RMD160_CTX *pctx2);
#endif
diff --git a/ext/digest/sha1/sha1.c b/ext/digest/sha1/sha1.c
index 6545744..6196ca6 100644
--- a/ext/digest/sha1/sha1.c
+++ b/ext/digest/sha1/sha1.c
@@ -267,9 +267,3 @@ void SHA1_Finish(SHA1_CTX* context, uint8_t digest[20])
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
}
}
-
-int SHA1_Equal(SHA1_CTX* pctx1, SHA1_CTX* pctx2) {
- return memcmp(pctx1->count, pctx2->count, sizeof(pctx1->count)) == 0
- && memcmp(pctx1->state, pctx2->state, sizeof(pctx1->state)) == 0
- && memcmp(pctx1->buffer, pctx2->buffer, sizeof(pctx1->buffer)) == 0;
-}
diff --git a/ext/digest/sha1/sha1.h b/ext/digest/sha1/sha1.h
index c9f8456..60e3b01 100644
--- a/ext/digest/sha1/sha1.h
+++ b/ext/digest/sha1/sha1.h
@@ -25,14 +25,12 @@ typedef struct {
#define SHA1_Init rb_Digest_SHA1_Init
#define SHA1_Update rb_Digest_SHA1_Update
#define SHA1_Finish rb_Digest_SHA1_Finish
-#define SHA1_Equal rb_Digest_SHA1_Equal
#endif
void SHA1_Transform _((uint32_t state[5], const uint8_t buffer[64]));
void SHA1_Init _((SHA1_CTX *context));
void SHA1_Update _((SHA1_CTX *context, const uint8_t *data, size_t len));
void SHA1_Finish _((SHA1_CTX *context, uint8_t digest[20]));
-int SHA1_Equal _((SHA1_CTX *pctx1, SHA1_CTX *pctx2));
#define SHA1_BLOCK_LENGTH 64
#define SHA1_DIGEST_LENGTH 20
diff --git a/ext/digest/sha1/sha1init.c b/ext/digest/sha1/sha1init.c
index a704dcb..e56f18b 100644
--- a/ext/digest/sha1/sha1init.c
+++ b/ext/digest/sha1/sha1init.c
@@ -14,7 +14,6 @@ static algo_t sha1 = {
(hash_init_func_t)SHA1_Init,
(hash_update_func_t)SHA1_Update,
(hash_finish_func_t)SHA1_Finish,
- (hash_equal_func_t)SHA1_Equal,
};
void
@@ -29,6 +28,9 @@ Init_sha1()
cDigest_SHA1 = rb_define_class_under(mDigest, "SHA1", cDigest_Base);
- rb_cvar_set(cDigest_SHA1, rb_intern("metadata"),
- Data_Wrap_Struct(rb_cObject, 0, 0, &sha1), Qtrue);
+ rb_define_const(cDigest_SHA1, "DIGEST_LENGTH", INT2NUM(SHA1_DIGEST_LENGTH));
+ rb_define_const(cDigest_SHA1, "BLOCK_LENGTH", INT2NUM(SHA1_BLOCK_LENGTH));
+
+ rb_ivar_set(cDigest_SHA1, rb_intern("metadata"),
+ Data_Wrap_Struct(rb_cObject, 0, 0, &sha1));
}
diff --git a/ext/digest/sha1/sha1ossl.c b/ext/digest/sha1/sha1ossl.c
index 96365c7..adf5cf2 100644
--- a/ext/digest/sha1/sha1ossl.c
+++ b/ext/digest/sha1/sha1ossl.c
@@ -2,24 +2,9 @@
#include "defs.h"
#include "sha1ossl.h"
-#include <stdlib.h>
void
SHA1_Finish(SHA1_CTX *ctx, char *buf)
{
SHA1_Final(buf, ctx);
}
-
-int
-SHA1_Equal(SHA1_CTX* pctx1, SHA1_CTX* pctx2)
-{
- return pctx1->num == pctx2->num
- && pctx1->h0 == pctx2->h0
- && pctx1->h1 == pctx2->h1
- && pctx1->h2 == pctx2->h2
- && pctx1->h3 == pctx2->h3
- && pctx1->h4 == pctx2->h4
- && pctx1->Nl == pctx2->Nl
- && pctx1->Nh == pctx2->Nh
- && memcmp(pctx1->data, pctx2->data, sizeof(pctx1->data)) == 0;
-}
diff --git a/ext/digest/sha1/sha1ossl.h b/ext/digest/sha1/sha1ossl.h
index c2e19d6..8f9984c 100644
--- a/ext/digest/sha1/sha1ossl.h
+++ b/ext/digest/sha1/sha1ossl.h
@@ -8,10 +8,13 @@
#define SHA1_CTX SHA_CTX
+#ifdef SHA_BLOCK_LENGTH
#define SHA1_BLOCK_LENGTH SHA_BLOCK_LENGTH
+#else
+#define SHA1_BLOCK_LENGTH SHA_CBLOCK
+#endif
#define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
void SHA1_Finish(SHA1_CTX *ctx, char *buf);
-int SHA1_Equal(SHA1_CTX *pctx1, SHA1_CTX *pctx2);
#endif
diff --git a/ext/digest/sha2/sha2.c b/ext/digest/sha2/sha2.c
index 1dd69c4..aca9ee9 100644
--- a/ext/digest/sha2/sha2.c
+++ b/ext/digest/sha2/sha2.c
@@ -578,12 +578,6 @@ void SHA256_Finish(SHA256_CTX* context, sha2_byte digest[]) {
usedspace = 0;
}
-int SHA256_Equal(SHA256_CTX* pctx1, SHA256_CTX* pctx2) {
- return pctx1->bitcount == pctx2->bitcount
- && memcmp(pctx1->state, pctx2->state, sizeof(pctx1->state)) == 0
- && memcmp(pctx1->buffer, pctx2->buffer, sizeof(pctx1->buffer)) == 0;
-}
-
/*** SHA-512: *********************************************************/
void SHA512_Init(SHA512_CTX* context) {
if (context == (SHA512_CTX*)0) {
@@ -881,12 +875,6 @@ void SHA512_Finish(SHA512_CTX* context, sha2_byte digest[]) {
MEMSET_BZERO(context, sizeof(SHA512_CTX));
}
-int SHA512_Equal(SHA512_CTX* pctx1, SHA512_CTX* pctx2) {
- return memcmp(pctx1->bitcount, pctx2->bitcount, sizeof(pctx1->bitcount)) == 0
- && memcmp(pctx1->state, pctx2->state, sizeof(pctx1->state)) == 0
- && memcmp(pctx1->buffer, pctx2->buffer, sizeof(pctx1->buffer)) == 0;
-}
-
/*** SHA-384: *********************************************************/
void SHA384_Init(SHA384_CTX* context) {
if (context == (SHA384_CTX*)0) {
@@ -929,9 +917,3 @@ void SHA384_Finish(SHA384_CTX* context, sha2_byte digest[]) {
/* Zero out state data */
MEMSET_BZERO(context, sizeof(SHA384_CTX));
}
-
-int SHA384_Equal(SHA384_CTX* pctx1, SHA384_CTX* pctx2) {
- return memcmp(pctx1->bitcount, pctx2->bitcount, sizeof(pctx1->bitcount)) == 0
- && memcmp(pctx1->state, pctx2->state, sizeof(pctx1->state)) == 0
- && memcmp(pctx1->buffer, pctx2->buffer, sizeof(pctx1->buffer)) == 0;
-}
diff --git a/ext/digest/sha2/sha2.h b/ext/digest/sha2/sha2.h
index db72cbb..917d415 100644
--- a/ext/digest/sha2/sha2.h
+++ b/ext/digest/sha2/sha2.h
@@ -78,34 +78,28 @@ typedef SHA512_CTX SHA384_CTX;
#define SHA256_Init rb_Digest_SHA256_Init
#define SHA256_Update rb_Digest_SHA256_Update
#define SHA256_Finish rb_Digest_SHA256_Finish
-#define SHA256_Equal rb_Digest_SHA256_Equal
#define SHA384_Init rb_Digest_SHA384_Init
#define SHA384_Update rb_Digest_SHA384_Update
#define SHA384_Finish rb_Digest_SHA384_Finish
-#define SHA384_Equal rb_Digest_SHA384_Equal
#define SHA512_Init rb_Digest_SHA512_Init
#define SHA512_Update rb_Digest_SHA512_Update
#define SHA512_Finish rb_Digest_SHA512_Finish
-#define SHA512_Equal rb_Digest_SHA512_Equal
#endif
/*** SHA-256/384/512 Function Prototypes ******************************/
void SHA256_Init _((SHA256_CTX *));
void SHA256_Update _((SHA256_CTX*, const uint8_t*, size_t));
void SHA256_Finish _((SHA256_CTX*, uint8_t[SHA256_DIGEST_LENGTH]));
-int SHA256_Equal _((SHA256_CTX*, SHA256_CTX*));
void SHA384_Init _((SHA384_CTX*));
void SHA384_Update _((SHA384_CTX*, const uint8_t*, size_t));
void SHA384_Finish _((SHA384_CTX*, uint8_t[SHA384_DIGEST_LENGTH]));
-int SHA384_Equal _((SHA384_CTX*, SHA384_CTX*));
void SHA512_Init _((SHA512_CTX*));
void SHA512_Update _((SHA512_CTX*, const uint8_t*, size_t));
void SHA512_Finish _((SHA512_CTX*, uint8_t[SHA512_DIGEST_LENGTH]));
-int SHA512_Equal _((SHA512_CTX*, SHA512_CTX*));
#ifdef __cplusplus
}
diff --git a/ext/digest/sha2/sha2init.c b/ext/digest/sha2/sha2init.c
index 9484f2a..c5a16ab 100644
--- a/ext/digest/sha2/sha2init.c
+++ b/ext/digest/sha2/sha2init.c
@@ -13,7 +13,6 @@ static algo_t sha##bitlen = { \
(hash_init_func_t)SHA##bitlen##_Init, \
(hash_update_func_t)SHA##bitlen##_Update, \
(hash_finish_func_t)SHA##bitlen##_Finish, \
- (hash_equal_func_t)SHA##bitlen##_Equal, \
};
FOREACH_BITLEN(DEFINE_ALGO_METADATA)
@@ -39,8 +38,11 @@ Init_sha2()
#define DEFINE_ALGO_CLASS(bitlen) \
cDigest_SHA##bitlen = rb_define_class_under(mDigest, "SHA" #bitlen, cDigest_Base); \
\
- rb_cvar_set(cDigest_SHA##bitlen, id_metadata, \
- Data_Wrap_Struct(rb_cObject, 0, 0, &sha##bitlen), Qtrue);
+ rb_define_const(cDigest_SHA##bitlen, "DIGEST_LENGTH", INT2NUM(SHA##bitlen##_DIGEST_LENGTH)); \
+ rb_define_const(cDigest_SHA##bitlen, "BLOCK_LENGTH", INT2NUM(SHA##bitlen##_BLOCK_LENGTH)); \
+\
+ rb_ivar_set(cDigest_SHA##bitlen, id_metadata, \
+ Data_Wrap_Struct(rb_cObject, 0, 0, &sha##bitlen));
FOREACH_BITLEN(DEFINE_ALGO_CLASS)
}