2001-07-13 16:06:14 -04:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
digest.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Fri May 25 08:57:27 JST 2001
|
|
|
|
|
|
|
|
Copyright (C) 1995-2001 Yukihiro Matsumoto
|
2006-10-05 15:13:00 -04:00
|
|
|
Copyright (C) 2001-2006 Akinori MUSHA
|
2001-07-13 16:06:14 -04:00
|
|
|
|
|
|
|
$RoughId: digest.c,v 1.16 2001/07/13 15:38:27 knu Exp $
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "digest.h"
|
|
|
|
|
|
|
|
static VALUE mDigest, cDigest_Base;
|
2006-10-11 02:05:32 -04:00
|
|
|
static ID id_metadata, id_new, id_initialize, id_update, id_digest;
|
2001-07-13 16:06:14 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Digest::Base
|
|
|
|
*/
|
|
|
|
|
|
|
|
static algo_t *
|
2005-12-11 19:36:54 -05:00
|
|
|
get_digest_base_metadata(VALUE klass)
|
2001-07-13 16:06:14 -04:00
|
|
|
{
|
|
|
|
VALUE obj;
|
|
|
|
algo_t *algo;
|
|
|
|
|
2002-09-03 01:20:14 -04:00
|
|
|
if (rb_cvar_defined(klass, id_metadata) == Qfalse) {
|
2006-10-05 13:39:51 -04:00
|
|
|
return NULL;
|
2002-09-03 01:20:14 -04:00
|
|
|
}
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2002-09-03 01:20:14 -04:00
|
|
|
obj = rb_cvar_get(klass, id_metadata);
|
2001-07-13 16:06:14 -04:00
|
|
|
|
|
|
|
Data_Get_Struct(obj, algo_t, algo);
|
|
|
|
|
|
|
|
return algo;
|
|
|
|
}
|
* array.c (ary_alloc), dir.c (dir_s_alloc), eval.c (thgroup_s_alloc),
file.c (rb_stat_s_alloc), hash.c (hash_alloc), io.c (io_alloc),
object.c (rb_module_s_alloc, rb_class_allocate_instance),
re.c (match_alloc, rb_reg_s_alloc), string.c (str_alloc),
time.c (time_s_alloc), ext/digest/digest.c (rb_digest_base_alloc),
ext/tcltklib/tcltklib.c (ip_alloc),
ext/win32ole/win32ole.c (fole_s_allocate, fev_s_allocate)
: add prototype to get rid of VC++ warnings.
* ext/sdbm/init.c (fsdbm_alloc): allocator takes only one argument.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-21 13:02:01 -05:00
|
|
|
|
* ext/digest/digest.[ch]: Since the argument order of
hash_final_func_t was inconsistent with others, change it and
rename to hash_finish_func_t to avoid confusion.
* ext/digest/digest.[ch]: Remove and eliminate the use of
hash_end_func_t. Implement hexdigest conversion in the base
class.
* ext/digest/md5/md5.c, ext/digest/md5/md5.h,
ext/digest/md5/md5init.c, ext/digest/md5/md5ossl.c,
ext/digest/md5/md5ossl.h: Remove MD5_End() and change
MD5_Final() to MD5_Finish().
* ext/digest/rmd160/depend, ext/digest/rmd160/extconf.rb,
ext/digest/rmd160/rmd160.c, ext/digest/rmd160/rmd160.h,
ext/digest/rmd160/rmd160hl.c, ext/digest/rmd160/rmd160init.c,
ext/digest/rmd160/rmd160ossl.c, ext/digest/rmd160/rmd160ossl.h:
Remove unused functions RMD160_End(), RMD160_File(),
RMD160_Data() and change RMD160_Final() to RMD160_Finish().
* ext/digest/sha1/extconf.rb, ext/digest/sha1/sha1.c,
ext/digest/sha1/sha1.h, ext/digest/sha1/sha1hl.c,
ext/digest/sha1/sha1init.c, ext/digest/sha1/sha1ossl.c,
ext/digest/sha1/sha1ossl.h: Likewise.
* ext/digest/sha2/extconf.rb, ext/digest/sha2/sha2.c,
ext/digest/sha2/sha2.h, ext/digest/sha2/sha2hl.c,
ext/digest/sha2/sha2init.c: Likewise.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11086 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-10-05 07:09:42 -04:00
|
|
|
static VALUE
|
2006-10-05 15:13:00 -04:00
|
|
|
hexdigest_str_new(VALUE str_digest)
|
* ext/digest/digest.[ch]: Since the argument order of
hash_final_func_t was inconsistent with others, change it and
rename to hash_finish_func_t to avoid confusion.
* ext/digest/digest.[ch]: Remove and eliminate the use of
hash_end_func_t. Implement hexdigest conversion in the base
class.
* ext/digest/md5/md5.c, ext/digest/md5/md5.h,
ext/digest/md5/md5init.c, ext/digest/md5/md5ossl.c,
ext/digest/md5/md5ossl.h: Remove MD5_End() and change
MD5_Final() to MD5_Finish().
* ext/digest/rmd160/depend, ext/digest/rmd160/extconf.rb,
ext/digest/rmd160/rmd160.c, ext/digest/rmd160/rmd160.h,
ext/digest/rmd160/rmd160hl.c, ext/digest/rmd160/rmd160init.c,
ext/digest/rmd160/rmd160ossl.c, ext/digest/rmd160/rmd160ossl.h:
Remove unused functions RMD160_End(), RMD160_File(),
RMD160_Data() and change RMD160_Final() to RMD160_Finish().
* ext/digest/sha1/extconf.rb, ext/digest/sha1/sha1.c,
ext/digest/sha1/sha1.h, ext/digest/sha1/sha1hl.c,
ext/digest/sha1/sha1init.c, ext/digest/sha1/sha1ossl.c,
ext/digest/sha1/sha1ossl.h: Likewise.
* ext/digest/sha2/extconf.rb, ext/digest/sha2/sha2.c,
ext/digest/sha2/sha2.h, ext/digest/sha2/sha2hl.c,
ext/digest/sha2/sha2init.c: Likewise.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11086 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-10-05 07:09:42 -04:00
|
|
|
{
|
2006-10-10 03:49:00 -04:00
|
|
|
char *digest;
|
|
|
|
size_t digest_len;
|
* ext/digest/digest.[ch]: Since the argument order of
hash_final_func_t was inconsistent with others, change it and
rename to hash_finish_func_t to avoid confusion.
* ext/digest/digest.[ch]: Remove and eliminate the use of
hash_end_func_t. Implement hexdigest conversion in the base
class.
* ext/digest/md5/md5.c, ext/digest/md5/md5.h,
ext/digest/md5/md5init.c, ext/digest/md5/md5ossl.c,
ext/digest/md5/md5ossl.h: Remove MD5_End() and change
MD5_Final() to MD5_Finish().
* ext/digest/rmd160/depend, ext/digest/rmd160/extconf.rb,
ext/digest/rmd160/rmd160.c, ext/digest/rmd160/rmd160.h,
ext/digest/rmd160/rmd160hl.c, ext/digest/rmd160/rmd160init.c,
ext/digest/rmd160/rmd160ossl.c, ext/digest/rmd160/rmd160ossl.h:
Remove unused functions RMD160_End(), RMD160_File(),
RMD160_Data() and change RMD160_Final() to RMD160_Finish().
* ext/digest/sha1/extconf.rb, ext/digest/sha1/sha1.c,
ext/digest/sha1/sha1.h, ext/digest/sha1/sha1hl.c,
ext/digest/sha1/sha1init.c, ext/digest/sha1/sha1ossl.c,
ext/digest/sha1/sha1ossl.h: Likewise.
* ext/digest/sha2/extconf.rb, ext/digest/sha2/sha2.c,
ext/digest/sha2/sha2.h, ext/digest/sha2/sha2hl.c,
ext/digest/sha2/sha2init.c: Likewise.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11086 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-10-05 07:09:42 -04:00
|
|
|
int i;
|
|
|
|
VALUE str;
|
|
|
|
char *p;
|
2006-10-05 13:07:59 -04:00
|
|
|
static const char hex[] = {
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
|
|
'a', 'b', 'c', 'd', 'e', 'f'
|
|
|
|
};
|
|
|
|
|
2006-10-10 03:49:00 -04:00
|
|
|
StringValue(str_digest);
|
|
|
|
digest = RSTRING_PTR(str_digest);
|
|
|
|
digest_len = RSTRING_LEN(str_digest);
|
|
|
|
|
2006-10-05 13:07:59 -04:00
|
|
|
if (LONG_MAX / 2 < digest_len) {
|
|
|
|
rb_raise(rb_eRuntimeError, "digest string too long");
|
|
|
|
}
|
* ext/digest/digest.[ch]: Since the argument order of
hash_final_func_t was inconsistent with others, change it and
rename to hash_finish_func_t to avoid confusion.
* ext/digest/digest.[ch]: Remove and eliminate the use of
hash_end_func_t. Implement hexdigest conversion in the base
class.
* ext/digest/md5/md5.c, ext/digest/md5/md5.h,
ext/digest/md5/md5init.c, ext/digest/md5/md5ossl.c,
ext/digest/md5/md5ossl.h: Remove MD5_End() and change
MD5_Final() to MD5_Finish().
* ext/digest/rmd160/depend, ext/digest/rmd160/extconf.rb,
ext/digest/rmd160/rmd160.c, ext/digest/rmd160/rmd160.h,
ext/digest/rmd160/rmd160hl.c, ext/digest/rmd160/rmd160init.c,
ext/digest/rmd160/rmd160ossl.c, ext/digest/rmd160/rmd160ossl.h:
Remove unused functions RMD160_End(), RMD160_File(),
RMD160_Data() and change RMD160_Final() to RMD160_Finish().
* ext/digest/sha1/extconf.rb, ext/digest/sha1/sha1.c,
ext/digest/sha1/sha1.h, ext/digest/sha1/sha1hl.c,
ext/digest/sha1/sha1init.c, ext/digest/sha1/sha1ossl.c,
ext/digest/sha1/sha1ossl.h: Likewise.
* ext/digest/sha2/extconf.rb, ext/digest/sha2/sha2.c,
ext/digest/sha2/sha2.h, ext/digest/sha2/sha2hl.c,
ext/digest/sha2/sha2init.c: Likewise.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11086 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-10-05 07:09:42 -04:00
|
|
|
|
|
|
|
str = rb_str_new(0, digest_len * 2);
|
|
|
|
|
|
|
|
for (i = 0, p = RSTRING_PTR(str); i < digest_len; i++) {
|
|
|
|
unsigned char byte = digest[i];
|
|
|
|
|
|
|
|
p[i + i] = hex[byte >> 4];
|
|
|
|
p[i + i + 1] = hex[byte & 0x0f];
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2006-10-05 13:39:51 -04:00
|
|
|
static VALUE
|
2006-10-05 15:13:00 -04:00
|
|
|
bubblebabble_str_new(VALUE str_digest)
|
2006-10-05 13:39:51 -04:00
|
|
|
{
|
2006-10-10 03:49:00 -04:00
|
|
|
char *digest;
|
|
|
|
size_t digest_len;
|
2006-10-05 15:13:00 -04:00
|
|
|
VALUE str;
|
|
|
|
char *p;
|
|
|
|
int i, j, seed = 1;
|
|
|
|
static const char vowels[] = {
|
|
|
|
'a', 'e', 'i', 'o', 'u', 'y'
|
|
|
|
};
|
|
|
|
static const char consonants[] = {
|
|
|
|
'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm', 'n',
|
|
|
|
'p', 'r', 's', 't', 'v', 'z', 'x'
|
|
|
|
};
|
|
|
|
|
2006-10-10 03:49:00 -04:00
|
|
|
StringValue(str_digest);
|
|
|
|
digest = RSTRING_PTR(str_digest);
|
|
|
|
digest_len = RSTRING_LEN(str_digest);
|
|
|
|
|
2006-10-05 15:13:00 -04:00
|
|
|
if ((LONG_MAX - 2) / 3 < (digest_len | 1)) {
|
|
|
|
rb_raise(rb_eRuntimeError, "digest string too long");
|
|
|
|
}
|
|
|
|
|
|
|
|
str = rb_str_new(0, (digest_len | 1) * 3 + 2);
|
|
|
|
p = RSTRING_PTR(str);
|
|
|
|
|
|
|
|
i = j = 0;
|
|
|
|
p[j++] = 'x';
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
unsigned char byte1, byte2;
|
|
|
|
|
|
|
|
if (i >= digest_len) {
|
|
|
|
p[j++] = vowels[seed % 6];
|
|
|
|
p[j++] = consonants[16];
|
|
|
|
p[j++] = vowels[seed / 6];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte1 = digest[i++];
|
|
|
|
p[j++] = vowels[(((byte1 >> 6) & 3) + seed) % 6];
|
|
|
|
p[j++] = consonants[(byte1 >> 2) & 15];
|
|
|
|
p[j++] = vowels[((byte1 & 3) + (seed / 6)) % 6];
|
|
|
|
|
|
|
|
if (i >= digest_len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte2 = digest[i++];
|
|
|
|
p[j++] = consonants[(byte2 >> 4) & 15];
|
|
|
|
p[j++] = '-';
|
|
|
|
p[j++] = consonants[byte2 & 15];
|
|
|
|
|
|
|
|
seed = (seed * 5 + byte1 * 7 + byte2) % 36;
|
|
|
|
}
|
|
|
|
|
|
|
|
p[j] = 'x';
|
|
|
|
|
|
|
|
return str;
|
2006-10-05 13:39:51 -04:00
|
|
|
}
|
|
|
|
|
2001-07-13 16:06:14 -04:00
|
|
|
static VALUE
|
2005-12-11 19:36:54 -05:00
|
|
|
rb_digest_base_alloc(VALUE klass)
|
2001-07-13 16:06:14 -04:00
|
|
|
{
|
|
|
|
algo_t *algo;
|
|
|
|
VALUE obj;
|
|
|
|
void *pctx;
|
|
|
|
|
2002-09-03 01:20:14 -04:00
|
|
|
if (klass == cDigest_Base) {
|
2001-07-13 16:06:14 -04:00
|
|
|
rb_raise(rb_eNotImpError, "Digest::Base is an abstract class");
|
2002-09-03 01:20:14 -04:00
|
|
|
}
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2002-09-03 01:20:14 -04:00
|
|
|
algo = get_digest_base_metadata(klass);
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2006-10-05 13:39:51 -04:00
|
|
|
if (algo == NULL) {
|
|
|
|
return Data_Wrap_Struct(klass, 0, free, 0);
|
|
|
|
}
|
|
|
|
|
2006-10-11 08:43:58 -04:00
|
|
|
pctx = xmalloc(algo->ctx_size);
|
2001-07-13 16:06:14 -04:00
|
|
|
algo->init_func(pctx);
|
|
|
|
|
2002-09-03 01:20:14 -04:00
|
|
|
obj = Data_Wrap_Struct(klass, 0, free, pctx);
|
2001-07-13 16:06:14 -04:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2005-12-11 19:36:54 -05:00
|
|
|
rb_digest_base_s_digest(VALUE klass, VALUE str)
|
2001-07-13 16:06:14 -04:00
|
|
|
{
|
2006-10-05 13:39:51 -04:00
|
|
|
algo_t *algo = get_digest_base_metadata(klass);
|
2001-07-13 16:06:14 -04:00
|
|
|
void *pctx;
|
2006-10-05 13:39:51 -04:00
|
|
|
volatile VALUE obj;
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2006-10-05 13:39:51 -04:00
|
|
|
if (algo == NULL) {
|
|
|
|
VALUE obj = rb_funcall(klass, id_new, 0);
|
|
|
|
rb_funcall(obj, id_update, 1, str);
|
|
|
|
return rb_funcall(obj, id_digest, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = rb_digest_base_alloc(klass);
|
2002-09-03 01:20:14 -04:00
|
|
|
Data_Get_Struct(obj, void, pctx);
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2001-09-06 01:02:29 -04:00
|
|
|
StringValue(str);
|
2006-08-31 06:30:33 -04:00
|
|
|
algo->update_func(pctx, RSTRING_PTR(str), RSTRING_LEN(str));
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2005-12-11 19:36:54 -05:00
|
|
|
str = rb_str_new(0, algo->digest_len);
|
* ext/digest/digest.[ch]: Since the argument order of
hash_final_func_t was inconsistent with others, change it and
rename to hash_finish_func_t to avoid confusion.
* ext/digest/digest.[ch]: Remove and eliminate the use of
hash_end_func_t. Implement hexdigest conversion in the base
class.
* ext/digest/md5/md5.c, ext/digest/md5/md5.h,
ext/digest/md5/md5init.c, ext/digest/md5/md5ossl.c,
ext/digest/md5/md5ossl.h: Remove MD5_End() and change
MD5_Final() to MD5_Finish().
* ext/digest/rmd160/depend, ext/digest/rmd160/extconf.rb,
ext/digest/rmd160/rmd160.c, ext/digest/rmd160/rmd160.h,
ext/digest/rmd160/rmd160hl.c, ext/digest/rmd160/rmd160init.c,
ext/digest/rmd160/rmd160ossl.c, ext/digest/rmd160/rmd160ossl.h:
Remove unused functions RMD160_End(), RMD160_File(),
RMD160_Data() and change RMD160_Final() to RMD160_Finish().
* ext/digest/sha1/extconf.rb, ext/digest/sha1/sha1.c,
ext/digest/sha1/sha1.h, ext/digest/sha1/sha1hl.c,
ext/digest/sha1/sha1init.c, ext/digest/sha1/sha1ossl.c,
ext/digest/sha1/sha1ossl.h: Likewise.
* ext/digest/sha2/extconf.rb, ext/digest/sha2/sha2.c,
ext/digest/sha2/sha2.h, ext/digest/sha2/sha2hl.c,
ext/digest/sha2/sha2init.c: Likewise.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11086 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-10-05 07:09:42 -04:00
|
|
|
algo->finish_func(pctx, RSTRING_PTR(str));
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2005-12-11 19:36:54 -05:00
|
|
|
return str;
|
2001-07-13 16:06:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2005-12-11 19:36:54 -05:00
|
|
|
rb_digest_base_s_hexdigest(VALUE klass, VALUE str)
|
2001-07-13 16:06:14 -04:00
|
|
|
{
|
2006-10-05 15:13:00 -04:00
|
|
|
return hexdigest_str_new(rb_funcall(klass, id_digest, 1, str));
|
|
|
|
}
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2006-10-05 15:13:00 -04:00
|
|
|
static VALUE
|
|
|
|
rb_digest_base_s_bubblebabble(VALUE klass, VALUE str)
|
|
|
|
{
|
|
|
|
return bubblebabble_str_new(rb_funcall(klass, id_digest, 1, str));
|
2001-07-13 16:06:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2005-12-11 19:36:54 -05:00
|
|
|
rb_digest_base_copy(VALUE copy, VALUE obj)
|
2001-07-13 16:06:14 -04:00
|
|
|
{
|
|
|
|
algo_t *algo;
|
|
|
|
void *pctx1, *pctx2;
|
2002-09-03 01:20:14 -04:00
|
|
|
|
2002-09-04 22:11:41 -04:00
|
|
|
if (copy == obj) return copy;
|
2002-09-03 01:20:14 -04:00
|
|
|
rb_check_frozen(copy);
|
2004-09-17 05:24:13 -04:00
|
|
|
algo = get_digest_base_metadata(rb_obj_class(copy));
|
2006-10-05 13:39:51 -04:00
|
|
|
|
2006-10-11 02:05:32 -04:00
|
|
|
if (algo == NULL) {
|
2006-10-11 08:43:58 -04:00
|
|
|
/* initialize_copy() is undefined or something */
|
2006-10-05 13:39:51 -04:00
|
|
|
rb_notimplement();
|
2006-10-11 02:05:32 -04:00
|
|
|
}
|
2006-10-05 13:39:51 -04:00
|
|
|
|
|
|
|
/* get_digest_base_metadata() may return a NULL */
|
2004-09-17 05:24:13 -04:00
|
|
|
if (algo != get_digest_base_metadata(rb_obj_class(obj))) {
|
2002-09-03 01:20:14 -04:00
|
|
|
rb_raise(rb_eTypeError, "wrong argument class");
|
|
|
|
}
|
2002-09-04 05:21:07 -04:00
|
|
|
Data_Get_Struct(obj, void, pctx1);
|
2002-09-03 01:20:14 -04:00
|
|
|
Data_Get_Struct(copy, void, pctx2);
|
2001-07-13 16:06:14 -04:00
|
|
|
memcpy(pctx2, pctx1, algo->ctx_size);
|
|
|
|
|
2002-09-03 01:20:14 -04:00
|
|
|
return copy;
|
2001-07-13 16:06:14 -04:00
|
|
|
}
|
|
|
|
|
2006-10-11 01:15:15 -04:00
|
|
|
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) {
|
2006-10-11 02:05:32 -04:00
|
|
|
rb_funcall(self, id_initialize, 0);
|
2006-10-11 01:15:15 -04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
Data_Get_Struct(self, void, pctx);
|
|
|
|
|
|
|
|
memset(pctx, 0, algo->ctx_size);
|
|
|
|
algo->init_func(pctx);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2001-07-13 16:06:14 -04:00
|
|
|
static VALUE
|
2005-12-11 19:36:54 -05:00
|
|
|
rb_digest_base_update(VALUE self, VALUE str)
|
2001-07-13 16:06:14 -04:00
|
|
|
{
|
|
|
|
algo_t *algo;
|
|
|
|
void *pctx;
|
|
|
|
|
2006-10-05 13:39:51 -04:00
|
|
|
algo = get_digest_base_metadata(rb_obj_class(self));
|
|
|
|
|
|
|
|
if (algo == NULL) {
|
|
|
|
/* subclasses must define update() */
|
|
|
|
rb_notimplement();
|
|
|
|
}
|
|
|
|
|
|
|
|
Data_Get_Struct(self, void, pctx);
|
|
|
|
|
2001-07-13 16:06:14 -04:00
|
|
|
StringValue(str);
|
2006-10-05 13:39:51 -04:00
|
|
|
algo->update_func(pctx, RSTRING_PTR(str), RSTRING_LEN(str));
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_digest_base_lshift(VALUE self, VALUE str)
|
|
|
|
{
|
|
|
|
algo_t *algo;
|
|
|
|
void *pctx;
|
|
|
|
|
2004-09-17 05:24:13 -04:00
|
|
|
algo = get_digest_base_metadata(rb_obj_class(self));
|
2006-10-05 13:39:51 -04:00
|
|
|
|
|
|
|
if (algo == NULL) {
|
|
|
|
/* subclasses just need to define update(), not << */
|
|
|
|
rb_funcall(self, id_update, 1, str);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2001-07-13 16:06:14 -04:00
|
|
|
Data_Get_Struct(self, void, pctx);
|
|
|
|
|
2006-10-05 13:39:51 -04:00
|
|
|
StringValue(str);
|
2006-08-31 06:30:33 -04:00
|
|
|
algo->update_func(pctx, RSTRING_PTR(str), RSTRING_LEN(str));
|
2001-07-13 16:06:14 -04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2005-12-11 19:36:54 -05:00
|
|
|
rb_digest_base_init(int argc, VALUE *argv, VALUE self)
|
2001-07-13 16:06:14 -04:00
|
|
|
{
|
|
|
|
VALUE arg;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &arg);
|
|
|
|
|
|
|
|
if (!NIL_P(arg)) rb_digest_base_update(self, arg);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2005-12-11 19:36:54 -05:00
|
|
|
rb_digest_base_digest(VALUE self)
|
2001-07-13 16:06:14 -04:00
|
|
|
{
|
|
|
|
algo_t *algo;
|
|
|
|
void *pctx1, *pctx2;
|
* ext/digest/digest.[ch]: Since the argument order of
hash_final_func_t was inconsistent with others, change it and
rename to hash_finish_func_t to avoid confusion.
* ext/digest/digest.[ch]: Remove and eliminate the use of
hash_end_func_t. Implement hexdigest conversion in the base
class.
* ext/digest/md5/md5.c, ext/digest/md5/md5.h,
ext/digest/md5/md5init.c, ext/digest/md5/md5ossl.c,
ext/digest/md5/md5ossl.h: Remove MD5_End() and change
MD5_Final() to MD5_Finish().
* ext/digest/rmd160/depend, ext/digest/rmd160/extconf.rb,
ext/digest/rmd160/rmd160.c, ext/digest/rmd160/rmd160.h,
ext/digest/rmd160/rmd160hl.c, ext/digest/rmd160/rmd160init.c,
ext/digest/rmd160/rmd160ossl.c, ext/digest/rmd160/rmd160ossl.h:
Remove unused functions RMD160_End(), RMD160_File(),
RMD160_Data() and change RMD160_Final() to RMD160_Finish().
* ext/digest/sha1/extconf.rb, ext/digest/sha1/sha1.c,
ext/digest/sha1/sha1.h, ext/digest/sha1/sha1hl.c,
ext/digest/sha1/sha1init.c, ext/digest/sha1/sha1ossl.c,
ext/digest/sha1/sha1ossl.h: Likewise.
* ext/digest/sha2/extconf.rb, ext/digest/sha2/sha2.c,
ext/digest/sha2/sha2.h, ext/digest/sha2/sha2hl.c,
ext/digest/sha2/sha2init.c: Likewise.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11086 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-10-05 07:09:42 -04:00
|
|
|
size_t ctx_size;
|
2001-07-13 16:06:14 -04:00
|
|
|
VALUE str;
|
|
|
|
|
2004-09-17 05:24:13 -04:00
|
|
|
algo = get_digest_base_metadata(rb_obj_class(self));
|
2006-10-05 13:39:51 -04:00
|
|
|
|
2006-10-11 02:05:32 -04:00
|
|
|
if (algo == NULL) {
|
|
|
|
/* subclasses must define update() */
|
2006-10-05 13:39:51 -04:00
|
|
|
rb_notimplement();
|
2006-10-11 02:05:32 -04:00
|
|
|
}
|
2006-10-05 13:39:51 -04:00
|
|
|
|
2001-07-13 16:06:14 -04:00
|
|
|
Data_Get_Struct(self, void, pctx1);
|
|
|
|
|
* ext/digest/digest.[ch]: Since the argument order of
hash_final_func_t was inconsistent with others, change it and
rename to hash_finish_func_t to avoid confusion.
* ext/digest/digest.[ch]: Remove and eliminate the use of
hash_end_func_t. Implement hexdigest conversion in the base
class.
* ext/digest/md5/md5.c, ext/digest/md5/md5.h,
ext/digest/md5/md5init.c, ext/digest/md5/md5ossl.c,
ext/digest/md5/md5ossl.h: Remove MD5_End() and change
MD5_Final() to MD5_Finish().
* ext/digest/rmd160/depend, ext/digest/rmd160/extconf.rb,
ext/digest/rmd160/rmd160.c, ext/digest/rmd160/rmd160.h,
ext/digest/rmd160/rmd160hl.c, ext/digest/rmd160/rmd160init.c,
ext/digest/rmd160/rmd160ossl.c, ext/digest/rmd160/rmd160ossl.h:
Remove unused functions RMD160_End(), RMD160_File(),
RMD160_Data() and change RMD160_Final() to RMD160_Finish().
* ext/digest/sha1/extconf.rb, ext/digest/sha1/sha1.c,
ext/digest/sha1/sha1.h, ext/digest/sha1/sha1hl.c,
ext/digest/sha1/sha1init.c, ext/digest/sha1/sha1ossl.c,
ext/digest/sha1/sha1ossl.h: Likewise.
* ext/digest/sha2/extconf.rb, ext/digest/sha2/sha2.c,
ext/digest/sha2/sha2.h, ext/digest/sha2/sha2hl.c,
ext/digest/sha2/sha2init.c: Likewise.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11086 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-10-05 07:09:42 -04:00
|
|
|
ctx_size = algo->ctx_size;
|
|
|
|
pctx2 = xmalloc(ctx_size);
|
|
|
|
memcpy(pctx2, pctx1, ctx_size);
|
2001-07-13 16:06:14 -04:00
|
|
|
|
* ext/digest/digest.[ch]: Since the argument order of
hash_final_func_t was inconsistent with others, change it and
rename to hash_finish_func_t to avoid confusion.
* ext/digest/digest.[ch]: Remove and eliminate the use of
hash_end_func_t. Implement hexdigest conversion in the base
class.
* ext/digest/md5/md5.c, ext/digest/md5/md5.h,
ext/digest/md5/md5init.c, ext/digest/md5/md5ossl.c,
ext/digest/md5/md5ossl.h: Remove MD5_End() and change
MD5_Final() to MD5_Finish().
* ext/digest/rmd160/depend, ext/digest/rmd160/extconf.rb,
ext/digest/rmd160/rmd160.c, ext/digest/rmd160/rmd160.h,
ext/digest/rmd160/rmd160hl.c, ext/digest/rmd160/rmd160init.c,
ext/digest/rmd160/rmd160ossl.c, ext/digest/rmd160/rmd160ossl.h:
Remove unused functions RMD160_End(), RMD160_File(),
RMD160_Data() and change RMD160_Final() to RMD160_Finish().
* ext/digest/sha1/extconf.rb, ext/digest/sha1/sha1.c,
ext/digest/sha1/sha1.h, ext/digest/sha1/sha1hl.c,
ext/digest/sha1/sha1init.c, ext/digest/sha1/sha1ossl.c,
ext/digest/sha1/sha1ossl.h: Likewise.
* ext/digest/sha2/extconf.rb, ext/digest/sha2/sha2.c,
ext/digest/sha2/sha2.h, ext/digest/sha2/sha2hl.c,
ext/digest/sha2/sha2init.c: Likewise.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11086 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-10-05 07:09:42 -04:00
|
|
|
str = rb_str_new(0, algo->digest_len);
|
|
|
|
algo->finish_func(pctx2, RSTRING_PTR(str));
|
2001-07-13 16:06:14 -04:00
|
|
|
free(pctx2);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2005-12-11 19:36:54 -05:00
|
|
|
rb_digest_base_hexdigest(VALUE self)
|
2001-07-13 16:06:14 -04:00
|
|
|
{
|
2006-10-05 15:13:00 -04:00
|
|
|
return hexdigest_str_new(rb_funcall(self, id_digest, 0));
|
|
|
|
}
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2006-10-05 15:13:00 -04:00
|
|
|
static VALUE
|
|
|
|
rb_digest_base_bubblebabble(VALUE self)
|
|
|
|
{
|
|
|
|
return bubblebabble_str_new(rb_funcall(self, id_digest, 0));
|
2001-07-13 16:06:14 -04:00
|
|
|
}
|
|
|
|
|
2006-09-13 19:32:12 -04:00
|
|
|
static VALUE
|
|
|
|
rb_digest_base_inspect(VALUE self)
|
|
|
|
{
|
|
|
|
algo_t *algo;
|
2006-10-05 13:39:51 -04:00
|
|
|
VALUE klass, str;
|
|
|
|
size_t digest_len = 32; /* no need to be just the right size */
|
2006-09-13 19:32:12 -04:00
|
|
|
char *cname;
|
|
|
|
|
2006-10-05 13:39:51 -04:00
|
|
|
klass = rb_obj_class(self);
|
|
|
|
algo = get_digest_base_metadata(klass);
|
|
|
|
|
|
|
|
if (algo != NULL)
|
|
|
|
digest_len = algo->digest_len;
|
2006-09-13 19:32:12 -04:00
|
|
|
|
|
|
|
cname = rb_obj_classname(self);
|
|
|
|
|
|
|
|
/* #<Digest::Alg: xxxxx...xxxx> */
|
2006-10-05 13:39:51 -04:00
|
|
|
str = rb_str_buf_new(2 + strlen(cname) + 2 + digest_len * 2 + 1);
|
2006-09-13 19:32:12 -04:00
|
|
|
rb_str_buf_cat2(str, "#<");
|
|
|
|
rb_str_buf_cat2(str, cname);
|
|
|
|
rb_str_buf_cat2(str, ": ");
|
|
|
|
rb_str_buf_append(str, rb_digest_base_hexdigest(self));
|
|
|
|
rb_str_buf_cat2(str, ">");
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2001-07-13 16:06:14 -04:00
|
|
|
static VALUE
|
2005-12-11 19:36:54 -05:00
|
|
|
rb_digest_base_equal(VALUE self, VALUE other)
|
2001-07-13 16:06:14 -04:00
|
|
|
{
|
|
|
|
algo_t *algo;
|
2002-09-03 01:20:14 -04:00
|
|
|
VALUE klass;
|
2001-07-13 16:06:14 -04:00
|
|
|
VALUE str1, str2;
|
|
|
|
|
2004-09-17 05:24:13 -04:00
|
|
|
klass = rb_obj_class(self);
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2004-09-17 05:24:13 -04:00
|
|
|
if (rb_obj_class(other) == klass) {
|
2006-10-11 08:43:58 -04:00
|
|
|
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);
|
2001-07-13 16:06:14 -04:00
|
|
|
}
|
|
|
|
|
2006-08-31 06:30:33 -04:00
|
|
|
if (RSTRING_LEN(str1) == RSTRING_LEN(str2)
|
2005-12-11 19:36:54 -05:00
|
|
|
&& rb_str_cmp(str1, str2) == 0)
|
2001-07-13 16:06:14 -04:00
|
|
|
return Qtrue;
|
|
|
|
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-08-27 19:03:20 -04:00
|
|
|
* This module provides an interface to the following hash algorithms:
|
|
|
|
*
|
|
|
|
* - the MD5 Message-Digest Algorithm by the RSA Data Security,
|
|
|
|
* Inc., described in RFC 1321
|
|
|
|
*
|
|
|
|
* - the SHA-1 Secure Hash Algorithm by NIST (the US' National
|
|
|
|
* Institute of Standards and Technology), described in FIPS PUB
|
|
|
|
* 180-1.
|
|
|
|
*
|
|
|
|
* - the SHA-256/384/512 Secure Hash Algorithm by NIST (the US'
|
|
|
|
* National Institute of Standards and Technology), described in
|
|
|
|
* FIPS PUB 180-2.
|
|
|
|
*
|
|
|
|
* - the RIPEMD-160 cryptographic hash function, designed by Hans
|
|
|
|
* Dobbertin, Antoon Bosselaers, and Bart Preneel.
|
2001-07-13 16:06:14 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2006-08-27 22:31:58 -04:00
|
|
|
Init_digest(void)
|
2001-07-13 16:06:14 -04:00
|
|
|
{
|
|
|
|
mDigest = rb_define_module("Digest");
|
|
|
|
|
|
|
|
cDigest_Base = rb_define_class_under(mDigest, "Base", rb_cObject);
|
|
|
|
|
* ext/curses/curses.c, ext/digest/digest.c, ext/dl/handle.c,
ext/dl/ptr.c, ext/dl/sym.c, ext/gdbm/gdbm.c, ext/iconv/iconv.c,
ext/stringio/stringio.c, ext/strscan/strscan.c,
ext/tcltklib/tcltklib.c, ext/win32ole/win32ole.c:
use rb_define_alloc_func().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3193 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-20 06:23:40 -05:00
|
|
|
rb_define_alloc_func(cDigest_Base, rb_digest_base_alloc);
|
2001-07-13 16:06:14 -04:00
|
|
|
rb_define_singleton_method(cDigest_Base, "digest", rb_digest_base_s_digest, 1);
|
|
|
|
rb_define_singleton_method(cDigest_Base, "hexdigest", rb_digest_base_s_hexdigest, 1);
|
2006-10-05 15:13:00 -04:00
|
|
|
rb_define_singleton_method(cDigest_Base, "bubblebabble", rb_digest_base_s_bubblebabble, 1);
|
2001-07-13 16:06:14 -04:00
|
|
|
|
|
|
|
rb_define_method(cDigest_Base, "initialize", rb_digest_base_init, -1);
|
2003-09-28 22:44:49 -04:00
|
|
|
rb_define_method(cDigest_Base, "initialize_copy", rb_digest_base_copy, 1);
|
2006-10-11 01:15:15 -04:00
|
|
|
rb_define_method(cDigest_Base, "reset", rb_digest_base_reset, 0);
|
2001-07-13 16:06:14 -04:00
|
|
|
rb_define_method(cDigest_Base, "update", rb_digest_base_update, 1);
|
2006-10-05 13:39:51 -04:00
|
|
|
rb_define_method(cDigest_Base, "<<", rb_digest_base_lshift, 1);
|
2001-07-13 16:06:14 -04:00
|
|
|
rb_define_method(cDigest_Base, "digest", rb_digest_base_digest, 0);
|
|
|
|
rb_define_method(cDigest_Base, "hexdigest", rb_digest_base_hexdigest, 0);
|
2006-10-05 15:13:00 -04:00
|
|
|
rb_define_method(cDigest_Base, "bubblebabble", rb_digest_base_bubblebabble, 0);
|
2001-07-13 16:06:14 -04:00
|
|
|
rb_define_method(cDigest_Base, "to_s", rb_digest_base_hexdigest, 0);
|
2006-09-13 19:32:12 -04:00
|
|
|
rb_define_method(cDigest_Base, "inspect", rb_digest_base_inspect, 0);
|
2001-07-13 16:06:14 -04:00
|
|
|
rb_define_method(cDigest_Base, "==", rb_digest_base_equal, 1);
|
|
|
|
|
|
|
|
id_metadata = rb_intern("metadata");
|
2006-10-05 13:39:51 -04:00
|
|
|
id_new = rb_intern("new");
|
2006-10-11 02:05:32 -04:00
|
|
|
id_initialize = rb_intern("initialize");
|
2006-10-05 13:39:51 -04:00
|
|
|
id_update = rb_intern("update");
|
|
|
|
id_digest = rb_intern("digest");
|
2001-07-13 16:06:14 -04:00
|
|
|
}
|