[IronPython] md5 builtins module

Mark Rees mark.john.rees at gmail.com
Thu Jul 27 06:40:24 CEST 2006


Great work Kevin. It's my understanding that the IP team want to
overtime make more of the standard library modules that rely on
compiled extensions part of the IP Module.cs. So you have done one for
them.

The bigger issue that was discovered when you tried running Seo's
md5.py is the IP's way of handling overloaded methods is broken in RC1
in some cases. I know that there is the same problem calling some
other .NET libraries as well.

On 7/27/06, Kevin Chu <redmoon17 at gmail.com> wrote:
> yesterday,we talked about missing md5 module.
> first,I found RC1 without md5 module,
> second,I found md5.py that be provide by
> Seo(http://sparcs.kaist.ac.kr/~tinuviel/fepy/lib/) is not working .
> so,I write a builtin module ,put it into source,and compile it.
> md5 module can work !
>
> below is md5 module's code:
>
> =======================================
> using System;
> using System.Collections.Generic;
> using System.Text;
> using IronPython.Runtime;
> using System.Security.Cryptography;
> using System.Runtime.InteropServices;
>
> [assembly: PythonModule("md5", typeof(IronPython.Modules.PythonMD5))]
> namespace IronPython.Modules
> {
>     [PythonType("md5")]
>     public class PythonMD5
>     {
>         private MD5 _provider;
>         private readonly Encoding raw = Encoding.GetEncoding("iso-8859-1");
>         private byte[] empty;
>
>         public PythonMD5()
>         {
>             _provider = MD5.Create();
>             empty = raw.GetBytes("");
>         }
>
>         public PythonMD5(string arg)
>             : this()
>         {
>             this.Update(arg);
>         }
>
>         internal PythonMD5(MD5 provider)
>             : this()
>         {
>             _provider = provider;
>         }
>         [PythonName("new")]
>         public static PythonMD5
> PythonNew([DefaultParameterValue(null)] string arg)
>         {
>             PythonMD5 obj;
>             if (arg == null)
>                 obj = new PythonMD5();
>             else
>                 obj = new PythonMD5(arg);
>             return obj;
>         }
>         [PythonName("md5")]
>         public static PythonMD5
> PythonNew2([DefaultParameterValue(null)] string arg)
>         {
>             return PythonMD5.PythonNew(arg);
>         }
>         [PythonName("update")]
>         public void Update(string arg)
>         {
>             byte[] bytes = raw.GetBytes(arg);
>             _provider.TransformBlock(bytes, 0, bytes.Length, bytes, 0);
>         }
>         [PythonName("digest")]
>         public string Digest()
>         {
>             _provider.TransformFinalBlock(empty, 0, 0);
>             return raw.GetString(_provider.Hash);
>         }
>         [PythonName("hexdigest")]
>         public string HexDigest()
>         {
>             _provider.TransformFinalBlock(empty, 0, 0);
>             string hexString = "";
>             foreach (byte b in empty)
>             {
>                 hexString += b.ToString("X2");
>             }
>             return hexString;
>         }
>         [PythonName("copy")]
>         public PythonMD5 Clone()
>         {
>             PythonMD5 obj = new PythonMD5(this._provider);
>             return obj;
>         }
>     }
> }
> ===================================
> I test it using example in Python Library Reference,
> test code is:
> ===================================
> For example, to obtain the digest of the string 'Nobody inspects the
> spammish repetition':
>
>
> >>> import md5
> >>> m = md5.new()
> >>> m.update("Nobody inspects")
> >>> m.update(" the spammish repetition")
> >>> m.digest()
> '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
>
> More condensed:
>
>
> >>> md5.new("Nobody inspects the spammish repetition").digest()
> '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
>
> ======================================
> a suggestion:
> you should notice that I write a static method "PythonNew2",because I
> would like to implement this:
>
> md5( [arg])
> For backward compatibility reasons, this is an alternative name for
> the new() function.
>
> therefore I have to write reduplicate method,but if PythonName
> attribute can accept multi args,the problem can be solved easily.
>
> --
> Once in a Redmoon
> _______________________________________________
> users mailing list
> users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>



More information about the Ironpython-users mailing list