Page 1 of 2 12>
Topic Options
#211426 - 2016-05-09 04:13 PM vbscript MD5 conversion issues
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
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}

 Quote:


'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.

 Code:
  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
#211430 - 2016-05-09 05:12 PM Re: vbscript MD5 conversion issues [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
I checked my lib - I just used md5.exe and called it a day! ;\)

Actually, I did recently update my internal code to call CertUtil, which is built-in on all current Windows platforms (at least XP). It can generate MD2 MD4 MD5 SHA1 SHA256 SHA384 & SHA512 Hash Algorithms.
 Code:
certUtil -hashfile pathToFileToCheck [HashAlgorithm]
I haven't released this update to prod, so my web site still references the md5.exe version, which required the externally obtained command. The code update simply changes CertUtil.exe for MD5.exe, so it's easy to hack if you want to go in that direction.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#211431 - 2016-05-09 06:20 PM Re: vbscript MD5 conversion issues [Re: Glenn Barnas]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
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
#211433 - 2016-05-09 07:47 PM Re: vbscript MD5 conversion issues [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
I hacked around with this about a month ago without much success and finally gave up when I found that CertUtil did this and was on every system I needed to run it on. Wasn't worth my time/effort to pursue at that point.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#211437 - 2016-05-09 09:17 PM Re: vbscript MD5 conversion issues [Re: Glenn Barnas]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
as far as I can remember this totally correct:
kixtart does not support binary content types.
that's why inside kixforms.net all bytes are translated to a string-array.

but, even if it did, you force kixtart to translate the bytes to a string on this line:
 Code:
$MidB = $sc.Eval('MIDB("'+ $string + 


so if the byte-array contains a single zero or special character, you end up messing it up.
_________________________
!

download KiXnet

Top
#211440 - 2016-05-09 09:42 PM Re: vbscript MD5 conversion issues [Re: Lonkero]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
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
#211441 - 2016-05-09 09:43 PM Re: vbscript MD5 conversion issues [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
Loc: USA
BTW, thanks for looking.
Top
#211442 - 2016-05-09 10:07 PM Re: vbscript MD5 conversion issues [Re: Allen]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
oh, another BTW. you can use kixforms to overcome this issue.
_________________________
!

download KiXnet

Top
#211443 - 2016-05-09 10:23 PM Re: vbscript MD5 conversion issues [Re: Lonkero]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
Loc: USA
Yeah... but I don't have kixforms installed on any of the pcs. I've just never invested the time in kixforms, I'm mostly a complete noob with it.
Top
#211449 - 2016-05-10 03:51 AM Re: vbscript MD5 conversion issues [Re: Allen]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
KiXforms is/was the thing of the future for us until Shawn got called away with other more pressing things in life :-(

I've made a few things and worked with Glenn and a few others over the years to make some things in KiXforms but have not used it much myself now in many years.

Top
#211450 - 2016-05-10 03:55 AM Re: vbscript MD5 conversion issues [Re: NTDOC]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
I did re-target kf.net to newer .net version, but the development has completely stalled. Lack of users might be a good excuse.
_________________________
!

download KiXnet

Top
#211452 - 2016-05-10 10:18 AM Re: vbscript MD5 conversion issues [Re: Lonkero]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
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.
 Code:
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_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
Please be amazed ;-)
 Code:
$=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_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
Only seems to work with files up to 250 MB. \:\(
Probably because of some variable size limit in Kix.

Top
#211456 - 2016-05-10 02:24 PM Re: vbscript MD5 conversion issues [Re: Arend_]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
Nice!!!!!!!!!!!

I might be mistaken, but your previous post seemed to me to have correct value as well
_________________________
!

download KiXnet

Top
#211457 - 2016-05-10 02:30 PM Re: vbscript MD5 conversion issues [Re: Lonkero]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
For some reason 4 values were incorrectly identified in the string.
Hence why I dropped the whole loop converting the byte array.

Top
#211458 - 2016-05-10 03:26 PM Re: vbscript MD5 conversion issues [Re: Arend_]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
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_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
And just because it's possible:
 Code:
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
#211460 - 2016-05-10 06:31 PM Re: vbscript MD5 conversion issues [Re: Arend_]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
Loc: USA
 Quote:
Only seems to work with files up to 250 MB. \:\(
Probably because of some variable size limit in Kix.


It's failing here on the +200mb files:
$=$objSHA1.ComputeHash_2( $BinaryFile )
? @serror

Output is:
Not enough storage is available to complete this operation.

I read some articles on breaking the stream into smaller chunks, but it didn't really seem to apply to what we are doing.



Top
#211461 - 2016-05-10 08:36 PM Re: vbscript MD5 conversion issues [Re: Allen]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
not so smart people of microsoft have destroyed all com documentation but with 99% certainty this is a limit of com/adodb.
_________________________
!

download KiXnet

Top
Page 1 of 2 12>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 686 anonymous users online.
Newest Members
Timothy, Jojo67, MaikSimon, kvn317, kixtarts2025
17874 Registered Users

Generated in 0.075 seconds in which 0.026 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org