Jump to content

[Solved] _Crypt_HashFile()


Trong
 Share

Recommended Posts

File Hash Generator Working script:

#RequireAdmin
#include <Crypt.au3>
#include <File.au3>

Global $DirToFindFile = @ScriptDir

_Crypt_Startup()

Global $aListFile = _FileListToArrayRec($DirToFindFile, "*", 1, 1, 2, 2)
For $i = 1 To $aListFile[0]
    ToolTip($i & "/" & $aListFile[0] & " - See Console")
    ConsoleWrite("-File : " & $aListFile[$i] & @CRLF)
    ConsoleWrite("MD2   : " & _HASH_Calculator($aListFile[$i], "MD2") & " - Error: " & @error & @CRLF)
    ConsoleWrite("MD5   : " & _HASH_Calculator($aListFile[$i], "MD4") & " - Error: " & @error & @CRLF)
    ConsoleWrite("MD5   : " & _HASH_Calculator($aListFile[$i], "MD5") & " - Error: " & @error & @CRLF)
    ConsoleWrite("SHA1  : " & _HASH_Calculator($aListFile[$i], "SHA1") & " - Error: " & @error & @CRLF)
    ConsoleWrite("SHA256: " & _HASH_Calculator($aListFile[$i], "SHA256") & " - Error: " & @error & @CRLF)
    ConsoleWrite("SHA384: " & _HASH_Calculator($aListFile[$i], "SHA384") & " - Error: " & @error & @CRLF)
    ConsoleWrite("SHA512: " & _HASH_Calculator($aListFile[$i], "SHA512") & " - Error: " & @error & @CRLF)
    ConsoleWrite("CRC32 : " & _HASH_Calculator($aListFile[$i], "CRC32") & " - Error: " & @error & @CRLF)
Next

Func _HASH_Calculator($sFilePath, $sHashType = "CRC32", $ctrlSetDataText = 0, $ctrlSetPercent = 0)
;~  ConsoleWrite("! " &$sFilePath & @CRLF)
    Local $iHash = "", $iError = 0
    $sHashType = StringUpper(StringStripWS($sHashType, 8))
    If $ctrlSetDataText Then GUICtrlSetData($ctrlSetDataText, ' Calculator [' & $sHashType & ']: ' & $sFilePath)
    Switch $sHashType
        Case "MD2"
            $iHash = _Crypt_HashFile($sFilePath, $CALG_MD2)
        Case "MD4"
            $iHash = _Crypt_HashFile($sFilePath, $CALG_MD4)
        Case "MD5"
            $iHash = _Crypt_HashFile($sFilePath, $CALG_MD5)
        Case "SHA1"
            $iHash = _Crypt_HashFile($sFilePath, $CALG_SHA1)
        Case "SHA256"
            $iHash = _Crypt_HashFile($sFilePath, $CALG_SHA_256)
        Case "SHA384"
            $iHash = _Crypt_HashFile($sFilePath, $CALG_SHA_384)
        Case "SHA512"
            $iHash = _Crypt_HashFile($sFilePath, $CALG_SHA_512)
        Case Else
            $iHash = _CRC32_Calculator($sFilePath)
    EndSwitch
    $iError = @error
    If (StringLeft($iHash, 2) = "0x") Then $iHash = StringTrimLeft($iHash, 2)
    If $ctrlSetDataText Then GUICtrlSetData($ctrlSetDataText, ' DONE ')
    If ($iError > 0) And ($iHash = "") Then Return SetError($iError, 0, "- ERROR: " & $iError)
    Return SetError($iError, 0, StringUpper($iHash))
EndFunc   ;==>_HASH_Calculator
Func _CRC32_Calculator($sFilePath)
    Local $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFileW", "wstr", $sFilePath, "dword", 0x80000000, "dword", 3, "ptr", 0, "dword", 3, "dword", 0, "ptr", 0)
    If @error Or ($a_hCall[0] = -1) Then Return SetError(1, 0, "")
    Local $hFile = $a_hCall[0]
    $a_hCall = DllCall("kernel32.dll", "ptr", "CreateFileMappingW", "hwnd", $hFile, "dword", 0, "dword", 2, "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, "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($sFilePath)
    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, 8))
EndFunc   ;==>_CRC32_Calculator

 

 

 

Edited by VIP
[Solved]

Regards,
 

Link to comment
Share on other sites

Return SetError(@error, 0, StringUpper(StringReplace($HashFile, "0x", "")))

replace it by 

Return SetError(@error, 0, StringTrimLeft($HashFile, 2))

and comparing a string with 0, not a good idea

If ($iHASH = "") Or ($iHASH = 0) Then Return SetError(1, 0, "")

change to simple 

If $iHASH = "" Then Return SetError(1, 0, "")

 

Also  _Crypt_HashFile does a _Crypt_startup and shutdown

Edited by Nine
Link to comment
Share on other sites

  • 3 months later...

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

×
×
  • Create New...