WinBatch Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: JTaylor on February 22, 2021, 08:07:19 pm

Title: MD5 Hash
Post by: JTaylor 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();  
    }  
  }  
}
 
Title: Re: MD5 Hash
Post by: 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
 (https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/WILAK_B__040.htm)
Title: Re: MD5 Hash
Post by: 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

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.
Title: Re: MD5 Hash
Post by: JTaylor 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
 (https://docs.winbatch.com/mergedProjects/WindowsInterfaceLanguage/html/WILAK_B__040.htm)
Title: Re: MD5 Hash
Post by: JTaylor 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.
Title: Re: MD5 Hash
Post by: stanl 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.


Title: Re: MD5 Hash
Post by: JTaylor 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
 
Title: Re: MD5 Hash
Post by: td 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.
Title: Re: MD5 Hash
Post by: JTaylor on February 23, 2021, 03:22:24 pm
I figured it out.   Stupid oversight on my part, as is usually the case.

Jim