;; 
;;=====================================================================================----- 
;; 
;;Function	CheckSumK() 
;; 
;;Author	Arend Pronk 
;; 
;;Contributors	Allen Powell (This UDF is based on his initial translation work) 
;;		Glenn Barnas (Suggestion for string hashing) 
;;              Joöel Nieminen (Convincing me to write an additional argument for string hashing) 
;;		(Adapted to ITCG Coding Standards - 2016/06/22) 
;; 
;;Action	Generates MD5, SHA-1, SHA-384, SHA-256 & SHA-512 hashes from a file 
;; 
;;Version	1.4 - 2016/06/22 
;; 
;;History	1.0 -            - Initial Release 
;;		1.1 -            - Fixed file size limitation 
;;		1.2 -            - Fixed logical error 
;;              1.3 -            - Added support for string hashing 
;;	        1.4 - 2016/06/22 - Added argument for string hashing; Update to ITCG standards 
;; 
;;Syntax	CheckSumK($strFileName, optional $strHashType) 
;; 
;;Parameters 
;;		$strFileName	(Required)	The location of the file to be hashed 
;;		$strHashType	(Optional)	The type of Hash you want to generate (defaults to MD5) 
;;		$strInputType   (optional)	"string" forces string hash 
;; 
;;Returns	The hash generated by the specified type. 
;; 
;;Dependencies	System.Security.Cryptography, ADODB, MSXML, DOT-NET 3.5 
;; 
;;Examples 
;; 
;;		"MD5: "+CheckSum("C:\Kix32.exe") ? 
;; 		"MD5: "+CheckSum("Hello") ? 
;; 		"SHA1: "+CheckSum("C:\Kix32.exe", "SHA1") ? 
;; 		"SHA1: "+CheckSum("Hello", "SHA1") ? 
;; 		"SHA256: "+CheckSum("C:\Kix32.exe", "SHA256") ? 
;; 		"SHA256: "+CheckSum("Hello", "SHA256") ? 
;; 		"SHA384: "+CheckSum("C:\Kix32.exe", "SHA384") ? 
;; 		"SHA384: "+CheckSum("Hello", "SHA384") ? 
;; 		"SHA512: "+CheckSum("C:\Kix32.exe", "SHA512") ? 
;; 		"SHA512: "+CheckSum("Hello", "SHA512") ? 
; 
; 
Function CheckSumK($_strInput, OPTIONAL $_strHashType, OPTIONAL $_strInputType)
 
  Dim $_objXML						;  
  Dim $_objXEL						;  
  Dim $_objHash						; object reference 
  Dim $_objStream					; file datastream object 
  Dim $_objtext						; string object 
  Dim $_BinaryFile					; incremental read 
  Dim $_i						; index pointer 
  Dim $_intFinalBlock					;  
  Dim $_byteString					;  
  Dim $_Rc						; Return Code Catcher 
 
  If NOT Exist($_strInput) AND NOT $_strInputType = "string"
    Exit 2 						;The system cannot find the file specified. 
  EndIf
 
  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
 
  $_objHash.Initialize
 
  If Exist($_strInput) And Not $_strInputType = 'string'
    $_objStream = CreateObject('ADODB.Stream')
    $_objStream.Type = 1
    $_objStream.Open
    $_objStream.LoadFromFile($_strInput)
    If $_objStream.Size > 209715200
      For $_i = 0 to $_objStream.Size Step 31744
        $_BinaryFile = $_objStream.Read(31744)
        $_Rc = $_objHash.TransformBlock($_BinaryFile, 0, 31744, $_BinaryFile, 0)
        If ($_objStream.Size -$_i) <= 31744
          $_intFinalBlock = ($_objStream.Size -$_i)
          $_Rc = $_objHash.TransformFinalBlock($_BinaryFile, 0, $_intFinalBlock)
        EndIf
      Next
    Else
      $_BinaryFile = $_objStream.Read
      $_Rc = $_objHash.ComputeHash_2($_BinaryFile)
    EndIf
    $_objStream.Close
    $_objStream = ''
  EndIf
 
  If $_strInputType = 'string'
    $_objText = CreateObject('System.Text.UTF8Encoding')
    $_byteString = $_objText.GetBytes_4($_strInput)
    $_Rc = $_objHash.ComputeHash_2($_byteString)
  EndIf
 
  $_objXML = CreateObject('MSXML2.DOMDocument')
  $_objXEL = $_objXML.CreateElement('tmp')
  $_objXEL.DataType = 'bin.hex'
  $_objXEL.NodeTypedValue = $_objHash.Hash
  $CheckSumK = $_objXEL.Text
 
  Exit 0
 
EndFunction