| 
| 
| 
| #211426 - 2016-05-09 04:13 PM  vbscript MD5 conversion issues |  
| Allen   KiX Supporter
 
       
 Registered:  2003-04-19
 Posts: 4562
 Loc:  USA
 | 
I found a vbscript that creates a MD5 hash but have not had luck converting it to kixtart.
 Here is the original vbscript code that does work:
 {edit:  had to change code tag to quotes on the vbscript code... board didnt like it}
 
 
 
 'MD5=151D5E94EE1197E3A3AD8E8089A73884
 spath="C:\Downloads\Firefox Setup 46.0.1.exe"
 wscript.echo bytesToHex(md5hashBytes(GetBytes(sPath)))
 
 function md5hashBytes(aBytes)
 Dim MD5
 set MD5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
 
 MD5.Initialize()
 'Note you MUST use computehash_2 to get the correct version of this method, and the bytes MUST be double wrapped in brackets to ensure they get passed in correctly.
 md5hashBytes = MD5.ComputeHash_2( (aBytes) )
 end function
 
 function stringToUTFBytes(aString)
 Dim UTF8
 Set UTF8 = CreateObject("System.Text.UTF8Encoding")
 stringToUTFBytes = UTF8.GetBytes_4(aString)
 end function
 
 function bytesToHex(aBytes)
 dim hexStr, x
 for x=1 to lenb(aBytes)
 hexStr= hex(ascb(midb( (aBytes),x,1)))
 if len(hexStr)=1 then hexStr="0" & hexStr
 bytesToHex=bytesToHex & hexStr
 next
 end function
 
 Function BytesToBase64(varBytes)
 With CreateObject("MSXML2.DomDocument").CreateElement("b64")
 .dataType = "bin.base64"
 .nodeTypedValue = varBytes
 BytesToBase64 = .Text
 End With
 End Function
 
 Function GetBytes(sPath)
 With CreateObject("Adodb.Stream")
 .Type = 1 ' adTypeBinary
 .Open
 .LoadFromFile sPath
 .Position = 0
 GetBytes = .Read
 .Close
 End With
 End Function
 
 My goal was to create a all in one function to spit out the MD5 hash.  I am getting results but they are not correct.  What's interesting is, the first few digits are correct although there are 0's in between the values.  Once the first few digits are reached, nothing else match the rest of the hash.  There were a few vbscript specific functions that we do not have in kix that I recreated using the scriptcontrol.  I was using dectohex but switched to the hex function in hopes it mattered, it didnt.
 
 
 
  dim $sPath, $ado, $bytes, $MD5, $MD5hashbytes, $x, $hexstring, $bytestohex
  $Spath="C:\Downloads\Firefox Setup 46.0.1.exe"
  $ADO=createobject("Adodb.Stream")
  $ADO.Type=1 ; adTypeBindar
  $ADO.Open
  $ADO.LoadfromFile($Spath)
  $ADO.position=0
  $Bytes=$ADO.Read
  $ADO.close
  $MD5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
  $MD5.Initialize()
  ;Note you MUST use computehash_2 to get the correct version of this method, and the bytes MUST be double wrapped in brackets to ensure they get passed in correctly.
  $md5hashBytes = $MD5.ComputeHash_2( ($Bytes) )
  for $x=1 to lenb($md5hashBytes)
    $hexStr= right("0" + hex(ascb(midb( ($md5hashBytes),$x,1))),2)
	? $hexstr
    $bytesToHex=$bytesToHex + $hexStr
  next
  
  ? $BytestoHex
  
Function ASCB($string)
  Dim $sc
  $sc = CreateObject("ScriptControl")
  $sc.Language = "VBScript"
  $ASCB = $sc.Eval('ASCB("'+ $String + '")')
EndFunction  
Function MidB($string, $start, $length)
  Dim $sc
  $sc = CreateObject("ScriptControl")
  $sc.Language = "VBScript"
  $MidB = $sc.Eval('MIDB("'+ $string + '",' + $Start + ',' + $length +  ')')
EndFunction  
Function LenB($string)
  Dim $sc
  $sc = CreateObject("ScriptControl")
  $sc.Language = "VBScript"
  $LenB = $sc.Eval('LenB("'+ $String + '")')
EndFunction
Function Hex($Number)
  Dim $sc
  $sc = CreateObject("ScriptControl")
  $sc.Language = "VBScript"
  $Hex = $sc.Eval('Hex('+ $Number + ')')
EndFunction
 Any ideas/suggestions?
 
 Edited by Allen (2016-05-09 04:16 PM)
 |  
