Author Topic: MD5-Hash  (Read 2596 times)

0 Members and 1 Guest are viewing this topic.

CADmium

  • Newt
  • Posts: 33
MD5-Hash
« 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 )
"Bei 99% aller Probleme ist die umfassende Beschreibung des Problems bereits mehr als die Hälfte der Lösung desselben."

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8743
  • AKA Daniel
Re: MD5-Hash
« Reply #1 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();
    }
  }
}


kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2144
  • class keyThumper<T>:ILazy<T>
Re: MD5-Hash
« Reply #2 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 .. :)
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8743
  • AKA Daniel
Re: MD5-Hash
« Reply #3 on: November 18, 2008, 06:56:28 PM »
Heh! When in doubt, convert to string  :laugh:

CADmium

  • Newt
  • Posts: 33
Re: MD5-Hash
« Reply #4 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.... ??
"Bei 99% aller Probleme ist die umfassende Beschreibung des Problems bereits mehr als die Hälfte der Lösung desselben."

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8743
  • AKA Daniel
Re: MD5-Hash
« Reply #5 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.