-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickHash.ps1
83 lines (74 loc) · 2.96 KB
/
QuickHash.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
81
82
83
# Define the main function to calculate hash
function Get-Hash {
param (
[Parameter(Mandatory = $true)]
[string]$InputValue,
[Parameter(Mandatory = $true)]
[ValidateSet("MD5", "SHA256")]
[string]$Algorithm
)
try {
Write-Host "Starting hash calculation for input: $InputValue using algorithm: $Algorithm" -ForegroundColor Cyan
# Check if input is a file path or string
if (Test-Path -Path $InputValue) {
# It's a file path, compute hash of the file
Write-Host "Input is a file. Computing hash of the file..." -ForegroundColor Green
$fileContent = Get-Content -Path $InputValue -Encoding Byte -ReadCount 0
$hash = $null
switch ($Algorithm) {
"MD5" {
$hash = [System.Security.Cryptography.MD5]::Create()
break
}
"SHA256" {
$hash = [System.Security.Cryptography.SHA256]::Create()
break
}
}
$computedHash = $hash.ComputeHash($fileContent)
$hash.Dispose()
$hashString = [BitConverter]::ToString($computedHash) -replace '-'
Write-Host "Hash of the file ($InputValue): $hashString" -ForegroundColor Yellow
}
else {
# It's a string, compute hash of the string
Write-Host "Input is a string. Computing hash of the string..." -ForegroundColor Green
$encodedInput = [System.Text.Encoding]::UTF8.GetBytes($InputValue)
$hash = $null
switch ($Algorithm) {
"MD5" {
$hash = [System.Security.Cryptography.MD5]::Create()
break
}
"SHA256" {
$hash = [System.Security.Cryptography.SHA256]::Create()
break
}
}
$computedHash = $hash.ComputeHash($encodedInput)
$hash.Dispose()
$hashString = [BitConverter]::ToString($computedHash) -replace '-'
Write-Host "Hash of the string ('$InputValue'): $hashString" -ForegroundColor Yellow
}
}
catch {
Write-Host "An error occurred: $_" -ForegroundColor Red
}
}
# Prompt user for input: String or file path
$userInput = Read-Host "Please enter a string or a file path"
if (-not (Test-Path -Path $userInput) -and $userInput -match '^[a-zA-Z0-9\s]+$') {
$isFile = $false
} else {
$isFile = $true
}
# Request algorithm choice
$algorithmChoice = Read-Host "Please choose an algorithm (MD5 or SHA256)"
# Validate algorithm choice
if (-not ($algorithmChoice -in @("MD5", "SHA256"))) {
Write-Host "Invalid algorithm choice. Please select either MD5 or SHA256." -ForegroundColor Red
exit
}
# Call Get-Hash with the appropriate parameters
Get-Hash -InputValue $userInput -Algorithm $algorithmChoice
Write-Host "Process completed." -ForegroundColor Cyan