dartokloning 修订了这个 Gist . 跳至此修订
没有变更
dartokloning 修订了这个 Gist . 跳至此修订
1 file changed, 65 insertions
hash.ps1(file created)
| @@ -0,0 +1,65 @@ | |||
| 1 | + | param( | |
| 2 | + | [Parameter(Mandatory = $true, Position = 0)] | |
| 3 | + | [string]$File, | |
| 4 | + | ||
| 5 | + | [Parameter(Mandatory = $false, Position = 1)] | |
| 6 | + | [ValidateSet("MD5", "SHA1", "SHA256", "SHA384", "SHA512")] | |
| 7 | + | [string]$Algorithm = "SHA384" | |
| 8 | + | ) | |
| 9 | + | ||
| 10 | + | if (-not (Test-Path -LiteralPath $File -PathType Leaf)) { | |
| 11 | + | Write-Host "ERROR: File tidak ditemukan: $File" -ForegroundColor Red | |
| 12 | + | exit 1 | |
| 13 | + | } | |
| 14 | + | ||
| 15 | + | $manifestNames = @{ | |
| 16 | + | "MD5" = "MD5" | |
| 17 | + | "SHA1" = "SHA-1" | |
| 18 | + | "SHA256" = "SHA-256" | |
| 19 | + | "SHA384" = "SHA-384" | |
| 20 | + | "SHA512" = "SHA-512" | |
| 21 | + | } | |
| 22 | + | ||
| 23 | + | try { | |
| 24 | + | $resolvedPath = (Resolve-Path -LiteralPath $File).Path | |
| 25 | + | ||
| 26 | + | # Hitung hash | |
| 27 | + | $result = Get-FileHash -LiteralPath $resolvedPath -Algorithm $Algorithm | |
| 28 | + | $hex = $result.Hash | |
| 29 | + | ||
| 30 | + | # Convert HEX menjadi raw bytes. | |
| 31 | + | # Kompatibel dengan Windows PowerShell 5.1 dan PowerShell 7+ | |
| 32 | + | [byte[]]$bytes = for ($i = 0; $i -lt $hex.Length; $i += 2) { | |
| 33 | + | [Convert]::ToByte($hex.Substring($i, 2), 16) | |
| 34 | + | } | |
| 35 | + | ||
| 36 | + | # Raw hash bytes -> Base64 | |
| 37 | + | $base64 = [Convert]::ToBase64String($bytes) | |
| 38 | + | ||
| 39 | + | $manifestAlgorithm = $manifestNames[$Algorithm] | |
| 40 | + | ||
| 41 | + | Write-Host "" | |
| 42 | + | Write-Host "File : " -NoNewline -ForegroundColor DarkGray | |
| 43 | + | Write-Host $resolvedPath | |
| 44 | + | ||
| 45 | + | Write-Host "Algorithm : " -NoNewline -ForegroundColor DarkGray | |
| 46 | + | Write-Host $manifestAlgorithm | |
| 47 | + | ||
| 48 | + | Write-Host "" | |
| 49 | + | Write-Host "HEX" -ForegroundColor Cyan | |
| 50 | + | Write-Host $hex | |
| 51 | + | ||
| 52 | + | Write-Host "" | |
| 53 | + | Write-Host "Base64" -ForegroundColor Green | |
| 54 | + | Write-Host $base64 | |
| 55 | + | ||
| 56 | + | #Write-Host "" | |
| 57 | + | #Write-Host "MANIFEST.MF" -ForegroundColor Yellow | |
| 58 | + | #Write-Host "${manifestAlgorithm}-Digest: $base64" | |
| 59 | + | ||
| 60 | + | Write-Host "" | |
| 61 | + | } | |
| 62 | + | catch { | |
| 63 | + | Write-Host "ERROR: $($_.Exception.Message)" -ForegroundColor Red | |
| 64 | + | exit 1 | |
| 65 | + | } | |
更新
更早