TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: CADmium on November 17, 2008, 04:32:41 AM

Title: MD5-Hash
Post by: CADmium on November 17, 2008, 04:32:41 AM
Has anyone a (Auto) Lisp-routine to get the MD5-Hash from a String ? (or other Lispcode like http://en.wikipedia.org/wiki/MD5#Pseudocode )
Title: Re: MD5-Hash
Post by: It's Alive! on November 18, 2008, 01:16:06 PM
You can do this via .net easier.. i.e

I attached a compiled dll for you to try.

Code: [Select]
namespace LispBase
{
  public class Commands
  {
    //(MD5 "MD5")
    [LispFunction("MD5")]
    public static ResultBuffer test1(ResultBuffer args)
    {
      ResultBuffer retList = new ResultBuffer();
      try
      {
        Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

        if (args == null)
        {
          retList.AddErr("Null Argument");
          return retList;
        }

        if (args.GetCount() == 0)
        {
          retList.AddErr("To Few Arguments");
          return retList;
        }

        if (args.GetCount() == 2)
        {
          retList.AddErr("To Many Arguments");
          return retList;
        }
        string StrMd5 = HashString(Convert.ToString(args.AsArray()[0].Value));
        retList.AddString(StrMd5);
      }
      catch (System.Exception ex)
      {
        retList.AddErr(ex.Message);
      }
      return retList;
    }

    private static string HashString(string Value)
    {
      System.Security.Cryptography.MD5CryptoServiceProvider MD5 =
        new System.Security.Cryptography.MD5CryptoServiceProvider();
      byte[] data = System.Text.Encoding.ASCII.GetBytes(Value);
      data = MD5.ComputeHash(data);
      StringBuilder sb = new StringBuilder(data.Length);
      for (int i = 0; i < data.Length; i++)
        sb.Append(data[i].ToString("x2").ToLower());
      return sb.ToString();
    }
  }
}

Title: Re: MD5-Hash
Post by: kdub_nz on November 18, 2008, 05:01:32 PM

string StrMd5 = HashString(Convert.ToString(args.AsArray()[0].Value));

nice protection against someone silly like me who would try to pass an integer .. :)
Title: Re: MD5-Hash
Post by: It's Alive! on November 18, 2008, 06:56:28 PM
Heh! When in doubt, convert to string  :laugh:
Title: Re: MD5-Hash
Post by: CADmium on November 19, 2008, 02:34:43 AM
Many thanks.
I know, that windows has such a function inside and that isn't a problem to use it via .Net ,but I'm looking for a Code only in autolisp, without any other dll and arx .... If somebody has such a Routine.... ??
Title: Re: MD5-Hash
Post by: It's Alive! on November 19, 2008, 02:46:28 AM
Sorry, I don’t know of any off hand, looks like it would be fun to write one though.