You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- refactored auto-detection of algorithm based on its length to be more concise and easier to read
- added a check to ensure mutual exclusivity of '-Algorithms' and '-Expected' now that auto-detection is used
- added a check to remove duplicates of algorithms specified
- clarified wording of some comments and error messages
- minor logic improvements
- rearranged some blocks for better flow
- renamed some variables to improve readability
- updated comment-based help
# Oneshot variable on script scope to ensure column headers of Get-FileHash table are only printed once
87
87
$script:tableHeaders=$false
88
88
89
-
# Lengths of each supported hash type, to validate length of hash passed with '-Expected'. No duplicate lengths allowed, would break '-Expected' algorithm detection.
# Ensure at least two file paths have been provided
93
-
if ($Files.Count-lt2-and-not ($Expected)) { Write-Error"When '-Expected' is not specified, at least two file paths must be provided." ; return }
94
-
95
-
# Ensure supplied paths exist, and are files, not directories
96
-
foreach ($filein$Files) {
97
-
98
-
if (-not (Test-Path$file)) {
99
-
$invalidPath=$true
100
-
Write-Error"Invalid Path: $file"
101
-
102
-
} elseif (-not (Test-Path$file-PathType Leaf)) {
103
-
$invalidPath=$true
104
-
Write-Error"Path is directory, not file: $file"
105
-
}
89
+
# Warn and return if '-Expected' is passed with '-Algorithms'
90
+
if ($Expected-and$Algorithms) {
91
+
Write-Error"When '-Expected' is specified, '-Algorithms' must be omitted.`nThe algorithm of your expected hash will be automatically derived from its length."
92
+
return
106
93
}
107
94
108
-
#Allow all path issues to be printed prior to return
109
-
if ($invalidPath) { return }
95
+
#If '-Algorithms' is unspecified, default to SHA512 (after ensuring '-Expected' + '-Algorithms' mutual exclusivity). Else, remove duplicate objects
96
+
if (-not$Algorithms) { $Algorithms="SHA512" } else { $Algorithms=$Algorithms|Select-Object-Unique }
110
97
111
-
# Automatically detect '-Expected' input hash type based on length
98
+
# Automatically derive algorithm of '-Expected' hash from its length
112
99
if ($Expected) {
113
100
114
101
$Algorithms=@()
115
102
116
-
foreach ($keyin$hashLengths.Keys) {
117
-
if ($hashLengths[$key] -eq$Expected.Length) {
118
-
$Algorithms=$key
119
-
break
103
+
$Algorithms=switch ($Expected.Length) {
104
+
32 { "MD5" }
105
+
40 { "SHA1" }
106
+
64 { "SHA256" }
107
+
96 { "SHA384" }
108
+
128 { "SHA512" }
109
+
default {
110
+
Write-Error"Invalid length of '-Expected' hash ($($Expected.Length) characters). Supported algorithms/lengths are as follows:`n`n"
Write-Error"When '-Expected' is specified, '-Algorithm' is limited to one type."
135
-
return
120
+
# Ensure at least two file paths have been provided, unless '-Expected' is passed
121
+
if ($Files.Count-lt2-and-not$Expected) { Write-Error"When '-Expected' is not specified, at least two file paths must be provided for comparison." ; return }
122
+
123
+
# Ensure supplied paths exist, and are files, not directories
124
+
foreach ($pathin$Files) {
125
+
126
+
if (-not (Test-Path$path)) {
127
+
$invalidPath=$true
128
+
Write-Error"Invalid Path: $path"
129
+
130
+
} elseif (-not (Test-Path$path-PathType Leaf)) {
131
+
$invalidPath=$true
132
+
Write-Error"Path is directory, not file: $path"
133
+
}
136
134
}
137
135
138
-
# If user's algorithm selection contains "All", run all algorithms, else just run what user specifies
0 commit comments