From e5435d124ea98ca9fd853e8d8bd4567c3f9f0f11 Mon Sep 17 00:00:00 2001 From: Princess Pi Date: Fri, 25 Sep 2026 15:50:28 -0600 Subject: [PATCH] waypoit debuguing --- Windows-Scripts/checksum.ps1 | 93 +++++++++++++----------------------- 1 file changed, 34 insertions(+), 59 deletions(-) diff --git a/Windows-Scripts/checksum.ps1 b/Windows-Scripts/checksum.ps1 index cc6a393..b1f1296 100644 --- a/Windows-Scripts/checksum.ps1 +++ b/Windows-Scripts/checksum.ps1 @@ -5,24 +5,19 @@ param( [Parameter(Position = 0, ParameterSetName = 'Match', ValueFromPipeline = $true)] [string[]]$FilePath, - # Output file switch (-o / --out) - auto generates file based on rules + # Output file (-o / --outfile / -OutFile) [Parameter(Mandatory = $false, ParameterSetName = 'Generate')] - [Alias('o', 'out', 'SaveToFile')] - [switch]$SaveToFile, - - # Custom Output file name (-f / --outfile) - [Parameter(Mandatory = $false, ParameterSetName = 'Generate')] - [Alias('f', 'outfile')] + [Alias('o')] [string]$OutFile, # File containing checksums to verify (-c / --check) [Parameter(Mandatory = $false, ParameterSetName = 'Verify')] - [Alias('c', 'check')] + [Alias('c')] [string]$Check, # Direct checksum hash string to match against (-m / --match) [Parameter(Mandatory = $true, ParameterSetName = 'Match')] - [Alias('m', 'match')] + [Alias('m')] [string]$Match, # Hashing algorithm to use (Defaults to SHA256) @@ -33,12 +28,12 @@ param( # Recursive flag (-r / --recursive) [Parameter(Mandatory = $false)] - [Alias('r', 'recursive')] + [Alias('r')] [switch]$Recursive, # Help flag (-h / --help) [Parameter(Mandatory = $false)] - [Alias('h', 'help')] + [Alias('h')] [switch]$Help ) @@ -49,7 +44,7 @@ NAME checksum - Compute and verify message digests / file checksums SYNOPSIS - .\checksum.ps1 [-FilePath] [-o] [-OutFile ] [-Algorithm ] [-r] + .\checksum.ps1 [-FilePath] [-o []] [-Algorithm ] [-r] .\checksum.ps1 [-FilePath] -Match [-Algorithm ] [-r] .\checksum.ps1 -Check [-Algorithm ] [-r] .\checksum.ps1 -Help @@ -57,22 +52,19 @@ 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 (--out, --check, --recursive) GNU-style flags. + 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, --out, -SaveToFile + -o, --outfile, -OutFile Enables auto-saving checksum results to file(s). - - If input contains a wildcard (*), writes to 'checksums.'. - - If an explicit file is given, writes to '.'. + - 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. - -f, --outfile, -OutFile - Specifies an explicit destination filename for saving checksum output. - -c, --check, -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. @@ -95,17 +87,17 @@ EXAMPLES 1. Compute SHA256 for a single file: .\checksum.ps1 myfile.iso - 2. Compute SHA512 checksums recursively and auto-save: + 2. Compute SHA512 checksums recursively and auto-save (prompts for filename, hit Enter for auto): .\checksum.ps1 * -r -o -a SHA512 - .\checksum.ps1 * --recursive --out --algo SHA512 + .\checksum.ps1 * --recursive --outfile "" --algo SHA512 - 3. Verify checksums against a verification file: + 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 - 4. Recursively verify checksum files across subdirectories: - .\checksum.ps1 --check checksums.sha256 --recursive - 5. Compare file directly against an expected hash string: .\checksum.ps1 myfile.bin --match 406653a68bd3538975656831f33f031b '@ @@ -308,7 +300,9 @@ process { if ([string]::IsNullOrWhiteSpace($Algorithm)) {$Algorithm = 'SHA256'; }; - $wantOutFile = $SaveToFile.IsPresent -or (-not [string]::IsNullOrWhiteSpace($OutFile)); + # 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); if ($wantOutFile) { if ($Recursive -and$isWildcardGlobal) { @@ -318,7 +312,7 @@ process { $dirPath =$group.Name; $filesInDir =$group.Group; - if ([string]::IsNullOrWhiteSpace($OutFile)) { + if ($isAutoFile) { $currentOutFile = Join-Path -Path ($dirPath) -ChildPath "checksums.$($Algorithm.ToLower())"; } else { $currentOutFile = Join-Path -Path ($dirPath) -ChildPath ($OutFile); @@ -337,45 +331,26 @@ process { }; } else { - $singleOutFile =$null; - - if ([string]::IsNullOrWhiteSpace($OutFile)) { - if ($isWildcardGlobal) {$singleOutFile = "checksums.$($Algorithm.ToLower())"; - } + if ($isAutoFile) {$singleOutFile = "checksums.$($Algorithm.ToLower())"; } else { $singleOutFile =$OutFile; }; - if ($singleOutFile) {$linesToWrite = @(); - foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower(); - - $relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue'; - if (-not $relativePath) { $relativePath =$item.Name; }; - if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) { - $relativePath =$relativePath.Substring(2); - }; - - $outputLine = "${hash}${relativePath}"; - $linesToWrite +=$outputLine; - Write-Output $outputLine; + $linesToWrite = @(); + foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower(); + + $relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue'; + if (-not $relativePath) { $relativePath =$item.Name; }; + if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) { + $relativePath =$relativePath.Substring(2); }; - Set-Content -Path ($singleOutFile) -Value$linesToWrite -Encoding utf8; - } else { - foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower(); - - $relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue'; - if (-not $relativePath) { $relativePath =$item.Name; }; - if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) { - $relativePath =$relativePath.Substring(2); - }; - - $outputLine = "${hash}${relativePath}"; - $currentOutFile = "$($item.FullName).$($Algorithm.ToLower())"; - Set-Content -Path ($currentOutFile) -Value$outputLine -Encoding utf8; - Write-Output $outputLine; - }; + $outputLine = "${hash}${relativePath}"; + $linesToWrite +=$outputLine; + Write-Output $outputLine; }; + + Set-Content -Path ($singleOutFile) -Value$linesToWrite -Encoding utf8; }; } else {