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
|
|
|
|
Copyright (C) 2001 Akinori MUSHA
|
|
|
|
|
|
|
|
$RoughId: digest.c,v 1.16 2001/07/13 15:38:27 knu Exp $
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "digest.h"
|
|
|
|
|
|
|
|
static VALUE mDigest, cDigest_Base;
|
|
|
|
static ID id_metadata;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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) {
|
2001-07-13 16:06:14 -04:00
|
|
|
rb_notimplement();
|
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
|
|
|
|
hexdigest_str_new(const unsigned char *digest, size_t digest_len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
VALUE str;
|
|
|
|
char *p;
|
|
|
|
static const char hex[] = "0123456789abcdef";
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* 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
|
|
|
/* XXX: An uninitialized buffer may lead ALGO_Equal() to fail */
|
2003-10-15 00:19:48 -04:00
|
|
|
pctx = xcalloc(algo->ctx_size, 1);
|
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
|
|
|
{
|
|
|
|
algo_t *algo;
|
|
|
|
void *pctx;
|
2005-12-11 19:36:54 -05:00
|
|
|
volatile VALUE obj = rb_digest_base_alloc(klass);
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2002-09-03 01:20:14 -04:00
|
|
|
algo = get_digest_base_metadata(klass);
|
|
|
|
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
|
|
|
{
|
|
|
|
algo_t *algo;
|
|
|
|
void *pctx;
|
* 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
|
|
|
void *digest;
|
|
|
|
size_t len;
|
2005-12-11 19:36:54 -05:00
|
|
|
volatile VALUE obj = rb_digest_base_alloc(klass);
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2002-09-03 01:20:14 -04:00
|
|
|
algo = get_digest_base_metadata(klass);
|
2002-09-26 09:49:40 -04:00
|
|
|
Data_Get_Struct(obj, void, pctx);
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2002-09-26 09:49:40 -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
|
|
|
|
* 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
|
|
|
len = algo->digest_len;
|
|
|
|
digest = xmalloc(len);
|
|
|
|
algo->finish_func(pctx, digest);
|
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
|
|
|
return hexdigest_str_new(digest, len);
|
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));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
StringValue(str);
|
2004-09-17 05:24:13 -04:00
|
|
|
algo = get_digest_base_metadata(rb_obj_class(self));
|
2001-07-13 16:06:14 -04:00
|
|
|
Data_Get_Struct(self, void, pctx);
|
|
|
|
|
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));
|
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
|
|
|
{
|
|
|
|
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
|
|
|
void *digest;
|
|
|
|
size_t ctx_size, len;
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2004-09-17 05:24:13 -04:00
|
|
|
algo = get_digest_base_metadata(rb_obj_class(self));
|
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
|
|
|
len = algo->digest_len;
|
|
|
|
digest = xmalloc(len);
|
|
|
|
algo->finish_func(pctx2, digest);
|
2001-07-13 16:06:14 -04:00
|
|
|
free(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
|
|
|
return hexdigest_str_new(digest, len);
|
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;
|
|
|
|
VALUE str;
|
|
|
|
char *cname;
|
|
|
|
|
|
|
|
algo = get_digest_base_metadata(rb_obj_class(self));
|
|
|
|
|
|
|
|
cname = rb_obj_classname(self);
|
|
|
|
|
|
|
|
/* #<Digest::Alg: xxxxx...xxxx> */
|
|
|
|
str = rb_str_buf_new(2 + strlen(cname) + 2 + algo->digest_len * 2 + 1);
|
|
|
|
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);
|
2002-09-03 01:20:14 -04:00
|
|
|
algo = get_digest_base_metadata(klass);
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2004-09-17 05:24:13 -04:00
|
|
|
if (rb_obj_class(other) == klass) {
|
2001-07-13 16:06:14 -04:00
|
|
|
void *pctx1, *pctx2;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, void, pctx1);
|
|
|
|
Data_Get_Struct(other, void, pctx2);
|
|
|
|
|
|
|
|
return algo->equal_func(pctx1, pctx2) ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringValue(other);
|
|
|
|
str2 = other;
|
|
|
|
|
2006-08-31 06:30:33 -04:00
|
|
|
if (RSTRING_LEN(str2) == algo->digest_len)
|
2001-07-13 16:06:14 -04:00
|
|
|
str1 = rb_digest_base_digest(self);
|
|
|
|
else
|
|
|
|
str1 = rb_digest_base_hexdigest(self);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
2001-07-13 16:06:14 -04:00
|
|
|
rb_define_method(cDigest_Base, "update", rb_digest_base_update, 1);
|
|
|
|
rb_define_method(cDigest_Base, "<<", rb_digest_base_update, 1);
|
|
|
|
rb_define_method(cDigest_Base, "digest", rb_digest_base_digest, 0);
|
|
|
|
rb_define_method(cDigest_Base, "hexdigest", rb_digest_base_hexdigest, 0);
|
|
|
|
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");
|
|
|
|
}
|