hash.ps1
· 1.6 KiB · PowerShell
Raw
param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$File,
[Parameter(Mandatory = $false, Position = 1)]
[ValidateSet("MD5", "SHA1", "SHA256", "SHA384", "SHA512")]
[string]$Algorithm = "SHA384"
)
if (-not (Test-Path -LiteralPath $File -PathType Leaf)) {
Write-Host "ERROR: File tidak ditemukan: $File" -ForegroundColor Red
exit 1
}
$manifestNames = @{
"MD5" = "MD5"
"SHA1" = "SHA-1"
"SHA256" = "SHA-256"
"SHA384" = "SHA-384"
"SHA512" = "SHA-512"
}
try {
$resolvedPath = (Resolve-Path -LiteralPath $File).Path
# Hitung hash
$result = Get-FileHash -LiteralPath $resolvedPath -Algorithm $Algorithm
$hex = $result.Hash
# Convert HEX menjadi raw bytes.
# Kompatibel dengan Windows PowerShell 5.1 dan PowerShell 7+
[byte[]]$bytes = for ($i = 0; $i -lt $hex.Length; $i += 2) {
[Convert]::ToByte($hex.Substring($i, 2), 16)
}
# Raw hash bytes -> Base64
$base64 = [Convert]::ToBase64String($bytes)
$manifestAlgorithm = $manifestNames[$Algorithm]
Write-Host ""
Write-Host "File : " -NoNewline -ForegroundColor DarkGray
Write-Host $resolvedPath
Write-Host "Algorithm : " -NoNewline -ForegroundColor DarkGray
Write-Host $manifestAlgorithm
Write-Host ""
Write-Host "HEX" -ForegroundColor Cyan
Write-Host $hex
Write-Host ""
Write-Host "Base64" -ForegroundColor Green
Write-Host $base64
#Write-Host ""
#Write-Host "MANIFEST.MF" -ForegroundColor Yellow
#Write-Host "${manifestAlgorithm}-Digest: $base64"
Write-Host ""
}
catch {
Write-Host "ERROR: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
| 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 | } |