Jump to content

File checksum


Recommended Posts

  • Moderators

FrancoDM,

Or look at the _Crypt_HashFile function, which seems to be exactly what you require.

Perhaps in future you might like to look through the Help file before you post.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

1 hour ago, Melba23 said:

Or look at the _Crypt_HashFile function ...

You are right, I should have mentioned this variant too.

@FrancoDM :

Some time ago I created a small comparison program (only for myself), but it should help you  as well ;).

#include <Crypt.au3>
#include <WinAPI.au3>

Opt("MustDeclareVars", 1)

Global $sFile =  @SystemDir & '\mspaint.exe'  ; <== example
Global $hTimer, $iTimer, $sData

ConsoleWrite('----------------------------------------------------------- ' & @CRLF)
; CRC32 von trancexx :
$hTimer = TimerInit()
$sData = _CRC32ForFile($sFile)
$iTimer = TimerDiff($hTimer)
ConsoleWrite(' CRC32 - trancexx : ' & $sData & @CRLF)
ConsoleWrite(' >>> Time = ' & $iTimer & " ms" & @CRLF)
ConsoleWrite('----------------------------------------------------------- ' & @CRLF)

; MD5 von trancexx :
$hTimer = TimerInit()
$sData = _MD5ForFile($sFile)
$iTimer = TimerDiff($hTimer)
ConsoleWrite(' MD5 - trancexx   : ' & Hex($sData) & @CRLF)
ConsoleWrite(' >>> Time = ' & $iTimer & " ms" & @CRLF)
ConsoleWrite('----------------------------------------------------------- ' & @CRLF)

; MD5 mit Standard AutoIt Crypt.au3 :
_Crypt_Startup()
$hTimer = TimerInit()
$sData = _Crypt_HashFile($sFile, $CALG_MD5)
$iTimer = TimerDiff($hTimer)
ConsoleWrite(' MD5 - _Crypt.au3 : ' & Hex($sData) & @CRLF)
ConsoleWrite(' >>> Time = ' & $iTimer & " ms" & @CRLF)
ConsoleWrite('----------------------------------------------------------- ' & @CRLF)
_Crypt_Shutdown()

;------------------------------------------------------------------------

; #FUNCTION# ;===============================================================================
; Name...........: _CRC32ForFile
; Description ...: Calculates CRC32 value for the specific file.
; Syntax.........: _CRC32ForFile ($sFile)
; Parameters ....: $sFile - Full path to the file to process.
; Return values .: Success - Returns CRC32 value in form of hex string
;                          - Sets @error to 0
;                  Failure - Returns empty string and sets @error:
;                  |1 - CreateFile function or call to it failed.
;                  |2 - CreateFileMapping function or call to it failed.
;                  |3 - MapViewOfFile function or call to it failed.
;                  |4 - RtlComputeCrc32 function or call to it failed.
; Author ........: trancexx
;==========================================================================================
Func _CRC32ForFile($sFile)
    Local $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFileW", _
            "wstr", $sFile, _
            "dword", 0x80000000, _ ; GENERIC_READ
            "dword", 3, _ ; FILE_SHARE_READ|FILE_SHARE_WRITE
            "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]

    $a_hCall = DllCall("kernel32.dll", "ptr", "CreateFileMappingW", _
            "hwnd", $hFile, _
            "dword", 0, _ ; default security descriptor
            "dword", 2, _ ; PAGE_READONLY
            "dword", 0, _
            "dword", 0, _
            "ptr", 0)

    If @error Or Not $a_hCall[0] Then
        DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)
        Return SetError(2, 0, "")
    EndIf

    DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)

    Local $hFileMappingObject = $a_hCall[0]

    $a_hCall = DllCall("kernel32.dll", "ptr", "MapViewOfFile", _
            "hwnd", $hFileMappingObject, _
            "dword", 4, _ ; FILE_MAP_READ
            "dword", 0, _
            "dword", 0, _
            "dword", 0)

    If @error Or Not $a_hCall[0] Then
        DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject)
        Return SetError(3, 0, "")
    EndIf

    Local $pFile = $a_hCall[0]
    Local $iBufferSize = FileGetSize($sFile)

    Local $a_iCall = DllCall("ntdll.dll", "dword", "RtlComputeCrc32", _
            "dword", 0, _
            "ptr", $pFile, _
            "int", $iBufferSize)

    If @error Or Not $a_iCall[0] Then
        DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile)
        DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject)
        Return SetError(4, 0, "")
    EndIf

    DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile)
    DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject)

    Local $iCRC32 = $a_iCall[0]
    Return SetError(0, 0, Hex($iCRC32))
EndFunc   ;==>_CRC32ForFile


; #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

_Crypt_HashFile is probably the easiest to implement.

 

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...