1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/ext/md5/md5init.c
matz f84f4aa6b3 * array.c (rb_ary_and): should not push frozen key string.
* array.c (rb_ary_or): ditto.

* eval.c (rb_thread_schedule): should save context before raising
  deadlock, saved context for current thread might be obsolete.

* time.c (make_time_t): non DST timezone shift supported (hopefully).

* time.c (make_time_t): strict range detection for negative time_t.

* signal.c: SIGINFO added.

* eval.c (rb_ensure): should not SEGV when prot_tag is NULL.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1399 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2001-05-16 09:05:54 +00:00

110 lines
2.2 KiB
C

/************************************************
md5init.c -
$Author$
created at: Fri Aug 2 09:24:12 JST 1996
Copyright (C) 1995-2001 Yukihiro Matsumoto
************************************************/
/* This module provides an interface to the RSA Data Security,
Inc. MD5 Message-Digest Algorithm, described in RFC 1321. */
#include "ruby.h"
#include "md5.h"
static VALUE cMD5;
static VALUE
md5i_update(obj, str)
VALUE obj, str;
{
md5_state_t *md5;
StringValue(str);
Data_Get_Struct(obj, md5_state_t, md5);
md5_append(md5, RSTRING(str)->ptr, RSTRING(str)->len);
return obj;
}
static VALUE
md5i_digest(obj)
VALUE obj;
{
md5_state_t *md5, ctx;
md5_byte_t digest[16];
Data_Get_Struct(obj, md5_state_t, md5);
ctx = *md5;
md5_finish(&ctx, digest);
return rb_str_new(digest, 16);
}
static VALUE
md5i_hexdigest(obj)
VALUE obj;
{
md5_state_t *md5, ctx;
md5_byte_t digest[16];
char buf[33];
int i;
Data_Get_Struct(obj, md5_state_t, md5);
ctx = *md5;
md5_finish(&ctx, digest);
for (i=0; i<16; i++) {
sprintf(buf+i*2, "%02x", digest[i]);
}
return rb_str_new(buf, 32);
}
static VALUE
md5i_clone(obj)
VALUE obj;
{
md5_state_t *md5, *md5_new;
Data_Get_Struct(obj, md5_state_t, md5);
obj = Data_Make_Struct(CLASS_OF(obj), md5_state_t, 0, free, md5_new);
*md5_new = *md5;
return obj;
}
static VALUE
md5i_new(argc, argv, class)
int argc;
VALUE* argv;
VALUE class;
{
VALUE obj, str;
md5_state_t *md5;
obj = Data_Make_Struct(class, md5_state_t, 0, free, md5);
md5_init(md5);
rb_scan_args(argc, argv, "01", &str);
if (argc == 1) {
md5i_update(obj, str);
}
return obj;
}
void
Init_md5()
{
cMD5 = rb_define_class("MD5", rb_cObject);
rb_define_singleton_method(cMD5, "new", md5i_new, -1);
rb_define_singleton_method(cMD5, "md5", md5i_new, -1);
rb_define_method(cMD5, "update", md5i_update, 1);
rb_define_method(cMD5, "<<", md5i_update, 1);
rb_define_method(cMD5, "digest", md5i_digest, 0);
rb_define_method(cMD5, "hexdigest", md5i_hexdigest, 0);
rb_define_method(cMD5, "clone", md5i_clone, 0);
}