Page 2 of 2 <12
Topic Options
#211464 - 2016-05-11 10:12 AM Re: vbscript MD5 conversion issues [Re: Lonkero]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1880
Loc: Hilversum, The Netherlands
I've tried to get around the file size limit but to no avail.
Instead I've added SHA256, SHA384 en SHA512 functions :P
 Code:
Function SHA256($strFileName)
  Dim $objXML, $objXEL, $objSHA256, $objStream, $BinaryFile
  $objStream = CreateObject("ADODB.Stream")
  $objStream.Type = 1
  $objStream.Open
  $objStream.LoadFromFile($strFileName)
  $BinaryFile = $objStream.Read
  $objStream.Close
  $objStream = ""
  $objSHA256 = CreateObject("System.Security.Cryptography.SHA256Managed")
  $=$objSHA256.ComputeHash_2($BinaryFile)
  $objXML = CreateObject("MSXML2.DOMDocument")
  $objXEL = $objXML.CreateElement("tmp")
  $objXEL.DataType = "bin.hex"
  $objXEL.NodeTypedValue = $objSHA256.Hash
  $SHA256 = $objXEL.Text
EndFunction

? "SHA256: "+SHA256("D:\Firefox Setup 46.0.1.exe")

 Code:
Function SHA384($strFileName)
  Dim $objXML, $objXEL, $objSHA384, $objStream, $BinaryFile
  $objStream = CreateObject("ADODB.Stream")
  $objStream.Type = 1
  $objStream.Open
  $objStream.LoadFromFile($strFileName)
  $BinaryFile = $objStream.Read
  $objStream.Close
  $objStream = ""
  $objSHA384 = CreateObject("System.Security.Cryptography.SHA384Managed")
  $=$objSHA384.ComputeHash_2($BinaryFile)
  $objXML = CreateObject("MSXML2.DOMDocument")
  $objXEL = $objXML.CreateElement("tmp")
  $objXEL.DataType = "bin.hex"
  $objXEL.NodeTypedValue = $objSHA384.Hash
  $SHA384 = $objXEL.Text
EndFunction

? "SHA384: "+SHA384("D:\Firefox Setup 46.0.1.exe")

 Code:
Function SHA512($strFileName)
  Dim $objXML, $objXEL, $objSHA512, $objStream, $BinaryFile
  $objStream = CreateObject("ADODB.Stream")
  $objStream.Type = 1
  $objStream.Open
  $objStream.LoadFromFile($strFileName)
  $BinaryFile = $objStream.Read
  $objStream.Close
  $objStream = ""
  $objSHA512 = CreateObject("System.Security.Cryptography.SHA512Managed")
  $=$objSHA512.ComputeHash_2($BinaryFile)
  $objXML = CreateObject("MSXML2.DOMDocument")
  $objXEL = $objXML.CreateElement("tmp")
  $objXEL.DataType = "bin.hex"
  $objXEL.NodeTypedValue = $objSHA512.Hash
  $SHA512 = $objXEL.Text
EndFunction

? "SHA512: "+SHA512("D:\Firefox Setup 46.0.1.exe")

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

Registered: 2005-01-17
Posts: 1880
Loc: Hilversum, The Netherlands
And I've just combined all in 1 function, enjoy \:\)
 Code:
Function Hash($strFileName, optional $strHashType)
  Dim $objXML, $objXEL, $objHash, $objStream, $BinaryFile
  $objStream = CreateObject("ADODB.Stream")
  $objStream.Type = 1
  $objStream.Open
  $objStream.LoadFromFile($strFileName)
  $BinaryFile = $objStream.Read
  $objStream.Close
  $objStream = ""
  Select
    Case $strHashType = "MD5"
      $objHash = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
    Case $strHashType = "SHA1"
      $objHash = CreateObject("System.Security.Cryptography.SHA1CryptoServiceProvider")
    Case $strHashType = "SHA256"
      $objHash = CreateObject("System.Security.Cryptography.SHA256Managed")
    Case $strHashType = "SHA384"
      $objHash = CreateObject("System.Security.Cryptography.SHA384Managed")
    Case $strHashType = "SHA512"
      $objHash = CreateObject("System.Security.Cryptography.SHA512Managed")
    Case 1
      $objHash = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
  EndSelect
  $RC=$objHash.ComputeHash_2($BinaryFile)
  $objXML = CreateObject("MSXML2.DOMDocument")
  $objXEL = $objXML.CreateElement("tmp")
  $objXEL.DataType = "bin.hex"
  $objXEL.NodeTypedValue = $objHash.Hash
  $Hash = $objXEL.Text
EndFunction

? "Hash: "+Hash("D:\Firefox Setup 46.0.1.exe", "md5")

