#include <Crypt.au3>
#include <WinAPI.au3>

Opt("MustDeclareVars", 1)

Global $sFile =  @SystemDir & '\mspaint.exe'
Global $sData

;------------------------------------------------------------------------
; MD5 von trancexx :
$sData = _MD5ForFile($sFile)
ConsoleWrite(' MD5 - trancexx   : ' & Hex($sData) & @CRLF)
;------------------------------------------------------------------------

;------------------------------------------------------------------------
; MD5 mit Standard AutoIt Crypt.au3 :
_Crypt_Startup()
$sData = _Crypt_HashFile($sFile, $CALG_MD5)
ConsoleWrite(' MD5 - _Crypt.au3 : ' & Hex($sData) & @CRLF & @CRLF)
_Crypt_Shutdown()
;------------------------------------------------------------------------

; #FUNCTION# ;===============================================================================
; Name...........: _MD5ForFile
; Description ...: Calculates MD5 value for the specific file.
; Syntax.........: _MD5ForFile ($sFile)
; Parameters ....: $sFile - Full path to the file to process.
; Return values .: Success - Returns MD5 value in form of binary data
; - Sets @error to 0
; Failure - Returns empty string and sets @error:
;           |1 - CreateFile function or call to it failed.
;           |2 - MD5Init function or call to it failed.
;           |4 - ReadFile function or call to it failed.
;           |5 - MD5Update function or call to it failed.
;           |6 - MD5Final function or call to it failed.
; Author ........: trancexx
;==========================================================================================
Func _MD5ForFile($sFile)
	Local $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFileW", _
                             "wstr", $sFile, _
                             "dword", 0x80000000, _ ; GENERIC_READ
                             "dword", 1, _ ; FILE_SHARE_READ
                             "ptr", 0, _
                             "dword", 3, _ ; OPEN_EXISTING
                             "dword", 0, _ ; SECURITY_ANONYMOUS
                             "ptr", 0)


	If @error Or $a_hCall[0] = -1 Then
		Return SetError(1, 0, "")
	EndIf

	Local $hFile = $a_hCall[0]
	Local $tMD5_CTX = DllStructCreate("dword i[2];" & _
                                      "dword buf[4];" & _
                                      "ubyte in[64];" & _
                                      "ubyte digest[16]")

	DllCall("advapi32.dll", "none", "MD5Init", "ptr", DllStructGetPtr($tMD5_CTX))
	If @error Then
		_WinAPI_CloseHandle($hFile)
		Return SetError(2, 0, "")
	EndIf

	Local $tBuffer = DllStructCreate("byte[524288]")
	Local $pBuffer = DllStructGetPtr($tBuffer), $iSize
	Local $iReadErr = True

	While _WinAPI_ReadFile($hFile, $pBuffer, 524288, $iSize) And $iSize
		$iReadErr = False
		DllCall("advapi32.dll", "none", "MD5Update", _
                "ptr", DllStructGetPtr($tMD5_CTX), _
                "ptr", $pBuffer, _
                "dword", $iSize)
		If @error Then
			_WinAPI_CloseHandle($hFile)
			Return SetError(5, 0, "")
		EndIf
	WEnd

	If $iReadErr Then
		_WinAPI_CloseHandle($hFile)
		Return SetError(4, 0, "")
	EndIf

	DllCall("advapi32.dll", "none", "MD5Final", "ptr", DllStructGetPtr($tMD5_CTX))
	If @error Then
		_WinAPI_CloseHandle($hFile)
		Return SetError(6, 0, "")
	EndIf
	_WinAPI_CloseHandle($hFile)
	Local $sMD5 = DllStructGetData($tMD5_CTX, "digest")
	Return SetError(0, 0, $sMD5)
EndFunc ;==>_MD5ForFile