MD5 Hash

Started by JTaylor, February 22, 2021, 08:07:19 PM

Previous topic - Next topic

JTaylor

Would someone please put me out of my misery...I am trying to create an MD5 Hash of a password.   I have the C# code below but can't figure out how to create the MD5CryptoServiceProvider() object.  Not sure if I am missing something on my Win10 system or what.  Suggestions?  Thanks.

Jim

I get this far but then a brick wall.

Code (winbatch) Select

  ObjectClrOption ( 'useany', 'System')

  strBuilder = ObjectClrNew('System.Text.StringBuilder')






Code (csharp) Select



using System.Text; 
using System.Security.Cryptography; 

namespace CryptoLib 

  public static class Encryptor 
  { 
    public static string MD5Hash(string text) 
    { 
      MD5 md5 = new MD5CryptoServiceProvider(); 

      //compute hash from the bytes of text 
      md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text)); 
 
      //get hash result after compute it 
      byte[] result = md5.Hash; 

      StringBuilder strBuilder = new StringBuilder(); 
      for (int i = 0; i < result.Length; i++) 
      { 
        //change it into 2 hexadecimal digits 
        //for each byte 
        strBuilder.Append(result[i].ToString("x2")); 
      } 

      return strBuilder.ToString(); 
    } 
  } 
}

td

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

[size=0px]BinaryCheckSum - another on of the functions easy to overlook. My brick wall came here[/size]
Code (WINBATCH) Select


text = "Compute Hash"
ObjectClrOption ( 'useany', 'System')
ObjectClrOption ( 'useany', 'System.Security')
md5 = ObjectClrNew('System.Security.Cryptography.MD5')
hash = ObjectClrNew('System.Security.Cryptography.HashAlgorithm',md5.Create())
Message(text,hash.Create())



doesn't error to that point... just no where to go.

JTaylor

Because I was too dumb to search for MD5 in the Help file?

Thanks.

Jim

Quote from: td on February 22, 2021, 09:48:14 PM
Why not just use the BinaryCheckSum function?

https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/WILAK_B__040.htm


JTaylor


Thanks Stan.  I had gotten a little further but your solution is much shorter and easier and works.

Jim

Quote from: stanl on February 23, 2021, 03:24:10 AM
[size=0px]BinaryCheckSum - another on of the functions easy to overlook. My brick wall came here[/size]
Code (WINBATCH) Select


text = "Compute Hash"
ObjectClrOption ( 'useany', 'System')
ObjectClrOption ( 'useany', 'System.Security')
md5 = ObjectClrNew('System.Security.Cryptography.MD5')
hash = ObjectClrNew('System.Security.Cryptography.HashAlgorithm',md5.Create())
Message(text,hash.Create())



doesn't error to that point... just no where to go.

stanl


hash = ObjectClrNew('System.Security.Cryptography.HashAlgorithm',md5.Create())

could be written as

hash = ObjectClrNew('System.Security.Cryptography.HashAlgorithm',md5)

This was something I tried a year ago, but too embarrassed to post. I think you have to use the System.Bit class to convert text to array then the hash would work.  It was more just part of a general CLR explore... so I gave up.



JTaylor

I am needing to match the hash returned by ColdFusion on a remote site.   They are coming up with a different response.

https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-h-im/hash.html

Below is what I used to obtain the MD5 Hash.  Am I doing something wrong?   Thanks.

Jim

Code (winbatch) Select

#DefineFunction GetMyHash(pwd)

  fs=StrLen(pwd)
  bb=BinaryAlloc(fs)
  BinaryPokeStr(bb,0,pwd)
  MD5=BinaryChecksum(bb,0)
  BinaryFree(bb)
  Return MD5

#EndFunction

td

Just a guess but if you check the documentation of the BinaryCheckSum function, you will notice that the MD5 hash contains two dashes (-). Use StrReplace to removed them.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

I figured it out.   Stupid oversight on my part, as is usually the case.

Jim