Digest::MD5 Resume?

C

christoph.heindl

Hi,

I'm currently working on a little project which requires computation of
a MD5 checksum from a stream.
This works well as long as the stream is not closed. However, if the
stream is eventually closed, I need to
persist the MD5 object somehow, so I can reload it later on. I did not
find any way to persist (dump, marshal, whatever) way such a MD5
object. It seems like it is not supported by ruby. However, it should
be possible to persist an MD5 object as long as the context is
persisted as well. This begs the question: Is there a way to
persist and activate an MD5 object?

regards,
christoph
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Digest::MD5 Resume?"

|I have the same problem. A 'Marshal.dump(md5obj)' would be nice.
|martinus

Let me put it in my todo list.

matz.
 
N

nobu.nokada

Hi,

At Fri, 24 Dec 2004 01:38:45 +0900,
Yukihiro Matsumoto wrote in [ruby-talk:124370]:
|I have the same problem. A 'Marshal.dump(md5obj)' would be nice.
|martinus

Let me put it in my todo list.

It seems quite simple.


Index: ext/digest/digest.c
===================================================================
RCS file: /cvs/ruby/src/ruby/ext/digest/digest.c,v
retrieving revision 1.15
diff -U2 -p -r1.15 digest.c
--- ext/digest/digest.c 17 Sep 2004 09:24:11 -0000 1.15
+++ ext/digest/digest.c 23 Dec 2004 17:36:19 -0000
@@ -289,4 +289,36 @@ rb_digest_base_equal(self, other)
}

+static VALUE
+rb_digest_base_mdump(self)
+ VALUE self;
+{
+ algo_t *algo;
+ void *pctx;
+
+ algo = get_digest_base_metadata(rb_obj_class(self));
+ Data_Get_Struct(self, void, pctx);
+ return rb_str_new(pctx, algo->ctx_size);
+}
+
+static VALUE
+rb_digest_base_mload(self, data)
+ VALUE self, data;
+{
+ algo_t *algo;
+ void *pctx;
+ size_t len;
+
+ StringValue(data);
+ algo = get_digest_base_metadata(rb_obj_class(self));
+ Data_Get_Struct(self, void, pctx);
+ len = algo->ctx_size;
+ if (RSTRING(data)->len != len) {
+ rb_raise(rb_eArgError, "wrong size digest context (%ld for %ld)",
+ RSTRING(data)->len, len);
+ }
+ memcpy(pctx, RSTRING(data)->ptr, len);
+ return self;
+}
+
/*
* Init
@@ -312,4 +344,6 @@ Init_digest()
rb_define_method(cDigest_Base, "to_s", rb_digest_base_hexdigest, 0);
rb_define_method(cDigest_Base, "==", rb_digest_base_equal, 1);
+ rb_define_method(cDigest_Base, "marshal_dump", rb_digest_base_mdump, 0);
+ rb_define_method(cDigest_Base, "marshal_load", rb_digest_base_mload, 1);

id_metadata = rb_intern("metadata");
 
F

Florian Gross

Yukihiro Matsumoto wrote in [ruby-talk:124370]:
|I have the same problem. A 'Marshal.dump(md5obj)' would be nice.
|martinus
Let me put it in my todo list.

It seems quite simple.

[snip patch]

Nobu, you are a wonderful patch machine. Thank you a lot!
 
N

nobu.nokada

Hi,

At Fri, 24 Dec 2004 08:54:56 +0900,
Yukihiro Matsumoto wrote in [ruby-talk:124384]:
|It seems quite simple.

Does this emit portable data?

Hmmm, unfortunately, no. They contain data which have endian.
And more unfortunate, OpenSSL versions are incompatible with
ruby own versions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top