Top
#211467 - 2016-05-11 04:01 PM Re: vbscript MD5 conversion issues [Re: Arend_]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22343
Loc: OK
nice. thanks arend.
_________________________
!

download KiXnet

Top
#211469 - 2016-05-11 04:34 PM Re: vbscript MD5 conversion issues [Re: Lonkero]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4347
Loc: New Jersey
Nice job!!

If you're going to place this into the UDF forum, please consider a test for large files and exit with an error if a file size exceeds the allowed maximum size (and a null value returned). Also, a name other than "Hash" would be best as there is already a UDF with that name that mimics the Perl hash function, which is something else entirely. (maybe CryptoHash?)

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

Top
#211470 - 2016-05-11 06:05 PM Re: vbscript MD5 conversion issues [Re: Glenn Barnas]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4503
Loc: USA
CheckSum() would also work as a the function name.
Top
#211472 - 2016-05-12 10:52 AM Re: vbscript MD5 conversion issues [Re: Allen]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1880
Loc: Hilversum, The Netherlands
Done.
Top
#211475 - 2016-05-12 05:19 PM Re: vbscript MD5 conversion issues [Re: Arend_]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1880
Loc: Hilversum, The Netherlands
Well I have figured out why the size matters (Yes ladies, it does ;\) ).
Apparently it is a memory limit.
To fix this, you have to feed the bytes to the System.Security.Cryptography object in blocks using the .TransFormBlocks and eventually the .TransFormFinalBlocks property of the object before using the ComputeHash property.
The only problem I see is to get the stream reader to split it up in chucks of less then 200 MB.
If I get more time I'll try to sort it out.

Top
#211477 - 2016-05-13 11:31 AM Re: vbscript MD5 conversion issues [Re: Arend_]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1880
Loc: Hilversum, The Netherlands
Well, even using TransFormBlock it still has the size problem.
Apparently the size problem lies within the System.Security.Cryptography.MD5CryptoServiceProvider.
 Code:
Function MD5($strFileName)
  Dim $adTypeBinary, $objStream, $objXML, $objXEL, $objMD5, $i, $intFinalBlock, $BinaryFile
  $adTypeBinary = 1
  $objStream = CreateObject("ADODB.Stream")
  $objStream.Type = $adTypeBinary
  $objStream.Open
  $objStream.LoadFromFile($strFileName)
  $objMD5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
  $objMD5.Initialize
  $BinaryFile = $objStream.Read
  For $i=0 to $objStream.Size Step 31744
    $=$objMD5.TransformBlock($BinaryFile, $i, 31744, $BinaryFile, $i)
    If ($objStream.Size -$i) < 31744
      $intFinalBlock = ($objStream.Size -$i)
      $=$objMD5.TransformFinalBlock($BinaryFile, $i, $intFinalBlock)
    EndIf
  Next
  $objXML = CreateObject("MSXML2.DOMDocument")
  $objXEL = $objXML.CreateElement("tmp")
  $objXEL.DataType = "bin.hex"
  $objXEL.NodeTypedValue = $objMD5.Hash
  $MD5 = $objXEL.Text
  $objStream.Close
  $objStream = ""
EndFunction


The last option is to read the Stream in chunks and feed the chunks to TransformBlock.

Top
#211480 - 2016-05-13 12:52 PM Re: vbscript MD5 conversion issues [Re: Arend_]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1880
Loc: Hilversum, The Netherlands
I was right, I changed the code and updated the UDF, CheckSum now works for all sizes.
Have a nice day!

Top
#211482 - 2016-05-13 03:52 PM Re: vbscript MD5 conversion issues [Re: Arend_]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4503
Loc: USA
Nice work!
Top
#211483 - 2016-05-13 03:53 PM Re: vbscript MD5 conversion issues [Re: Allen]
ShaneEP Moderator Online   content
MM club member
*****

Registered: 2002-11-29
Posts: 2092
Loc: Tulsa, OK
Well done sir.
Top
#211486 - 2016-05-13 04:10 PM Re: vbscript MD5 conversion issues [Re: ShaneEP]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1880
Loc: Hilversum, The Netherlands
Thanks \:\)
Couldn't be done without Allen's initial work, thanks Allen!

Top
#211498 - 2016-05-13 10:53 PM Re: vbscript MD5 conversion issues [Re: Arend_]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11598
Loc: CA
Very nice :thumbs up:
Top
Page 2 of 2 <12


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

Who's Online
1 registered (Mart) and 127 anonymous users online.
Newest Members
multiman, simonedsouza, amarawatt, harry_hs, johnjorge
17730 Registered Users

Generated in 0.039 seconds in which 0.011 seconds were spent on a total of 14 queries. Zlib compression enabled.

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