1998-01-16 07:13:05 -05:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
md5init.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Fri Aug 2 09:24:12 JST 1996
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Copyright (C) 1995-1998 Yukihiro Matsumoto
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
************************************************/
|
|
|
|
/* This module provides an interface to the RSA Data Security,
|
|
|
|
Inc. MD5 Message-Digest Algorithm, described in RFC 1321.
|
|
|
|
It requires the files md5c.c and md5.h (which are slightly changed
|
|
|
|
from the versions in the RFC to avoid the "global.h" file.) */
|
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
#include "md5.h"
|
|
|
|
|
|
|
|
static VALUE cMD5;
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
md5_update(obj, str)
|
|
|
|
VALUE obj;
|
|
|
|
struct RString *str;
|
|
|
|
{
|
|
|
|
MD5_CTX *md5;
|
|
|
|
|
|
|
|
Check_Type(str, T_STRING);
|
|
|
|
Data_Get_Struct(obj, MD5_CTX, md5);
|
|
|
|
MD5Update(md5, str->ptr, str->len);
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
return obj;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:37:52 -04:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
md5_digest(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
MD5_CTX *md5, ctx;
|
|
|
|
unsigned char digest[16];
|
|
|
|
|
|
|
|
Data_Get_Struct(obj, MD5_CTX, md5);
|
|
|
|
ctx = *md5;
|
|
|
|
MD5Final(digest, &ctx);
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_str_new(digest, 16);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
|
|
|
md5_hexdigest(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
MD5_CTX *md5, ctx;
|
|
|
|
unsigned char digest[16];
|
|
|
|
char buf[33];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
Data_Get_Struct(obj, MD5_CTX, md5);
|
|
|
|
ctx = *md5;
|
|
|
|
MD5Final(digest, &ctx);
|
|
|
|
|
|
|
|
for (i=0; i<16; i++) {
|
|
|
|
sprintf(buf+i*2, "%02x", digest[i]);
|
|
|
|
}
|
|
|
|
return rb_str_new(buf, 32);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
md5_clone(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
MD5_CTX *md5, *md5_new;
|
|
|
|
|
|
|
|
Data_Get_Struct(obj, MD5_CTX, md5);
|
1999-01-19 23:59:39 -05:00
|
|
|
obj = Data_Make_Struct(CLASS_OF(obj), MD5_CTX, 0, free, md5_new);
|
1998-01-16 07:13:05 -05:00
|
|
|
*md5_new = *md5;
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
md5_new(argc, argv, class)
|
1999-01-19 23:59:39 -05:00
|
|
|
int argc;
|
|
|
|
VALUE* argv;
|
|
|
|
VALUE class;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE arg, obj;
|
|
|
|
MD5_CTX *md5;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &arg);
|
|
|
|
if (!NIL_P(arg)) Check_Type(arg, T_STRING);
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
obj = Data_Make_Struct(class, MD5_CTX, 0, free, md5);
|
1998-01-16 07:13:05 -05:00
|
|
|
MD5Init(md5);
|
|
|
|
if (!NIL_P(arg)) {
|
|
|
|
md5_update(obj, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
void
|
1998-01-16 07:13:05 -05:00
|
|
|
Init_md5()
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
cMD5 = rb_define_class("MD5", rb_cObject);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
rb_define_singleton_method(cMD5, "new", md5_new, -1);
|
2000-07-24 03:19:34 -04:00
|
|
|
rb_define_singleton_method(cMD5, "md5", md5_new, -1);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
rb_define_method(cMD5, "update", md5_update, 1);
|
|
|
|
rb_define_method(cMD5, "digest", md5_digest, 0);
|
1999-08-13 01:37:52 -04:00
|
|
|
rb_define_method(cMD5, "hexdigest", md5_hexdigest, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_define_method(cMD5, "clone", md5_clone, 0);
|
|
|
|
}
|