diff --git a/Windows-Scripts/checksum.ps1 b/Windows-Scripts/checksum.ps1 index b1f1296..05d1de7 100644 --- a/Windows-Scripts/checksum.ps1 +++ b/Windows-Scripts/checksum.ps1 @@ -5,33 +5,33 @@ param( [Parameter(Position = 0, ParameterSetName = 'Match', ValueFromPipeline = $true)] [string[]]$FilePath, - # Output file (-o / --outfile / -OutFile) + # Output file (-o / -OutFile) [Parameter(Mandatory = $false, ParameterSetName = 'Generate')] [Alias('o')] [string]$OutFile, - # File containing checksums to verify (-c / --check) + # File containing checksums to verify (-c / -Check) [Parameter(Mandatory = $false, ParameterSetName = 'Verify')] [Alias('c')] [string]$Check, - # Direct checksum hash string to match against (-m / --match) + # Direct checksum hash string to match against (-m / -Match) [Parameter(Mandatory = $true, ParameterSetName = 'Match')] [Alias('m')] [string]$Match, # Hashing algorithm to use (Defaults to SHA256) [Parameter(Mandatory = $false)] - [Alias('a', 'algo', 'algorithm')] + [Alias('a', 'algo')] [ValidateSet('SHA1', 'SHA256', 'SHA384', 'SHA512', 'MD5')] [string]$Algorithm, - # Recursive flag (-r / --recursive) + # Recursive flag (-r / -Recursive) [Parameter(Mandatory = $false)] [Alias('r')] [switch]$Recursive, - # Help flag (-h / --help) + # Help flag (-h / -Help) [Parameter(Mandatory = $false)] [Alias('h')] [switch]$Help @@ -52,35 +52,34 @@ SYNOPSIS DESCRIPTION Computes or verifies checksums using cryptographic hash algorithms (SHA256, SHA512, SHA1, MD5, SHA384). Outputs hash values in standard GNU coreutils format ( ). - Supports both single-dash (-o, -c, -r) and double-dash (--outfile, --check, --recursive) GNU-style flags. Ignores checksum metadata files (.md5, .sha1, .sha256, .sha384, .sha512) during wildcards unless passed explicitly. PARAMETERS -FilePath Specifies the target file(s) or wildcard pattern (*). - -o, --outfile, -OutFile + -o, -OutFile Enables auto-saving checksum results to file(s). - If passed with an argument (e.g. -o custom.txt), writes to 'custom.txt'. - If passed with NO argument (or empty string ""), automatically outputs to 'checksums.'. When combined with -r and *, recursively auto-generates checksum files per directory. - -c, --check, -Check + -c, -Check Reads hashes from a specified checksum file and verifies target files on disk. If -r is specified, recursively looks for and validates checksum files by name across subdirectories. - -m, --match, -Match + -m, -Match Compares the computed hash of target file(s) directly against a given hash string. - -a, --algo, --algorithm, -Algorithm + -a, -algo, -Algorithm Specifies the hash algorithm to use. Supported options: SHA1, SHA256 (default), SHA384, SHA512, MD5. In -c or -m mode, auto-detects algorithm from hash length if omitted. - -r, --recursive, -Recursive + -r, -Recursive Processes target files or verification files recursively across directory structures. - -h, --help, -Help + -h, -Help Displays this help documentation. EXAMPLES @@ -89,17 +88,16 @@ EXAMPLES 2. Compute SHA512 checksums recursively and auto-save (prompts for filename, hit Enter for auto): .\checksum.ps1 * -r -o -a SHA512 - .\checksum.ps1 * --recursive --outfile "" --algo SHA512 + .\checksum.ps1 * -Recursive -OutFile "" -Algorithm SHA512 3. Compute SHA256 checksums and save to custom file: .\checksum.ps1 * -o my_hashes.txt 4. Verify checksums against a verification file: .\checksum.ps1 -c checksums.sha256 - .\checksum.ps1 --check checksums.sha256 5. Compare file directly against an expected hash string: - .\checksum.ps1 myfile.bin --match 406653a68bd3538975656831f33f031b + .\checksum.ps1 myfile.bin -m 406653a68bd3538975656831f33f031b '@ }; @@ -189,7 +187,7 @@ process { }; }; - # --- DIRECT MATCH MODE (-m / --match) --- + # --- DIRECT MATCH MODE (-m / -Match) --- if ($PSCmdlet.ParameterSetName -eq 'Match') { if ([string]::IsNullOrWhiteSpace($Algorithm)) { $Algorithm = Get-AlgorithmFromHashLength -HashString ($Match); @@ -213,7 +211,7 @@ process { }; }; } - # --- VERIFICATION MODE (-c / --check) --- + # --- VERIFICATION MODE (-c / -Check) --- elseif ($PSCmdlet.ParameterSetName -eq 'Verify') { $checkFiles = @(); if ($Recursive) { @@ -257,12 +255,10 @@ process { $expectedHash =$null; $targetFile =$null; - # Standard GNU checksum format: if ($line -match '^([a-fA-F0-9]+)\s+[* ]?(.*)$') { $expectedHash =$Matches[1].ToLower(); $targetFile =$Matches[2].Trim().TrimStart('*'); } - # Fallback for unspaced files: elseif ($line -match '^([a-fA-F0-9]{128}\vert{}[a-fA-F0-9]{96}\vert{}[a-fA-F0-9]{64}\vert{}[a-fA-F0-9]{40}\vert{}[a-fA-F0-9]{32})[* ]?(.*)$') { $expectedHash =$Matches[1].ToLower(); $targetFile =$Matches[2].Trim().TrimStart('*'); @@ -300,7 +296,6 @@ process { if ([string]::IsNullOrWhiteSpace($Algorithm)) {$Algorithm = 'SHA256'; }; - # Detect if the parameter was used AT ALL, even if they hit Enter at the prompt (resulting in empty string) $wantOutFile =$PSBoundParameters.ContainsKey('OutFile'); $isAutoFile = [string]::IsNullOrWhiteSpace($OutFile);