Author Topic: MD5 Hash  (Read 89 times)

JTaylor

  • Pundit
  • *****
  • Posts: 1595
    • Data & Stuff Inc.
MD5 Hash
« on: February 22, 2021, 08:07:19 pm »
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
  ObjectClrOption ( 'useany', 'System')

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





Code: C#


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

  • Tech Support
  • *****
  • Posts: 3634
    • WinBatch
Re: MD5 Hash
« Reply #1 on: February 22, 2021, 09:48:14 pm »
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 1390
Re: MD5 Hash
« Reply #2 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

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

  • Pundit
  • *****
  • Posts: 1595
    • Data & Stuff Inc.
Re: MD5 Hash
« Reply #3 on: February 23, 2021, 06:42:56 am »
Because I was too dumb to search for MD5 in the Help file?

Thanks.

Jim

Why not just use the BinaryCheckSum function?

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


JTaylor

  • Pundit
  • *****
  • Posts: 1595
    • Data & Stuff Inc.
Re: MD5 Hash
« Reply #4 on: February 23, 2021, 06:44:22 am »

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

Jim

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

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

  • Pundit
  • *****
  • Posts: 1390
Re: MD5 Hash
« Reply #5 on: February 23, 2021, 08:31:28 am »

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

  • Pundit
  • *****
  • Posts: 1595
    • Data & Stuff Inc.
Re: MD5 Hash
« Reply #6 on: February 23, 2021, 10:26:18 am »
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
#DefineFunction GetMyHash(pwd)

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

#EndFunction
 

td

  • Tech Support
  • *****
  • Posts: 3634
    • WinBatch
Re: MD5 Hash
« Reply #7 on: February 23, 2021, 12:22:14 pm »
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

  • Pundit
  • *****
  • Posts: 1595
    • Data & Stuff Inc.
Re: MD5 Hash
« Reply #8 on: February 23, 2021, 03:22:24 pm »
I figured it out.   Stupid oversight on my part, as is usually the case.

Jim