-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddCompareHash.ps1
executable file
·80 lines (66 loc) · 2.75 KB
/
AddCompareHash.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Get the user's profile path
$profilePath = $PROFILE.CurrentUserCurrentHost
# Check if the profile exists
if (-not (Test-Path -Path $profilePath -PathType Leaf)) {
# Create the profile directory if it doesn't exist
$profileDirectory = Split-Path -Path $profilePath
if (-not (Test-Path -Path $profileDirectory -PathType Container)) {
New-Item -Path $profileDirectory -ItemType Directory -Force
}
# Create the profile file
New-Item -Path $profilePath -ItemType File
}
# Function code
$functionCode = @"
function CompareHash {
param (
[string]`$FilePath,
[string]`$ExpectedHash,
[string]`$HashType
)
`$HashTypes = @("md5", "sha1", "sha256", "sha512")
# Check for help flag
if (`$FilePath -eq "-h" -or `$FilePath -eq "--help" -or `$FilePath -eq "-help") {
Write-Host "Compares two hash values and returns true if they are equal"
Write-Host " Usage: CompareHash /path/to/file expectedHash hashType"
Write-Host " Acceptable hash types: md5, sha1, sha256, sha512"
Write-Host " Example: CompareHash ~\`$Downloads\msys2-x86_64-20231026.exe 5702372d25111e24d59596de62ae24daef873018cbf63c9dd9ff12292a57aca9 sha256"
return
}
if (-not `$FilePath) {
Write-Host "Please provide a file path.\`nEx: CompareHash /path/to/file expectedHash hashType"
return
}
if (-not `$ExpectedHash) {
Write-Host "Please provide the expected hash value.\`nEx: CompareHash /path/to/file expectedHash hashType"
return
}
if (-not `$HashType) {
Write-Host "Please provide a hash type.\`nEx: CompareHash /path/to/file expectedHash hashType"
return
}
if (-not (Test-Path -Path `$FilePath -PathType Leaf)) {
Write-Host "File not found. Please provide the absolute file path."
return
}
if (-not `$HashTypes.Contains(`$HashType)) {
Write-Host "Please provide a valid hash type.\`nAcceptable hash types: md5, sha1, sha256, sha512"
return
}
# Compute the hash using Get-FileHash
`$computedHash = (Get-FileHash -Path `$FilePath -Algorithm `$HashType).Hash
# Compare the computed hash with the expected hash
`$result = `$computedHash.ToLower() -eq `$ExpectedHash.ToLower()
# Output "True" or "False"
Write-Host `$result
}
"@
# Check if the function already exists in the profile
if (-not (Get-Content $profilePath | Select-String -Pattern "function CompareHash")) {
# Write the function code to the profile file
Add-Content -Path $profilePath -Value $functionCode
Write-Host "Function added to the profile successfully."
}
else {
Write-Host "Function already exists in the profile."
}