Naposledy aktivní 1791324749

Put this in your Environment Path of your windows and run with command: hash <filename> <cryptographic>. Example: hash .\refurbished_furniture.common.mixins.json sha384

hash.ps1 Raw
1param(
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
10if (-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
23try {
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}
62catch {
63 Write-Host "ERROR: $($_.Exception.Message)" -ForegroundColor Red
64 exit 1
65}