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 *
|
2002-09-03 01:20:14 -04:00
|
|
|
get_digest_base_metadata(klass)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2002-09-03 01:20:14 -04:00
|
|
|
rb_digest_base_alloc(klass)
|
|
|
|
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
|
|
|
|
|
|
|
pctx = xmalloc(algo->ctx_size);
|
|
|
|
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
|
2002-09-03 01:20:14 -04:00
|
|
|
rb_digest_base_s_digest(klass, str)
|
|
|
|
VALUE klass;
|
2001-07-13 16:06:14 -04:00
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
algo_t *algo;
|
|
|
|
void *pctx;
|
|
|
|
size_t len;
|
|
|
|
unsigned char *digest;
|
2002-09-03 01:20:14 -04:00
|
|
|
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);
|
|
|
|
|
|
|
|
len = algo->digest_len;
|
|
|
|
|
|
|
|
digest = xmalloc(len);
|
|
|
|
algo->final_func(digest, pctx);
|
|
|
|
|
|
|
|
obj = rb_str_new(digest, len);
|
|
|
|
|
|
|
|
free(digest);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2002-09-03 01:20:14 -04:00
|
|
|
rb_digest_base_s_hexdigest(klass, str)
|
|
|
|
VALUE klass;
|
2001-07-13 16:06:14 -04:00
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
algo_t *algo;
|
|
|
|
void *pctx;
|
|
|
|
size_t len;
|
|
|
|
unsigned char *hexdigest;
|
2002-09-03 01:20:14 -04:00
|
|
|
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);
|
|
|
|
|
|
|
|
len = algo->digest_len * 2;
|
|
|
|
|
|
|
|
hexdigest = xmalloc(len + 1); /* +1 is for '\0' */
|
|
|
|
algo->end_func(pctx, hexdigest);
|
2001-09-06 01:29:27 -04:00
|
|
|
|
2001-07-13 16:06:14 -04:00
|
|
|
obj = rb_str_new(hexdigest, len);
|
|
|
|
|
|
|
|
free(hexdigest);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2002-09-03 01:20:14 -04:00
|
|
|
rb_digest_base_become(copy, obj)
|
|
|
|
VALUE copy, 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);
|
2002-09-04 05:21:07 -04:00
|
|
|
algo = get_digest_base_metadata(CLASS_OF(copy));
|
2002-09-03 01:20:14 -04:00
|
|
|
if (algo != get_digest_base_metadata(CLASS_OF(obj))) {
|
|
|
|
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
|
|
|
|
rb_digest_base_update(self, str)
|
|
|
|
VALUE self, str;
|
|
|
|
{
|
|
|
|
algo_t *algo;
|
|
|
|
void *pctx;
|
|
|
|
|
|
|
|
StringValue(str);
|
|
|
|
algo = get_digest_base_metadata(CLASS_OF(self));
|
|
|
|
Data_Get_Struct(self, void, pctx);
|
|
|
|
|
|
|
|
algo->update_func(pctx, RSTRING(str)->ptr, RSTRING(str)->len);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_digest_base_init(argc, argv, self)
|
|
|
|
int argc;
|
|
|
|
VALUE* argv;
|
|
|
|
VALUE self;
|
|
|
|
{
|
|
|
|
VALUE arg;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &arg);
|
|
|
|
|
|
|
|
if (!NIL_P(arg)) rb_digest_base_update(self, arg);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_digest_base_digest(self)
|
|
|
|
VALUE self;
|
|
|
|
{
|
|
|
|
algo_t *algo;
|
|
|
|
void *pctx1, *pctx2;
|
|
|
|
unsigned char *digest;
|
|
|
|
size_t len;
|
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
algo = get_digest_base_metadata(CLASS_OF(self));
|
|
|
|
Data_Get_Struct(self, void, pctx1);
|
|
|
|
|
|
|
|
len = algo->ctx_size;
|
|
|
|
|
|
|
|
pctx2 = xmalloc(len);
|
|
|
|
memcpy(pctx2, pctx1, len);
|
|
|
|
|
|
|
|
len = algo->digest_len;
|
|
|
|
|
|
|
|
digest = xmalloc(len);
|
|
|
|
algo->final_func(digest, pctx2);
|
|
|
|
|
|
|
|
str = rb_str_new(digest, len);
|
|
|
|
|
|
|
|
free(digest);
|
|
|
|
free(pctx2);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_digest_base_hexdigest(self)
|
|
|
|
VALUE self;
|
|
|
|
{
|
|
|
|
algo_t *algo;
|
|
|
|
void *pctx1, *pctx2;
|
|
|
|
unsigned char *hexdigest;
|
|
|
|
size_t len;
|
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
algo = get_digest_base_metadata(CLASS_OF(self));
|
|
|
|
Data_Get_Struct(self, void, pctx1);
|
|
|
|
|
|
|
|
len = algo->ctx_size;
|
|
|
|
|
|
|
|
pctx2 = xmalloc(len);
|
|
|
|
memcpy(pctx2, pctx1, len);
|
|
|
|
|
|
|
|
len = algo->digest_len * 2;
|
|
|
|
|
|
|
|
hexdigest = xmalloc(len + 1); /* +1 is for '\0' */
|
|
|
|
algo->end_func(pctx2, hexdigest);
|
|
|
|
|
|
|
|
str = rb_str_new(hexdigest, len);
|
|
|
|
|
|
|
|
free(hexdigest);
|
|
|
|
free(pctx2);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_digest_base_equal(self, other)
|
|
|
|
VALUE self, other;
|
|
|
|
{
|
|
|
|
algo_t *algo;
|
2002-09-03 01:20:14 -04:00
|
|
|
VALUE klass;
|
2001-07-13 16:06:14 -04:00
|
|
|
VALUE str1, str2;
|
|
|
|
|
2002-09-03 01:20:14 -04:00
|
|
|
klass = CLASS_OF(self);
|
|
|
|
algo = get_digest_base_metadata(klass);
|
2001-07-13 16:06:14 -04:00
|
|
|
|
2002-09-03 01:20:14 -04:00
|
|
|
if (CLASS_OF(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
|
|
|
|
&& rb_str_cmp(str1, str2) == 0)
|
|
|
|
return Qtrue;
|
|
|
|
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Init
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_digest()
|
|
|
|
{
|
|
|
|
mDigest = rb_define_module("Digest");
|
|
|
|
|
|
|
|
cDigest_Base = rb_define_class_under(mDigest, "Base", rb_cObject);
|
|
|
|
|
2002-09-03 01:20:14 -04:00
|
|
|
rb_define_singleton_method(cDigest_Base, "allocate", rb_digest_base_alloc, 0);
|
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);
|
2002-09-03 01:20:14 -04:00
|
|
|
rb_define_method(cDigest_Base, "become", rb_digest_base_become, 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");
|
|
|
|
}
|