Autoit | Sample code to check file hash value

This time, I will introduce how to obtain the hash value of a file and some sample code created with Autoit.

Simply put, a hash value is a file's identification.

However, hash values ​​are more strict than IDs, being calculated based on file size, time, type, creator, machine, etc., and any slight change will change the hash value.

Hash values ​​are often used to verify file identity.

For example, on this website,Homemade softwareIf the hash value of the software matches the hash value of the software downloaded by the user, then the file is correct. However, if the hash values ​​do not match, then the file cannot be said to be correct, as follows:

  • The file version is different
  • The file is corrupted
  • The file has been tampered with (a third party has rewritten the program)
table of contents

Obtaining the hash value

To get the hash value, use the Windows command "certutil"Use the.

The available hash algorithms are

  • MD2
  • MD4
  • MD5
  • SHA1
  • SHA256
  • SHA384
  • SHA512

There are seven of them.

The command is written as follows:

%systemroot%\System32\certutil.exe -hashfile %path% MD5

The "%path%" part is the file name, and the "MD5" part is the algorithm.

If the file name you specify contains spaces, you must enclose it in double quotation marks ("").

File: “C:\Program Files\WinMyMenu\WinMyMenu.exe” Algorithm: Example of specifying MD5

%systemroot%\System32\certutil.exe -hashfile "C:\Program Files\WinMyMenu\WinMyMenu.exe" MD5

Try opening a command prompt and running it.

Learn how to open a command prompt

1. Press Windows key + R to open Run.

2. Type "cmd".

Run
Run

3.Shift + Ctrl + Enter Please press.

Keyboard
Keyboard

If you do not need administrator privileges, just press Enter.

4. When the "User Account Control" message appears, click "Yes".

ユーザーアカウント制御
ユーザーアカウント制御

Enter the command:

command prompt
command prompt

Press Enter to display the hash value.

command prompt
command prompt

Autoit sample code using the Windows command "certutil"

Sample Code

2022/12/21: Code corrections have been made.

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include-once

Opt("TrayIconHide", 0)

$Form1 = GUICreate("ハッシュ値の確認ツール", 448, 303, 206, 143, -1, BitOR($WS_EX_TOPMOST, $WS_EX_ACCEPTFILES))
GUISetBkColor(0xFFFFFF)
$Radio1 = GUICtrlCreateRadio("MD2", 40, 48, 113, 17)
$Radio2 = GUICtrlCreateRadio("MD4", 40, 80, 113, 17)
$Radio3 = GUICtrlCreateRadio("MD5", 40, 112, 113, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Radio4 = GUICtrlCreateRadio("SHA1", 176, 48, 113, 17)
$Radio5 = GUICtrlCreateRadio("SHA256", 176, 80, 113, 17)
$Radio6 = GUICtrlCreateRadio("SHA384", 176, 112, 113, 17)
$Radio7 = GUICtrlCreateRadio("SHA512", 312, 48, 113, 17)
GUICtrlCreateGroup("", 16, 24, 417, 121)
$Edit1 = GUICtrlCreateEdit("", 16, 160, 417, 77, $ES_AUTOVSCROLL)
GUICtrlSetState($Edit1, $GUI_DROPACCEPTED)
$Button1 = GUICtrlCreateButton("コピー", 328, 252, 105, 33)
$Button2 = GUICtrlCreateButton("リセット", 203, 252, 105, 33)
GUISetState(@SW_SHOW)

If IsAdmin() Then ; 管理者として実行するとドラッグ&ドロップができない問題を回避する
    _ChangeWindowMessageFilterEx($Form1, 0x233, 1) ; $WM_DROPFILES
    _ChangeWindowMessageFilterEx($Form1, $WM_COPYDATA, 1) ; redundant?
    _ChangeWindowMessageFilterEx($Form1, 0x0049, 1) ; $WM_COPYGLOBALDATA
EndIf

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Copy();ハッシュ値をコピーします。
        Case $Button2
            GUICtrlSetData($Edit1, ""); エディットコントロールの文字を削除します。
        Case $GUI_EVENT_DROPPED
            GUICtrlSetData($Edit1, "")
            If Not StringInStr(FileGetAttrib(@GUI_DragFile), "D") Then; フォルダー以外のファイルのみ対応
                CheckRadio(@GUI_DragFile)
            EndIf
    EndSwitch
WEnd

Func CheckRadio($_DragFile)
    Local $txt
    For $i = 1 To 7
        If _IsChecked(Eval("Radio" & $i)) Then; ラジオボタンがチェックされていれば
            $iPID = Run(@ComSpec & ' /c %systemroot%\System32\certutil.exe -hashfile "' & _
                    $_DragFile & '" ' & GUICtrlRead(Eval("Radio" & $i), 1), "", @SW_HIDE, _
                    $STDOUT_CHILD)
            While 1
                $sOutput = StdoutRead($iPID)

                If @error Then ExitLoop
                $txt &= $sOutput
            WEnd

            $String = StringRegExp($txt, "(?m)^([^:]+)\r$", 1)
            If Not @error Then
                GUICtrlSetData($Edit1, GUICtrlRead(Eval("Radio" & $i), 1) & ' ハッシュ値:' & @CRLF & $String[0])
                ExitLoop
            EndIf
        EndIf
    Next
EndFunc   ;==>CheckRadio

Func Copy()
    If GUICtrlRead($Edit1) <> "" Then
        ClipPut(GUICtrlRead($Edit1))
        MsgBox(0, "コピー", "コピーしました", 1, $Form1)
    EndIf
EndFunc   ;==>Copy

Func _IsChecked($iControlID)
    Return BitAND(GUICtrlRead($iControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked

Func _ChangeWindowMessageFilterEx($hwnd, $iMsg, $iAction)
    Local $aCall = DllCall("user32.dll", "bool", "ChangeWindowMessageFilterEx", _
            "hwnd", $hwnd, _
            "dword", $iMsg, _
            "dword", $iAction, _
            "ptr", 0)
    If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
    Return 1
EndFunc   ;==>_ChangeWindowMessageFilterEx

How to use the hash value verification tool

Select the hash algorithm you want to obtain, then drag and drop the file into the edit control to display the hash value.

Check out the video to see it in action.

Hash value verification tool
Hash value verification tool
  • Clicking the "Reset" button will erase the output.
  • You can copy the output by clicking the "Copy" button.
If you found this article helpful, please share it on social media.

Person who wrote this article

Driven by questions arising from my daily PC use and the desire to "do more," I have been pursuing self-study in Windows since around 2008. I am sharing the "aha!" techniques and solutions I discovered through trial and error with the sole purpose of helping you in your PC life.

View profile

Comment:

To comment

[About submissions]
We welcome any questions or information regarding the content of the article.
However, please note that content unrelated to the purpose of the article, criticism of specific individuals or organizations, offensive language,Inappropriate wordsComments containing the above may be deleted or made private without notice at the discretion of the administrator.
Please note that spam may be automatically deleted by anti-spam measures.

CAPTCHA


table of contents