0

I want to hash the passwort 'HelloWorld' to MD5. Following code is an excerpt from Generating the hash value of a file. The problem is that with the presented code, I need to save the password to a file before hashing it. How can I pass it in memory? I am feeling very uncomfortable with vbs, please excuse me. I do not know what kind of type binary is in vbs.

Option Explicit
MsgBox("Md5 Hash for 'HelloWorld': " & GenerateMD5("HelloWorld"))

Public Function GenerateMD5(ByRef hashInput)
  'hashInput is the plain text hash algorithm input
  Dim oMD5           : Set oMD5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider") 
  oMD5.Initialize()
  Dim baHash         : baHash = oMD5.ComputeHash_2(GetBinaryFile("D:/HASHINPUT.txt"))  
  GenerateMD5 = ByteArrayToHexStr(baHash) 
End Function

Private Function ByteArrayToHexStr(ByVal fByteArray)
    Dim k
    ByteArrayToHexStr = ""
    For k = 1 To Lenb(fByteArray)
        ByteArrayToHexStr = ByteArrayToHexStr & Right("0" & Hex(Ascb(Midb(fByteArray, k, 1))), 2)
    Next
End Function

Private Function GetBinaryFile(filename)
  Dim oStream: Set oStream = CreateObject("ADODB.Stream")
  oStream.Type = 1 'adTypeBinary
  oStream.Open
  oStream.LoadFromFile filename
  GetBinaryFile = oStream.Read
  oStream.Close
  Set oStream = Nothing
End Function
user2366975
  • 4,350
  • 9
  • 47
  • 87

1 Answers1

0

I suspect you need input of data type Byte() for ComputeHash_2(). VBScript can't create that data type by itself, but you should be able to use the ADODB.Stream object for converting a string to a byte array without writing it to a file first. Something like this:

pwd = "foobar"

Set stream = CreateObject("ADODB.Stream")
stream.Mode = 3     'read/write
stream.Type = 2     'text
stream.Charset = "ascii"
stream.Open
stream.WriteText pwd
stream.Position = 0 'rewind
stream.Type = 1     'binary
bytearray = stream.Read
stream.Close
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328