зеркало из https://github.com/github/ruby.git
* ext/digest/digest.c (rb_digest_base_alloc,
rb_digest_base_equal): Simplify the equality check and just compare resulted digests since state-level equality should not be significant. * ext/digest/digest.h: Ditto. * ext/digest/*/*.[ch]: Ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11131 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
76f721470b
Коммит
945a76d97b
11
ChangeLog
11
ChangeLog
|
@ -1,3 +1,14 @@
|
|||
Wed Oct 11 21:36:47 2006 Akinori MUSHA <knu@iDaemons.org>
|
||||
|
||||
* ext/digest/digest.c (rb_digest_base_alloc,
|
||||
rb_digest_base_equal): Simplify the equality check and just
|
||||
compare resulted digests since state-level equality should
|
||||
not be so significant.
|
||||
|
||||
* ext/digest/digest.h: Ditto.
|
||||
|
||||
* ext/digest/*/*.[ch]: Ditto.
|
||||
|
||||
Wed Oct 11 17:11:03 2006 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||
|
||||
* eval.c (rb_obj_define_method): add half boiled RDoc document.
|
||||
|
|
|
@ -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);
|
||||
|
@ -208,7 +207,7 @@ rb_digest_base_copy(VALUE copy, VALUE obj)
|
|||
algo = get_digest_base_metadata(rb_obj_class(copy));
|
||||
|
||||
if (algo == NULL) {
|
||||
/* subclasses must define initialize_copy() */
|
||||
/* initialize_copy() is undefined or something */
|
||||
rb_notimplement();
|
||||
}
|
||||
|
||||
|
@ -375,25 +374,22 @@ 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;
|
||||
str1 = rb_digest_base_digest(self);
|
||||
str2 = rb_digest_base_digest(other);
|
||||
} else {
|
||||
StringValue(other);
|
||||
str2 = other;
|
||||
|
||||
Data_Get_Struct(self, void, pctx1);
|
||||
Data_Get_Struct(other, void, pctx2);
|
||||
algo = get_digest_base_metadata(klass);
|
||||
|
||||
return algo->equal_func(pctx1, pctx2) ? Qtrue : Qfalse;
|
||||
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)
|
||||
return Qtrue;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -9,6 +9,5 @@
|
|||
#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
|
||||
|
|
|
@ -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 **********************/
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -16,6 +16,5 @@
|
|||
#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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Загрузка…
Ссылка в новой задаче