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$
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
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
|
|
|
|
2003-10-15 09:50:51 -04:00
|
|
|
/* XXX: An uninitialized buffer leads 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);
|
2001-07-13 16:06:14 -04:00
|
|
|
algo->update_func(pctx, RSTRING(str)->ptr, RSTRING(str)->len);
|
|
|
|
|
2005-12-11 19:36:54 -05:00
|
|
|
str = rb_str_new(0, algo->digest_len);
|
|
|
|
algo->final_func(RSTRING(str)->ptr, pctx);
|
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;
|
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);
|
2001-07-13 16:06:14 -04:00
|
|
|
algo->update_func(pctx, RSTRING(str)->ptr, RSTRING(str)->len);
|
|
|
|
|
2005-12-11 19:36:54 -05:00
|
|
|
str = rb_str_new(0, algo->digest_len * 2);
|
|
|
|
algo->end_func(pctx, RSTRING(str)->ptr);
|
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_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);
|
|
|
|
|
|
|
|
algo->update_func(pctx, RSTRING(str)->ptr, RSTRING(str)->len);
|
|
|
|
|
|
|
|
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;
|
|
|
|
size_t len;
|
|
|
|
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);
|
|
|
|
|
2005-12-11 19:36:54 -05:00
|
|
|
str = rb_str_new(0, algo->digest_len);
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2005-12-11 19:36:54 -05:00
|
|
|
len = algo->ctx_size;
|
2001-07-13 16:06:14 -04:00
|
|
|
pctx2 = xmalloc(len);
|
|
|
|
memcpy(pctx2, pctx1, len);
|
|
|
|
|
2005-12-11 19:36:54 -05:00
|
|
|
algo->final_func(RSTRING(str)->ptr, pctx2);
|
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;
|
|
|
|
size_t len;
|
|
|
|
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);
|
|
|
|
|
2005-12-11 19:36:54 -05:00
|
|
|
str = rb_str_new(0, algo->digest_len * 2);
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2005-12-11 19:36:54 -05:00
|
|
|
len = algo->ctx_size;
|
2001-07-13 16:06:14 -04:00
|
|
|
pctx2 = xmalloc(len);
|
|
|
|
memcpy(pctx2, pctx1, len);
|
|
|
|
|
2005-12-11 19:36:54 -05:00
|
|
|
algo->end_func(pctx2, RSTRING(str)->ptr);
|
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_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;
|
|
|
|
|
|
|
|
if (RSTRING(str2)->len == algo->digest_len)
|
|
|
|
str1 = rb_digest_base_digest(self);
|
|
|
|
else
|
|
|
|
str1 = rb_digest_base_hexdigest(self);
|
|
|
|
|
|
|
|
if (RSTRING(str1)->len == RSTRING(str2)->len
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Init
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_digest()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
rb_define_method(cDigest_Base, "==", rb_digest_base_equal, 1);
|
|
|
|
|
|
|
|
id_metadata = rb_intern("metadata");
|
|
|
|
}
|