# Re: MD5 MemberwiseClone

**URL:** <https://www.ruby-forum.com/t/re-md5-memberwiseclone/131016>\
**Category:** IronRuby\
**Created:** [March 5, 2008, 7:56am UTC](https://www.ruby-forum.com/t/re-md5-memberwiseclone/131016 "2008-03-05T07:56:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wayne\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wayne\_K](https://www.ruby-forum.com/u/Wayne_K)\
**Post date:** [March 5, 2008, 7:56am UTC](https://www.ruby-forum.com/t/re-md5-memberwiseclone/131016/1 "2008-03-05T07:56:19Z")

</div>

> From: Sanghyeon S. [removed\_email\_address@domain.invalid]  
> Sent: Friday, 29 February 2008 2:23 PM  
> To: removed\_email\_address@domain.invalid  
> Subject: Re: [Ironruby-core] digest.so implemented
> 
> Hash implementations are fully managed (in .NET and Mono), and shallow  
> copying by MemberwiseClone does give the correct semantics _now_.

Are you sure? Which version of the .NET framework are you assuming? Are  
you claiming it works on .NET 2.0?

Consider the following C# snippet:

```
HashAlgorithm digest = MD5.Create();
HashAlgorithm clone = 

```

(HashAlgorithm)digest.GetType().GetMethod(“MemberwiseClone”,  
BindingFlags.Instance | BindingFlags.NonPublic).Invoke(digest, null);  
clone.TransformFinalBlock(new byte[0], 0, 0);  
byte[] block = new byte[] {1, 2, 3, 4};  
digest.TransformBlock(block, 0, block.Length, block, 0);

On .NET 2.0, the final line fails despite the fact that only the clone  
has been finalized.

This corresponds to the following Ruby use case:

digest = Digest::MD5.new  
digest \<\< ‘Initial’  
puts digest.hexdigest  
digest \<\< ‘add some more’  
puts digest.hexdigest

You could argue that such useage is unlikely in practice, but it seems  
to me that the CRuby implementation was specifically designed to support  
this case - why else would they bother cloning?

Cheers, Wayne.

BTW, how does one code (BindingFlags.Instance | BindingFlags.NonPublic)  
in IronRuby?  
I had to resort to  
System::Enum.ToObject(System::Reflection::BindingFlags.Instance.GetType(),  
36)  
Is there a simplier/more elegant solution?
