將 MD5 計算功能整合至 Windows 檔案右鍵選單,單擊即可計算任意檔案雜湊值並自動寫入剪貼簿,支援大檔案流式處理,基於 CAPICOM 元件實現零依賴(系統內建)加密雜湊。
' === GetMD5.reg ===
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\HASH(&G)\command]
@="WScript.exe //nologo C:\\Windows\\System32\\GetMD5.vbs \"%1\""
關鍵點:
•HKEY_CLASSES_ROOT\*針對所有檔案類型
•&G設定熱鍵 Alt+G 快速觸發
•//nologo隱藏腳本宿主版權資訊
• 路徑雙反斜線轉義
' === GetMD5.vbs (放置於 C:\Windows\System32\) ===
Public MD5Value
' 入口:接收檔案路徑參數
MD5Sum WScript.Arguments(0)
' 結果寫入剪貼簿:格式 "MD5: , FilePath: "
SetClipboardText "MD5: " & MD5Value & ", FilePath: " & WScript.Arguments(0)
' === MD5 計算核心:ADODB.Stream + CAPICOM.HashedData ===
Function MD5Sum(filePath)
Dim stream, hashed
Set stream = CreateObject("ADODB.Stream")
stream.Type = 1 ' adTypeBinary
stream.Open
stream.LoadFromFile filePath
Set hashed = CreateObject("CAPICOM.HashedData")
hashed.Algorithm = 3 ' CAPICOM_HASH_ALGORITHM_MD5
hashed.Hash stream.Read
stream.Close
MD5Value = hashed.Value ' 返回十六進制字串
End Function
' === 剪貼簿寫入:透過 Word.Application 繞過 VBScript 無原生剪貼簿支援 ===
Sub SetClipboardText(text)
Dim word
Set word = CreateObject("Word.Application")
word.Documents.Add
word.Selection.Text = text
word.Selection.Copy
word.Quit False ' 不儲存變更
End Sub
| 元件 | ProgID | 來源 | 備註 |
|---|---|---|---|
| ADODB.Stream | ADODB.Stream | MDAC/Windows 內建 | 二進制檔案讀取 |
| CAPICOM.HashedData | CAPICOM.HashedData | CAPICOM 2.1+ | XP/2003 內建,Vista+ 需安裝 |
| Word.Application | Word.Application | Microsoft Office | 剪貼簿代理,需安裝 Office |
無 Office 環境替代方案:使用 htmlfile COM 物件實現剪貼簿存取(無需 Office):
Sub SetClipboardText_NoOffice(text)
Dim html
Set html = CreateObject("htmlfile")
html.ParentWindow.ClipboardData.SetData "text", text
End Sub
當前實現 stream.Read 一次性讀取整個檔案到記憶體,大檔案(>500MB)可能導致記憶體壓力。改進為分塊雜湊:
Function MD5Sum_Streaming(filePath, Optional chunkSize = 1048576) ' 1MB chunks
Dim stream, hashed, buffer, bytesRead
Set stream = CreateObject("ADODB.Stream")
stream.Type = 1
stream.Open
stream.LoadFromFile filePath
Set hashed = CreateObject("CAPICOM.HashedData")
hashed.Algorithm = 3
Do Until stream.EOS
buffer = stream.Read(chunkSize)
hashed.Hash buffer
Loop
stream.Close
MD5Value = hashed.Value
End Function
Function HashFile(filePath, algorithm)
' algorithm: 0=SHA1, 1=SHA256, 2=SHA384, 3=SHA512, 4=MD5 (CAPICOM 常數)
Dim stream, hashed
Set stream = CreateObject("ADODB.Stream")
stream.Type = 1: stream.Open: stream.LoadFromFile filePath
Set hashed = CreateObject("CAPICOM.HashedData")
hashed.Algorithm = algorithm
hashed.Hash stream.Read
stream.Close
HashFile = hashed.Value
End Function
' 右鍵選單擴展:多算法子選單
' [HKEY_CLASSES_ROOT\*\shell\HASH]
' [HKEY_CLASSES_ROOT\*\shell\HASH\command] @="..." (主腳本判斷參數決定算法)
' [HKEY_CLASSES_ROOT\*\shell\HASH\shell\MD5] @="MD5"
' [HKEY_CLASSES_ROOT\*\shell\HASH\shell\SHA256] @="SHA-256"
' 命令列測試
cscript //nologo C:\Windows\System32\GetMD5.vbs "C:\Windows\notepad.exe"
' 輸出範例:剪貼簿獲得 "MD5: 1A2B3C4D5E6F..., FilePath: C:\Windows\notepad.exe"
' 驗證工具
certutil -hashfile "C:\Windows\notepad.exe" MD5
' 比對輸出是否一致
Get-FileHash 或 .NET System.Security.Cryptography。# Register-FileHashContextMenu.ps1 (需管理員權限)
$scriptBlock = {
param($file)
$hash = (Get-FileHash -Algorithm MD5 -Path $file).Hash
Set-Clipboard -Value "MD5: $hash, FilePath: $file"
Write-Host "Copied: MD5: $hash"
}
$regPath = "HKCR:\*\shell\Get-MD5"
New-Item -Path $regPath -Force | Out-Null
New-ItemProperty -Path $regPath -Name "Icon" -Value "C:\Windows\System32\certutil.exe" -Force
Set-ItemProperty -Path "$regPath\command" -Name "(Default)" -Value "powershell.exe -NoProfile -WindowStyle Hidden -Command & { $f='%1'; $h=(Get-FileHash -Algorithm MD5 -Path `$f).Hash; Set-Clipboard -Value \"MD5: `$h, FilePath: `$f\" }" -Force
Write-Host "Context menu registered. Right-click any file -> Get-MD5"
總結:VBScript 方案展示了 Windows 腳本自動化的經典模式(註冊表 + COM 元件 + 剪貼簿技巧),但受限於元件老舊、效能與安全性。生產環境強烈建議遷移至 PowerShell 7+ 原生實現。