Trong Posted December 28, 2018 Share Posted December 28, 2018 (edited) File Hash Generator Working script: expandcollapse popup#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 December 28, 2018 by VIP [Solved] Regards, Link to comment Share on other sites More sharing options...
Nine Posted December 28, 2018 Share Posted December 28, 2018 (edited) 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 December 28, 2018 by Nine Trong 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Trong Posted December 28, 2018 Author Share Posted December 28, 2018 Thank Nine, my mistake comparing strings to zeros! Regards, Link to comment Share on other sites More sharing options...
Nine Posted December 28, 2018 Share Posted December 28, 2018 16 minutes ago, VIP said: my mistake comparing strings to zeros! Happens to the bests “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
dmob Posted December 29, 2018 Share Posted December 29, 2018 (edited) 21 hours ago, Nine said: If ($iHASH = "") Or ($iHASH = 0) Then Return SetError(1, 0, "") If Not $iHASH Then Return SetError(1, 0, "") Wouldn't this handle them both correctly? Edited December 29, 2018 by dmob Wrong quote. Link to comment Share on other sites More sharing options...
adamdevine Posted April 26, 2019 Share Posted April 26, 2019 Thanks Nine, Your comment resolve my issue. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now