waypoit debuguing
This commit is contained in:
+103
-69
@@ -5,40 +5,40 @@ param(
|
|||||||
[Parameter(Position = 0, ParameterSetName = 'Match', ValueFromPipeline = $true)]
|
[Parameter(Position = 0, ParameterSetName = 'Match', ValueFromPipeline = $true)]
|
||||||
[string[]]$FilePath,
|
[string[]]$FilePath,
|
||||||
|
|
||||||
# Output file switch (-o) - auto generates file based on rules
|
# Output file switch (-o / --out) - auto generates file based on rules
|
||||||
[Parameter(Mandatory = $false, ParameterSetName = 'Generate')]
|
[Parameter(Mandatory = $false, ParameterSetName = 'Generate')]
|
||||||
[Alias('o', 'out')]
|
[Alias('o', 'out', 'SaveToFile')]
|
||||||
[switch]$SaveToFile,
|
[switch]$SaveToFile,
|
||||||
|
|
||||||
# Custom Output file name (-f / -OutFile)
|
# Custom Output file name (-f / --outfile)
|
||||||
[Parameter(Mandatory = $false, ParameterSetName = 'Generate')]
|
[Parameter(Mandatory = $false, ParameterSetName = 'Generate')]
|
||||||
[Alias('f')]
|
[Alias('f', 'outfile')]
|
||||||
[string]$OutFile,
|
[string]$OutFile,
|
||||||
|
|
||||||
# File containing checksums to verify (-c / -Check)
|
# File containing checksums to verify (-c / --check)
|
||||||
[Parameter(Mandatory = $false, ParameterSetName = 'Verify')]
|
[Parameter(Mandatory = $false, ParameterSetName = 'Verify')]
|
||||||
[Alias('c')]
|
[Alias('c', 'check')]
|
||||||
[string]$Check,
|
[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')]
|
[Parameter(Mandatory = $true, ParameterSetName = 'Match')]
|
||||||
[Alias('m')]
|
[Alias('m', 'match')]
|
||||||
[string]$Match,
|
[string]$Match,
|
||||||
|
|
||||||
# Hashing algorithm to use (Defaults to SHA256)
|
# Hashing algorithm to use (Defaults to SHA256)
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
[Alias('a', 'algo')]
|
[Alias('a', 'algo', 'algorithm')]
|
||||||
[ValidateSet('SHA1', 'SHA256', 'SHA384', 'SHA512', 'MD5')]
|
[ValidateSet('SHA1', 'SHA256', 'SHA384', 'SHA512', 'MD5')]
|
||||||
[string]$Algorithm,
|
[string]$Algorithm,
|
||||||
|
|
||||||
# Recursive flag (-r / -Recursive)
|
# Recursive flag (-r / --recursive)
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
[Alias('r')]
|
[Alias('r', 'recursive')]
|
||||||
[switch]$Recursive,
|
[switch]$Recursive,
|
||||||
|
|
||||||
# Help flag (-h / -Help)
|
# Help flag (-h / --help)
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
[Alias('h')]
|
[Alias('h', 'help')]
|
||||||
[switch]$Help
|
[switch]$Help
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -55,51 +55,59 @@ SYNOPSIS
|
|||||||
.\checksum.ps1 -Help
|
.\checksum.ps1 -Help
|
||||||
|
|
||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
Computes or verifies checksums using various cryptographic algorithms.
|
Computes or verifies checksums using cryptographic hash algorithms (SHA256, SHA512, SHA1, MD5, SHA384).
|
||||||
Outputs hash values in standard Linux utility format (<hash> <filename>).
|
Outputs hash values in standard GNU coreutils format (<hash> <filename>).
|
||||||
Ignores checksum files (.md5, .sha1, .sha256, .sha384, .sha512) unless explicitly passed.
|
Supports both single-dash (-o, -c, -r) and double-dash (--out, --check, --recursive) GNU-style flags.
|
||||||
|
Ignores checksum metadata files (.md5, .sha1, .sha256, .sha384, .sha512) during wildcards unless passed explicitly.
|
||||||
|
|
||||||
PARAMETERS
|
PARAMETERS
|
||||||
-FilePath <string[]>
|
-FilePath <string[]>
|
||||||
Specifies the path to one or more files to hash. Accepts wildcards (*).
|
Specifies the target file(s) or wildcard pattern (*).
|
||||||
|
|
||||||
-o, -Out, --Out <switch>
|
-o, --out, -SaveToFile <switch>
|
||||||
Enables writing checksum results to an auto-named file.
|
Enables auto-saving checksum results to file(s).
|
||||||
- If input has a wildcard (*), outputs to 'checksums.<algo>'
|
- If input contains a wildcard (*), writes to 'checksums.<algo>'.
|
||||||
- If no wildcard is used, outputs to '<filename>.<algo>' per file.
|
- If an explicit file is given, writes to '<filename>.<algo>'.
|
||||||
When combined with -r and *, it auto-generates checksum files
|
When combined with -r and *, recursively auto-generates checksum files per directory.
|
||||||
inside the current working directory and each subdirectory recursively.
|
|
||||||
|
|
||||||
-f, -OutFile, --OutFile <string>
|
-f, --outfile, -OutFile <string>
|
||||||
Specifies an explicit output file name to save results to.
|
Specifies an explicit destination filename for saving checksum output.
|
||||||
|
|
||||||
-r, -Recursive, --Recursive <switch>
|
-c, --check, -Check <string>
|
||||||
Recursively generate or verify checksums in subdirectories.
|
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.
|
||||||
|
|
||||||
-c, -Check, --Check <string>
|
-m, --match, -Match <string>
|
||||||
Read checksums from the specified file and verify them against files on disk.
|
Compares the computed hash of target file(s) directly against a given hash string.
|
||||||
If -r is used, searches for the specified filename recursively in subdirectories.
|
|
||||||
|
|
||||||
-m, -Match, --Match <string>
|
-a, --algo, --algorithm, -Algorithm <string>
|
||||||
Compare the computed hash of the target file(s) against a specific hash string.
|
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.
|
||||||
|
|
||||||
-a, -Algorithm, --Algorithm <string>
|
-r, --recursive, -Recursive <switch>
|
||||||
Specifies the cryptographic hash algorithm to use.
|
Processes target files or verification files recursively across directory structures.
|
||||||
Supported values: SHA1, SHA256 (default), SHA384, SHA512, MD5.
|
|
||||||
If omitted in -c or -m mode, the algorithm is auto-detected by hash length.
|
|
||||||
|
|
||||||
-h, -Help, --Help <switch>
|
-h, --help, -Help <switch>
|
||||||
Display this help message.
|
Displays this help documentation.
|
||||||
|
|
||||||
EXAMPLES
|
EXAMPLES
|
||||||
1. Compute SHA256 checksum for a single file:
|
1. Compute SHA256 for a single file:
|
||||||
.\checksum.ps1 myfile.iso
|
.\checksum.ps1 myfile.iso
|
||||||
|
|
||||||
2. Compute checksums for all files and auto-write to files (-o flag):
|
2. Compute SHA512 checksums recursively and auto-save:
|
||||||
.\checksum.ps1 * -r -o
|
.\checksum.ps1 * -r -o -a SHA512
|
||||||
|
.\checksum.ps1 * --recursive --out --algo SHA512
|
||||||
|
|
||||||
3. Verify checksums from verification files recursively:
|
3. Verify checksums against a verification file:
|
||||||
.\checksum.ps1 -c checksums.sha256 -r
|
.\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
|
||||||
'@
|
'@
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -124,7 +132,7 @@ process {
|
|||||||
$helpInPath =$false;
|
$helpInPath =$false;
|
||||||
if ($FilePath) {
|
if ($FilePath) {
|
||||||
foreach ($p in ($FilePath)) {
|
foreach ($p in ($FilePath)) {
|
||||||
if ($p -match '^(?i)[-/]{1,2}(h\vert{}help)$') {
|
if ($p -match '^(?i)[-/]{1,2}(h\vert{}help\vert{}\?)$') {
|
||||||
$helpInPath =$true;
|
$helpInPath =$true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -189,7 +197,7 @@ process {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
# --- DIRECT MATCH MODE (-m / -Match) ---
|
# --- DIRECT MATCH MODE (-m / --match) ---
|
||||||
if ($PSCmdlet.ParameterSetName -eq 'Match') {
|
if ($PSCmdlet.ParameterSetName -eq 'Match') {
|
||||||
if ([string]::IsNullOrWhiteSpace($Algorithm)) {
|
if ([string]::IsNullOrWhiteSpace($Algorithm)) {
|
||||||
$Algorithm = Get-AlgorithmFromHashLength -HashString ($Match);
|
$Algorithm = Get-AlgorithmFromHashLength -HashString ($Match);
|
||||||
@@ -202,7 +210,7 @@ process {
|
|||||||
$actualHash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
|
$actualHash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
|
||||||
$targetFile = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
|
$targetFile = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
|
||||||
if (-not $targetFile) { $targetFile = $item.Name; };
|
if (-not $targetFile) { $targetFile = $item.Name; };
|
||||||
if ($targetFile.StartsWith(".\")) { $targetFile = $targetFile.Substring(2); };
|
if ($targetFile.StartsWith(".\") -or $targetFile.StartsWith("./")) { $targetFile = $targetFile.Substring(2); };
|
||||||
|
|
||||||
if ($actualHash -eq $expectedHash) {
|
if ($actualHash -eq $expectedHash) {
|
||||||
Write-Host "${targetFile}: " -NoNewline;
|
Write-Host "${targetFile}: " -NoNewline;
|
||||||
@@ -213,7 +221,7 @@ process {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
# --- VERIFICATION MODE (-c / -Check) ---
|
# --- VERIFICATION MODE (-c / --check) ---
|
||||||
elseif ($PSCmdlet.ParameterSetName -eq 'Verify') {
|
elseif ($PSCmdlet.ParameterSetName -eq 'Verify') {
|
||||||
$checkFiles = @();
|
$checkFiles = @();
|
||||||
if ($Recursive) {
|
if ($Recursive) {
|
||||||
@@ -238,10 +246,15 @@ process {
|
|||||||
if ([string]::IsNullOrWhiteSpace($fileAlgo)) {
|
if ([string]::IsNullOrWhiteSpace($fileAlgo)) {
|
||||||
foreach ($line in ($lines)) {
|
foreach ($line in ($lines)) {
|
||||||
if ([string]::IsNullOrWhiteSpace($line) -or$line.StartsWith("#")) { continue; };
|
if ([string]::IsNullOrWhiteSpace($line) -or$line.StartsWith("#")) { continue; };
|
||||||
|
|
||||||
if ($line -match '^([a-fA-F0-9]+)\s+') {
|
if ($line -match '^([a-fA-F0-9]+)\s+') {
|
||||||
$fileAlgo = Get-AlgorithmFromHashLength -HashString ($Matches[1]);
|
$fileAlgo = Get-AlgorithmFromHashLength -HashString ($Matches[1]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
elseif ($line -match '^([a-fA-F0-9]{128}|[a-fA-F0-9]{96}|[a-fA-F0-9]{64}|[a-fA-F0-9]{40}|[a-fA-F0-9]{32})') {
|
||||||
|
$fileAlgo = Get-AlgorithmFromHashLength -HashString ($Matches[1]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
if ([string]::IsNullOrWhiteSpace($fileAlgo)) {$fileAlgo = 'SHA256'; };
|
if ([string]::IsNullOrWhiteSpace($fileAlgo)) {$fileAlgo = 'SHA256'; };
|
||||||
};
|
};
|
||||||
@@ -249,13 +262,27 @@ process {
|
|||||||
foreach ($line in ($lines)) {
|
foreach ($line in ($lines)) {
|
||||||
if ([string]::IsNullOrWhiteSpace($line) -or$line.StartsWith("#")) { continue; };
|
if ([string]::IsNullOrWhiteSpace($line) -or$line.StartsWith("#")) { continue; };
|
||||||
|
|
||||||
if ($line -match '^([a-fA-F0-9]+)\s+[* ]?(.*)$') {$expectedHash = $Matches[1].ToLower();$targetFile = $Matches[2].Trim().TrimStart('*');$targetFullPath = Join-Path -Path ($baseDir) -ChildPath ($targetFile);
|
$expectedHash =$null;
|
||||||
|
$targetFile =$null;
|
||||||
|
|
||||||
|
# Standard GNU checksum format: <hash> <space/asterisk> <filename>
|
||||||
|
if ($line -match '^([a-fA-F0-9]+)\s+[* ]?(.*)$') {
|
||||||
|
$expectedHash =$Matches[1].ToLower();
|
||||||
|
$targetFile =$Matches[2].Trim().TrimStart('*');
|
||||||
|
}
|
||||||
|
# Fallback for unspaced files: <hash><filename>
|
||||||
|
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('*');
|
||||||
|
};
|
||||||
|
|
||||||
|
if ($expectedHash -and $targetFile) {$targetFullPath = Join-Path -Path ($baseDir) -ChildPath ($targetFile);
|
||||||
|
|
||||||
if (Test-Path -Path ($targetFullPath) -PathType 'Leaf') {$actualHash = (Get-FileHash -Path ($targetFullPath) -Algorithm ($fileAlgo)).Hash.ToLower();
|
if (Test-Path -Path ($targetFullPath) -PathType 'Leaf') {$actualHash = (Get-FileHash -Path ($targetFullPath) -Algorithm ($fileAlgo)).Hash.ToLower();
|
||||||
|
|
||||||
$displayPath = Resolve-Path -Path ($targetFullPath) -Relative -ErrorAction 'SilentlyContinue';
|
$displayPath = Resolve-Path -Path ($targetFullPath) -Relative -ErrorAction 'SilentlyContinue';
|
||||||
if (-not $displayPath) { $displayPath =$targetFile; };
|
if (-not $displayPath) { $displayPath =$targetFile; };
|
||||||
if ($displayPath.StartsWith(".\")) { $displayPath =$displayPath.Substring(2); };
|
if ($displayPath.StartsWith(".\") -or $displayPath.StartsWith("./")) { $displayPath =$displayPath.Substring(2); };
|
||||||
|
|
||||||
if ($actualHash -eq$expectedHash) {
|
if ($actualHash -eq$expectedHash) {
|
||||||
Write-Host "${displayPath}: " -NoNewline;
|
Write-Host "${displayPath}: " -NoNewline;
|
||||||
@@ -267,7 +294,7 @@ process {
|
|||||||
} else {
|
} else {
|
||||||
$displayPath =$targetFile;
|
$displayPath =$targetFile;
|
||||||
if ($baseDir -ne (Get-Location).Path) {$displayPath = Join-Path -Path (Resolve-Path -Path ($baseDir) -Relative -ErrorAction 'SilentlyContinue') -ChildPath ($targetFile);
|
if ($baseDir -ne (Get-Location).Path) {$displayPath = Join-Path -Path (Resolve-Path -Path ($baseDir) -Relative -ErrorAction 'SilentlyContinue') -ChildPath ($targetFile);
|
||||||
if ($displayPath.StartsWith(".\")) { $displayPath =$displayPath.Substring(2); };
|
if ($displayPath.StartsWith(".\") -or $displayPath.StartsWith("./")) { $displayPath =$displayPath.Substring(2); };
|
||||||
};
|
};
|
||||||
Write-Host "${displayPath}: " -NoNewline;
|
Write-Host "${displayPath}: " -NoNewline;
|
||||||
Write-Host "FAILED open or read" -ForegroundColor Red;
|
Write-Host "FAILED open or read" -ForegroundColor Red;
|
||||||
@@ -297,15 +324,16 @@ process {
|
|||||||
$currentOutFile = Join-Path -Path ($dirPath) -ChildPath ($OutFile);
|
$currentOutFile = Join-Path -Path ($dirPath) -ChildPath ($OutFile);
|
||||||
};
|
};
|
||||||
|
|
||||||
$null = New-Item -Path ($currentOutFile) -ItemType 'File' -Force;
|
$linesToWrite = @();
|
||||||
|
|
||||||
foreach ($item in ($filesInDir)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
|
foreach ($item in ($filesInDir)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
|
||||||
$relativePath =$item.Name;
|
$relativePath =$item.Name;
|
||||||
|
|
||||||
$outputLine = "$hash$relativePath";
|
$outputLine = "${hash}${relativePath}";
|
||||||
Add-Content -Path ($currentOutFile) -Value ($outputLine);
|
$linesToWrite +=$outputLine;
|
||||||
Write-Output $outputLine;
|
Write-Output $outputLine;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Set-Content -Path ($currentOutFile) -Value$linesToWrite -Encoding utf8;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -318,42 +346,48 @@ process {
|
|||||||
$singleOutFile =$OutFile;
|
$singleOutFile =$OutFile;
|
||||||
};
|
};
|
||||||
|
|
||||||
if ($singleOutFile) {
|
if ($singleOutFile) {$linesToWrite = @();
|
||||||
$null = New-Item -Path ($singleOutFile) -ItemType 'File' -Force;
|
|
||||||
};
|
|
||||||
|
|
||||||
foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
|
foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
|
||||||
|
|
||||||
$relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
|
$relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
|
||||||
if (-not $relativePath) { $relativePath =$item.Name; };
|
if (-not $relativePath) { $relativePath =$item.Name; };
|
||||||
if ($relativePath.StartsWith(".\")) {
|
if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) {
|
||||||
$relativePath =$relativePath.Substring(2);
|
$relativePath =$relativePath.Substring(2);
|
||||||
};
|
};
|
||||||
|
|
||||||
$outputLine = "$hash$relativePath";
|
$outputLine = "${hash}${relativePath}";
|
||||||
|
$linesToWrite +=$outputLine;
|
||||||
if ($singleOutFile) {
|
Write-Output $outputLine;
|
||||||
Add-Content -Path ($singleOutFile) -Value ($outputLine);
|
|
||||||
} else {
|
|
||||||
$currentOutFile = "$($item.FullName).$($Algorithm.ToLower())";
|
|
||||||
$null = New-Item -Path ($currentOutFile) -ItemType 'File' -Force;
|
|
||||||
Add-Content -Path ($currentOutFile) -Value ($outputLine);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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;
|
Write-Output $outputLine;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
};
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
|
foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
|
||||||
|
|
||||||
$relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
|
$relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
|
||||||
if (-not $relativePath) { $relativePath =$item.Name; };
|
if (-not $relativePath) { $relativePath =$item.Name; };
|
||||||
if ($relativePath.StartsWith(".\")) {
|
if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) {
|
||||||
$relativePath =$relativePath.Substring(2);
|
$relativePath =$relativePath.Substring(2);
|
||||||
};
|
};
|
||||||
|
|
||||||
$outputLine = "$hash$relativePath";
|
$outputLine = "${hash}${relativePath}";
|
||||||
Write-Output $outputLine;
|
Write-Output $outputLine;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user