| Top |  |  |  |  
| 
| 
| #211431 - 2016-05-09 06:20 PM  Re: vbscript MD5 conversion issues
[Re:  Glenn Barnas] |  
| Allen   KiX Supporter
 
       
 Registered:  2003-04-19
 Posts: 4562
 Loc:  USA
 | 
CertUtil has this capability... I didn't know that.   
 If all else fails at least I have that as option.  I would love to see this working though.
 |  
| Top |  |  |  |  
| 
| 
| #211440 - 2016-05-09 09:42 PM  Re: vbscript MD5 conversion issues
[Re:  Lonkero] |  
| Allen   KiX Supporter
 
       
 Registered:  2003-04-19
 Posts: 4562
 Loc:  USA
 | 
I might try to do all of this within scriptcontrol.  I don't know why the exe is so unappealing to me     |  
| Top |  |  |  |  
| 
| 
| #211452 - 2016-05-10 10:18 AM  Re: vbscript MD5 conversion issues
[Re:  Lonkero] |  
| Arend_   MM club member
 
       
   Registered:  2005-01-17
 Posts: 1896
 Loc:  Hilversum, The Netherlands
 | 
Seems interesting, I'm going to try it.So far I get almost the correct value.
 Above is the real, expected value, below is the output of my script.
 
 
dfdc6c74 96	d9645953 89   ed48e229ee81 *KIX32.EXE
DFDC6C74 1320	D9645953 3020 ED48E229EE81
 |  
| Top |  |  |  |  
| 
| 
| #211453 - 2016-05-10 11:01 AM  Re: vbscript MD5 conversion issues
[Re:  Arend_] |  
| Arend_   MM club member
 
       
   Registered:  2005-01-17
 Posts: 1896
 Loc:  Hilversum, The Netherlands
 | 
Please be amazed ;-)
 
$=SetOption('Explicit','On')
Function md5($filename)
  Dim $objXML, $objXEL, $objMD5, $objStream, $BinaryFile
  $objStream = CreateObject("ADODB.Stream")
  $objStream.Type = 1
  $objStream.Open
  $objStream.LoadFromFile($filename)
  $BinaryFile = $objStream.Read
  $objStream.Close
  $objStream = ""
  $objMD5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
  $=$objMD5.ComputeHash_2($BinaryFile)
  $objXML = CreateObject("MSXML2.DOMDocument")
  $objXEL = $objXML.CreateElement("tmp")
  $objXEL.DataType = "bin.hex"
  $objXEL.NodeTypedValue = $objMD5.Hash
  $md5 = $objXEL.Text
EndFunction
? md5("C:\KIX32.EXE")
 |  
| Top |  |  |  |  
| 
| 
| #211454 - 2016-05-10 11:34 AM  Re: vbscript MD5 conversion issues
[Re:  Arend_] |  
| Arend_   MM club member
 
       
   Registered:  2005-01-17
 Posts: 1896
 Loc:  Hilversum, The Netherlands
 | 
Only seems to work with files up to 250 MB.  Probably because of some variable size limit in Kix.
 |  
| Top |  |  |  |  
| 
| 
| #211458 - 2016-05-10 03:26 PM  Re: vbscript MD5 conversion issues
[Re:  Arend_] |  
| Allen   KiX Supporter
 
       
 Registered:  2003-04-19
 Posts: 4562
 Loc:  USA
 | 
Oh wow...  I'll give it whirl. |  
| Top |  |  |  |  
| 
| 
| #211459 - 2016-05-10 03:54 PM  Re: vbscript MD5 conversion issues
[Re:  Allen] |  
| Arend_   MM club member
 
       
   Registered:  2005-01-17
 Posts: 1896
 Loc:  Hilversum, The Netherlands
 | 
And just because it's possible:
 
Function SHA1($strFileName)
  Dim $objXML, $objXEL, $objSHA1, $objStream, $BinaryFile
  $objStream = CreateObject("ADODB.Stream")
  $objStream.Type = 1
  $objStream.Open
  $objStream.LoadFromFile($strFileName)
  $BinaryFile = $objStream.Read
  $objStream.Close
  $objStream = ""
  $objSHA1 = CreateObject("System.Security.Cryptography.SHA1CryptoServiceProvider")
  $=$objSHA1.ComputeHash_2($BinaryFile)
  $objXML = CreateObject("MSXML2.DOMDocument")
  $objXEL = $objXML.CreateElement("tmp")
  $objXEL.DataType = "bin.hex"
  $objXEL.NodeTypedValue = $objSHA1.Hash
  $SHA1 = $objXEL.Text
EndFunction
? SHA1("C:\KIX32.EXE")
 |  
| Top |  |  |  |  
 Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
 
 | 
| 
 
| 0 registered
and 456 anonymous users online. 
 | 
 |  |