[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Digest::MD5 Resume?

christoph.heindl@gmail.com

12/22/2004 4:53:00 PM

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

5 Answers

martinus

12/23/2004 9:02:00 AM

0

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

Yukihiro Matsumoto

12/23/2004 4:39:00 PM

0

Hi,

In message "Re: Digest::MD5 Resume?"
on Thu, 23 Dec 2004 18:06:59 +0900, "martinus" <martin.ankerl@gmail.com> writes:

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

Let me put it in my todo list.

matz.


nobu.nokada

12/23/2004 6:11:00 PM

0

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");


--
Nobu Nakada


Florian Gross

12/23/2004 7:22:00 PM

0

nobu.nokada@softhome.net wrote:

> 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!

nobu.nokada

12/25/2004 6:32:00 AM

0

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.

--
Nobu Nakada