Compare commits
15 Commits
dc17c77c87
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 586e8746be | |||
| 9bfedfafc5 | |||
| 4e798818d1 | |||
| 56b08f32da | |||
| 251c9aa06c | |||
| 1dbd1ad79b | |||
| 7bd65e7d76 | |||
| 2fd8238a98 | |||
| 503e947eaf | |||
| 07a29167dd | |||
| 608e225833 | |||
| 4fea9dbccc | |||
| 4377cc6a45 | |||
| e5435d124e | |||
| 757f1775ff |
@@ -0,0 +1,9 @@
|
|||||||
|
1. recursive verify error: `FAILED open or read` RIGHT NOW GENERATES RELATIVE PATH/FILENAME IN CHECKSUMS FILES IN SUBDIRS, SHOULD JUST BE FOR THE FILES IN DAT DIR (for linux compatability)
|
||||||
|
2. recursive progress set to count total number of files recursively first for progress bar.
|
||||||
|
3. paralell processing
|
||||||
|
4. Princess Pi ascii art magenta
|
||||||
|
5. princess pi bubble text ascii art blue
|
||||||
|
6. script for full top-to-bottom testing
|
||||||
|
7. BASH linux version
|
||||||
|
8. more hashing algos?
|
||||||
|
9. multi-algo paralell workin? like multiple -a params?
|
||||||
+570
-277
@@ -1,20 +1,32 @@
|
|||||||
|
<#
|
||||||
|
.SYNOPSIS
|
||||||
|
Princess Pi's Magical CHECKSUM! - Computes and verifies message digests / file checksums.
|
||||||
|
.DESCRIPTION
|
||||||
|
Computes or verifies checksums using cryptographic hash algorithms (SHA256, SHA512, SHA1, MD5, SHA384).
|
||||||
|
Supports parallel processing, recursive scanning, direct hash matching, and verification file parsing.
|
||||||
|
.NOTES
|
||||||
|
Requires PowerShell 7.0 or higher.
|
||||||
|
#>
|
||||||
|
|
||||||
[CmdletBinding(DefaultParameterSetName='Generate')]
|
[CmdletBinding(DefaultParameterSetName='Generate')]
|
||||||
param(
|
param(
|
||||||
# File(s) or wildcard pattern to compute checksum for
|
# File(s) or wildcard pattern to compute checksum for
|
||||||
[Parameter(Position = 0, ParameterSetName = 'Generate', ValueFromPipeline = $true)]
|
[Parameter(Position = 0, ParameterSetName = 'Generate', ValueFromPipeline = $true)]
|
||||||
[Parameter(Position = 0, ParameterSetName = 'Match', ValueFromPipeline = $true)]
|
[Parameter(Position = 0, ParameterSetName = 'Match', ValueFromPipeline = $true)]
|
||||||
|
[Parameter(Position = 0, ParameterSetName = 'PrincessPi', ValueFromPipeline = $false)]
|
||||||
|
[Alias('i', 'input', 'if', 'InputFile')]
|
||||||
[string[]]$FilePath,
|
[string[]]$FilePath,
|
||||||
|
|
||||||
# Output file switch (-o) - auto generates file based on rules
|
# Custom output file (-f / -OutFile / -of)
|
||||||
[Parameter(Mandatory = $false, ParameterSetName = 'Generate')]
|
[Parameter(Mandatory = $false, ParameterSetName = 'Generate')]
|
||||||
[Alias('o', 'out')]
|
[Alias('f', 'of')]
|
||||||
[switch]$SaveToFile,
|
|
||||||
|
|
||||||
# Custom Output file name (-f / -OutFile)
|
|
||||||
[Parameter(Mandatory = $false, ParameterSetName = 'Generate')]
|
|
||||||
[Alias('f')]
|
|
||||||
[string]$OutFile,
|
[string]$OutFile,
|
||||||
|
|
||||||
|
# Auto output file switch (-s / -Save / -o)
|
||||||
|
[Parameter(Mandatory = $false, ParameterSetName = 'Generate')]
|
||||||
|
[Alias('s', 'o')]
|
||||||
|
[switch]$Save,
|
||||||
|
|
||||||
# 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')]
|
||||||
@@ -36,328 +48,609 @@ param(
|
|||||||
[Alias('r')]
|
[Alias('r')]
|
||||||
[switch]$Recursive,
|
[switch]$Recursive,
|
||||||
|
|
||||||
|
# Princess Pi art flag (-pp / -PrincessPi)
|
||||||
|
[Parameter(Mandatory = $true, ParameterSetName = 'PrincessPi')]
|
||||||
|
[Alias('pp')]
|
||||||
|
[switch]$PrincessPi,
|
||||||
|
|
||||||
# Help flag (-h / -Help)
|
# Help flag (-h / -Help)
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
[Alias('h')]
|
[Alias('h')]
|
||||||
[switch]$Help
|
[switch]$Help
|
||||||
)
|
)
|
||||||
|
|
||||||
begin {
|
# --- ENVIRONMENT SANITY CHECK ---
|
||||||
function Show-Help {
|
if ($PSVersionTable.PSVersion.Major -lt 7) {
|
||||||
@'
|
Write-Error "checksum: This script requires PowerShell 7 or higher for parallel execution (-Parallel support). Current version is $($PSVersionTable.PSVersion)."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- HELPER FUNCTIONS ---
|
||||||
|
function Show-Help {
|
||||||
|
@'
|
||||||
|
|
||||||
|
Princess Pi's Magical CHECKSUM!
|
||||||
|
|
||||||
NAME
|
NAME
|
||||||
checksum - Compute and verify message digests / file checksums
|
checksum - Compute and verify message digests / file checksums
|
||||||
|
|
||||||
SYNOPSIS
|
SYNOPSIS
|
||||||
.\checksum.ps1 [-FilePath] <string[]> [-o] [-OutFile <string>] [-Algorithm <string>] [-r]
|
.\checksum.ps1 [-FilePath] <string[]> [-s] [-f <string>] [-Algorithm <string>] [-r]
|
||||||
.\checksum.ps1 [-FilePath] <string[]> -Match <string> [-Algorithm <string>] [-r]
|
.\checksum.ps1 [-FilePath] <string[]> -Match <string> [-Algorithm <string>] [-r]
|
||||||
.\checksum.ps1 -Check <string> [-Algorithm <string>] [-r]
|
.\checksum.ps1 -Check <string> [-Algorithm <string>] [-r]
|
||||||
|
.\checksum.ps1 -PrincessPi
|
||||||
.\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.
|
Ignores checksum metadata files (.md5, .sha1, .sha256, .sha384, .sha512) during wildcards unless passed explicitly.
|
||||||
|
|
||||||
PARAMETERS
|
PARAMETERS
|
||||||
-FilePath <string[]>
|
-FilePath, -i, -input, -if, -InputFile <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>
|
-s, -S, -Save, -o, -O <switch>
|
||||||
Enables writing checksum results to an auto-named file.
|
Auto-saves checksum results. (Takes no arguments).
|
||||||
- If input has a wildcard (*), outputs to 'checksums.<algo>'
|
- If input filename contains a wildcard (*), saves to 'checksums.<algo>'.
|
||||||
- If no wildcard is used, outputs to '<filename>.<algo>' per file.
|
- If input filename does NOT contain a wildcard, saves to '<filename>.<algo>'.
|
||||||
When combined with -r and *, it auto-generates checksum files
|
When combined with -r and *, recursively auto-generates checksums per directory.
|
||||||
inside the current working directory and each subdirectory recursively.
|
|
||||||
|
|
||||||
-f, -OutFile, --OutFile <string>
|
-f, -F, -of, -OF, -OutFile <string>
|
||||||
Specifies an explicit output file name to save results to.
|
Saves checksum results to a specific custom filename.
|
||||||
|
Overrides the auto-naming behavior of -s if both are passed.
|
||||||
|
|
||||||
-r, -Recursive, --Recursive <switch>
|
-c, -C, -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, -M, -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, -A, -Algo, -Algorithm <string>
|
||||||
Compare the computed hash of the target file(s) against a specific hash string.
|
Specifies the hash algorithm to use.
|
||||||
|
Supported options: MD5, SHA1, SHA256 (default), SHA384, SHA512.
|
||||||
|
In -c or -m mode, auto-detects algorithm from hash length if omitted.
|
||||||
|
|
||||||
-a, -Algorithm, --Algorithm <string>
|
-r, -R, -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>
|
-pp, -PP, -PrincessPi <switch>
|
||||||
Display this help message.
|
Displays Princess Pi!
|
||||||
|
|
||||||
|
-h, -H, -Help <switch>
|
||||||
|
Displays this help documentation.
|
||||||
|
Also displays on any error.
|
||||||
|
|
||||||
EXAMPLES
|
EXAMPLES
|
||||||
1. Compute SHA256 checksum for a single file:
|
1. Compute SHA256 for a single file and auto-save as 'myfile.iso.sha256':
|
||||||
.\checksum.ps1 myfile.iso
|
.\checksum.ps1 myfile.iso -s
|
||||||
|
|
||||||
2. Compute checksums for all files and auto-write to files (-o flag):
|
2. Compute SHA512 recursively and auto-save as 'checksums.sha512' in each folder:
|
||||||
.\checksum.ps1 * -r -o
|
.\checksum.ps1 * -r -s -a SHA512
|
||||||
|
|
||||||
|
3. Compute SHA256 checksums and save to a specific custom file:
|
||||||
|
.\checksum.ps1 * -f my_hashes.txt
|
||||||
|
|
||||||
|
4. Verify checksums against a verification file:
|
||||||
|
.\checksum.ps1 -c checksums.sha256
|
||||||
|
|
||||||
|
5. Compare file directly against an expected hash string:
|
||||||
|
.\checksum.ps1 myfile.bin -m 406653a68bd3538975656831f33f031b
|
||||||
|
|
||||||
|
Princess Pi LOVES YOU!~
|
||||||
|
https://linktr.ee/princesspi3
|
||||||
|
|
||||||
3. Verify checksums from verification files recursively:
|
|
||||||
.\checksum.ps1 -c checksums.sha256 -r
|
|
||||||
'@
|
'@
|
||||||
};
|
|
||||||
|
|
||||||
function Get-AlgorithmFromHashLength {
|
|
||||||
param([string]$HashString);
|
|
||||||
|
|
||||||
switch ($HashString.Trim().Length) {
|
|
||||||
32 { return 'MD5'; }
|
|
||||||
40 { return 'SHA1'; }
|
|
||||||
64 { return 'SHA256'; }
|
|
||||||
96 { return 'SHA384'; }
|
|
||||||
128 { return 'SHA512'; }
|
|
||||||
default {
|
|
||||||
Write-Error "checksum: Could not auto-detect algorithm for hash length $($HashString.Length). Please specify -Algorithm.";
|
|
||||||
return $null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
process {
|
function Write-ErrorWithHelp {
|
||||||
$helpInPath =$false;
|
param([string]$ErrorMessage);
|
||||||
if ($FilePath) {
|
Show-Help;
|
||||||
foreach ($p in ($FilePath)) {
|
Write-Host "";
|
||||||
if ($p -match '^(?i)[-/]{1,2}(h\vert{}help)$') {
|
Write-Error $ErrorMessage;
|
||||||
$helpInPath =$true;
|
}
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
if ($Help.IsPresent -or$helpInPath) {
|
function Show-PrincessPiArt {
|
||||||
Show-Help;
|
$art = @'
|
||||||
|
|
||||||
|
=#= -- ##*+++++ *+
|
||||||
|
-#=#- +# -*%%= ##++++++++++# ##*********#
|
||||||
|
#==#= *** %%++%% -++# #++++++++++++*+ *#***************#
|
||||||
|
#===#= %***+%++#*%%@+++++% #++++++++++++**+++#******++********#
|
||||||
|
====+#%@*******###%=%++##++% #+++++++++++*++++++++#***====+****++#
|
||||||
|
=#=*==**#*#########%++#*+++@ *++++++++#+++++++++++#****==+******#
|
||||||
|
*#=====%####***%%++++#*+++**% ++++++++*+++++++++++++#************#*******
|
||||||
|
@****#==%####**#*+++++++#+++#**# #+++++++++++++++++++#####+*****************#
|
||||||
|
@****#######%**@+*%**#%+++++%##*** #+++++++++++++++++#+++++#****+======*****+
|
||||||
|
#****##%####@**%+*******@@+++@###**% #+++++++++++++*++++++++++****========****#
|
||||||
|
****#####%%*%*%+***@@@%%@*+++@####** *+++++++++++++++++++++#****=======****#
|
||||||
|
%***####%%**+%%*++*@=##@@%@++++%###*** ++++*+++++++++++#+**************#
|
||||||
|
@%%%@@##%%#++++++++==**@@**+++%*###**% +++++++++++#*+++++#*********####
|
||||||
|
%###%++++++++====**#++++%**##**% @% ++++#+++++++++++++**************#
|
||||||
|
%###%++++++++++++++++++%**%##**%@###@ %++++++++++++++++***=====********
|
||||||
|
###%%++++++++%++++++*%%%%###***##%##@ +++++++++++++##**************#
|
||||||
|
###%%%%+++++##+++++++#*+%###***%%%##@ ++++++++++++++%********+############%@
|
||||||
|
@%%*@*%%%%###****+*%%****++%##***%##@@@ #+++++++++++++++*********###*****#########*
|
||||||
|
#%%###***#####%**++++##**%#@@#@ ##+++++++++*###%#+***+***##*************#######
|
||||||
|
@#%%####%%####%+++++++##*######%++++++++++++++#*+++++#%###%#######*********######%
|
||||||
|
##%########@%@+++@###%*%#####@+++++++++++%**++++++++++%%%%%%########***==**###%##%
|
||||||
|
%#%###%###########@@%#%#@@##++++++++****++++********+++ %########**===*###%*#
|
||||||
|
#**##%####@@####%@######%*++++++++++++++++#++**+*#++++# ########******###% %
|
||||||
|
@#***# ###%@@##%#######@+++++++++++++++++++++%*++**++++# *########******##% @
|
||||||
|
*******% %##% =@#########++++++++++++++++++#++++*%++*#%#++# ########******##%
|
||||||
|
#@%@# %% *@#@@###*+++++++++++++++++++%+++##+++###++* ##*#####******##@
|
||||||
|
**##%*++%++++++++++++++++++++++++++++++++% +#**#####******###
|
||||||
|
##%#######*@***+#+++++++++++++++#***#++++++++++++ %**#####*******#%
|
||||||
|
*+++++$****##****++++++++##% #******#+++++++++++ %**#####*******#@
|
||||||
|
#++++++++*******- ++++++++# *******#++++++++++# @**#####*******#
|
||||||
|
#@@+++++++******- *+++++++# #******#+++++++++* **####%**==+**@
|
||||||
|
@@@@@+++ %+++++++# ******# %+++++++ %*###%***===**@ %=
|
||||||
|
@@@@@@@@@ #+++++++# =****** #++++++% *###%#***=+**% %**
|
||||||
|
@@@@@@@@@@ ++++++++# ******# #++++++# *##@%********% @*@
|
||||||
|
@@+@@@++@@+ ++++++++# ******# #++++++# @## #*********@ #***#
|
||||||
|
@@+@@++@@@++ @@@++++++* @@@%**** #@@++++% +## %#****************%
|
||||||
|
@*@**@+++# @@@@+++++ @@@@@@**# @@@@@++++# @# %#****************
|
||||||
|
+#+++++# @@@@@@+++% @@@@@@@@# @@@@@@@++ @ %##************%
|
||||||
|
#+++** @@@@@@@@@@ @@@@@@@@@ @@@@@@@@@ %###********%*
|
||||||
|
@@@@@@@@@@+ @@@@@@@@@@ =@@@@@@@@@ %%#####%%
|
||||||
|
*@@@@@@@@@@ @+*@@@@@@@= @@++@@@@@@
|
||||||
|
@++@@@@@@@@ #**@**@@@@@ *@@*#@@@@@@ -=
|
||||||
|
@%#@@@@@@@@ *#***@@@@@@ @+#+@++#@@@
|
||||||
|
#+++@++@@@@@ **********# #+++++@+@@@@
|
||||||
|
##+#+++@@@@@@ ##****#### *+#+##+++@@%%
|
||||||
|
#+++++++++++# #++++++++++#
|
||||||
|
#########*
|
||||||
|
|
||||||
|
____ ____ ____ _ _ ___ ____ ___ ___ ____ ____
|
||||||
|
( _ \( _ \(_ _)( \( )/ __)( ___)/ __)/ __) ( _ \(_ _)/\/
|
||||||
|
)___/ ) / _)(_ ) (( (__ )__) \__ \\__ \ )___/ _)(_
|
||||||
|
(__) (_)\_)(____)(_)\_)\___)(____)(___/(___/ (__) (____)
|
||||||
|
|
||||||
|
Princess Pi
|
||||||
|
https://linktr.ee/princesspi3
|
||||||
|
|
||||||
|
'@
|
||||||
|
Write-Host $art -ForegroundColor Blue;
|
||||||
|
}
|
||||||
|
|
||||||
|
function Get-AlgorithmFromHashLength {
|
||||||
|
param([string]$HashString);
|
||||||
|
|
||||||
|
switch ($HashString.Trim().Length) {
|
||||||
|
32 { return 'MD5'; }
|
||||||
|
40 { return 'SHA1'; }
|
||||||
|
64 { return 'SHA256'; }
|
||||||
|
96 { return 'SHA384'; }
|
||||||
|
128 { return 'SHA512'; }
|
||||||
|
default {
|
||||||
|
Write-ErrorWithHelp "checksum: Could not auto-detect algorithm for hash length $($HashString.Length). Please specify -Algorithm.";
|
||||||
|
return $null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- MAIN EXECUTION FLOW ---
|
||||||
|
$helpInPath = $false;
|
||||||
|
if ($FilePath) {
|
||||||
|
foreach ($p in ($FilePath)) {
|
||||||
|
if ($p -match '^(?i)[-/]{1,2}(h|help|\?)$') {
|
||||||
|
$helpInPath = $true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($Help.IsPresent -or $helpInPath) {
|
||||||
|
Show-Help;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($PrincessPi.IsPresent) {
|
||||||
|
Show-PrincessPiArt;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-not [string]::IsNullOrWhiteSpace($Algorithm)) {
|
||||||
|
$Algorithm = $Algorithm.ToUpper();
|
||||||
|
}
|
||||||
|
|
||||||
|
$isWildcardGlobal = $false;
|
||||||
|
if ($FilePath) {
|
||||||
|
foreach ($p in $FilePath) {
|
||||||
|
if ($p -match '\*') { $isWildcardGlobal = $true; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$showProgress = $Recursive.IsPresent -or $isWildcardGlobal -or ($PSCmdlet.ParameterSetName -eq 'Verify');
|
||||||
|
|
||||||
|
# --- ITEM GATHERING (Generate & Match Modes) ---
|
||||||
|
if ($PSCmdlet.ParameterSetName -in @('Generate', 'Match')) {
|
||||||
|
if (-not $FilePath -or $FilePath.Count -eq 0) {
|
||||||
|
Write-ErrorWithHelp "checksum: Missing file path parameter. Use -Help for usage.";
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
|
|
||||||
if (-not [string]::IsNullOrWhiteSpace($Algorithm)) {
|
$allItems = @();
|
||||||
$Algorithm =$Algorithm.ToUpper();
|
$skipExtensions = @('.md5', '.sha1', '.sha256', '.sha384', '.sha512');
|
||||||
};
|
|
||||||
|
|
||||||
# --- ITEM GATHERING (Generate & Match Modes) ---
|
foreach ($pathEntry in ($FilePath)) {
|
||||||
if ($PSCmdlet.ParameterSetName -in @('Generate', 'Match')) {
|
$isWildcard = ($pathEntry -match '\*');
|
||||||
if (-not $FilePath -or$FilePath.Count -eq 0) {
|
$resolvedItems = @();
|
||||||
Write-Error "checksum: Missing file path parameter. Use -Help for usage.";
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
$allItems = @();
|
if ($Recursive) {
|
||||||
$isWildcardGlobal =$false;
|
$resolvedItems = Get-ChildItem -Path ($pathEntry) -Recurse -File -ErrorAction 'SilentlyContinue';
|
||||||
$skipExtensions = @('.md5', '.sha1', '.sha256', '.sha384', '.sha512');
|
} else {
|
||||||
|
if ($isWildcard -or ($pathEntry -match '\?')) {
|
||||||
foreach ($pathEntry in ($FilePath)) {
|
$resolvedItems = Get-ChildItem -Path ($pathEntry) -File -ErrorAction 'SilentlyContinue';
|
||||||
$isWildcard = ($pathEntry -match '\*');
|
|
||||||
if ($isWildcard) { $isWildcardGlobal =$true; };
|
|
||||||
|
|
||||||
$resolvedItems = @();
|
|
||||||
if ($Recursive) {
|
|
||||||
$resolvedItems = Get-ChildItem -Path ($pathEntry) -Recurse -File -ErrorAction 'SilentlyContinue';
|
|
||||||
} else {
|
} else {
|
||||||
if ($isWildcard -or ($pathEntry -match '\?')) {
|
$resolvedItems = Get-Item -Path ($pathEntry) -ErrorAction 'SilentlyContinue';
|
||||||
$resolvedItems = Get-ChildItem -Path ($pathEntry) -File -ErrorAction 'SilentlyContinue';
|
}
|
||||||
} else {
|
}
|
||||||
$resolvedItems = Get-Item -Path ($pathEntry) -ErrorAction 'SilentlyContinue';
|
|
||||||
|
if (-not $resolvedItems) {
|
||||||
|
Write-ErrorWithHelp "checksum: $($pathEntry): No such file or directory";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$explicitFileFound = $false;
|
||||||
|
if ((-not $isWildcard) -and (Test-Path -Path ($pathEntry) -PathType 'Leaf')) {
|
||||||
|
$explicitFileFound = $true;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($item in ($resolvedItems)) {
|
||||||
|
if ($item.PSIsContainer) { continue; }
|
||||||
|
|
||||||
|
if (-not $explicitFileFound) {
|
||||||
|
if ($skipExtensions -contains $item.Extension.ToLower()) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
$allItems += $item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (-not $resolvedItems) {
|
# --- DIRECT MATCH MODE (-m / -Match) ---
|
||||||
Write-Error "checksum: $($pathEntry)`: No such file or directory";
|
if ($PSCmdlet.ParameterSetName -eq 'Match') {
|
||||||
continue;
|
if ([string]::IsNullOrWhiteSpace($Algorithm)) {
|
||||||
};
|
$Algorithm = Get-AlgorithmFromHashLength -HashString ($Match);
|
||||||
|
if (-not $Algorithm) { return; }
|
||||||
|
}
|
||||||
|
|
||||||
$explicitFileFound = $false;
|
$expectedHash = $Match.Trim().ToLower();
|
||||||
if ((-not $isWildcard) -and (Test-Path -Path ($pathEntry) -PathType 'Leaf')) {
|
$totalFiles = $allItems.Count;
|
||||||
$explicitFileFound = $true;
|
$currentIndex = 0;
|
||||||
};
|
|
||||||
|
|
||||||
foreach ($item in ($resolvedItems)) {
|
$allItems | ForEach-Object -Parallel {
|
||||||
if ($item.PSIsContainer) { continue; };
|
$item = $_
|
||||||
|
$algo = $using:Algorithm
|
||||||
|
$exp = $using:expectedHash
|
||||||
|
|
||||||
if (-not $explicitFileFound) {
|
$actualHash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).Hash.ToLower();
|
||||||
if ($skipExtensions -contains $item.Extension.ToLower()) {
|
$targetFile = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
|
||||||
continue;
|
if (-not $targetFile) { $targetFile = $item.Name; }
|
||||||
}
|
if ($targetFile.StartsWith(".\") -or $targetFile.StartsWith("./")) { $targetFile = $targetFile.Substring(2); }
|
||||||
|
|
||||||
|
[PSCustomObject]@{
|
||||||
|
TargetFile = $targetFile
|
||||||
|
IsMatch = ($actualHash -eq $exp)
|
||||||
|
}
|
||||||
|
} -ThrottleLimit 16 | ForEach-Object {
|
||||||
|
$currentIndex++
|
||||||
|
if ($showProgress -and $totalFiles -gt 0) {
|
||||||
|
Write-Progress -Activity "Matching Checksums ($Algorithm)" -Status "Processing file $currentIndex of $totalFiles" -PercentComplete (($currentIndex / $totalFiles) * 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($_.IsMatch) {
|
||||||
|
Write-Host "$($_.TargetFile): " -NoNewline;
|
||||||
|
Write-Host "OK" -ForegroundColor Green;
|
||||||
|
} else {
|
||||||
|
Write-Host "$($_.TargetFile): " -NoNewline;
|
||||||
|
Write-Host "FAILED" -ForegroundColor Red;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($showProgress) {
|
||||||
|
Write-Progress -Activity "Matching Checksums ($Algorithm)" -Completed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
# --- VERIFICATION MODE (-c / -Check) ---
|
||||||
|
elseif ($PSCmdlet.ParameterSetName -eq 'Verify') {
|
||||||
|
$checkFiles = @();
|
||||||
|
if ($Recursive) {
|
||||||
|
$checkFileName = Split-Path -Path ($Check) -Leaf;
|
||||||
|
$checkFiles = Get-ChildItem -Path '.\' -Filter ($checkFileName) -Recurse -File -ErrorAction 'SilentlyContinue';
|
||||||
|
} else {
|
||||||
|
if (Test-Path -Path ($Check) -PathType 'Leaf') {
|
||||||
|
$checkFiles = @(Get-Item -Path ($Check));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-not $checkFiles -or $checkFiles.Count -eq 0) {
|
||||||
|
Write-ErrorWithHelp "checksum: $($Check): No such file or directory";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($cf in ($checkFiles)) {
|
||||||
|
$lines = Get-Content -Path ($cf.FullName);
|
||||||
|
$baseDir = $cf.DirectoryName;
|
||||||
|
|
||||||
|
$fileAlgo = $Algorithm;
|
||||||
|
if ([string]::IsNullOrWhiteSpace($fileAlgo)) {
|
||||||
|
foreach ($line in ($lines)) {
|
||||||
|
if ([string]::IsNullOrWhiteSpace($line) -or $line.StartsWith("#")) { continue; }
|
||||||
|
|
||||||
|
if ($line -match '^([a-fA-F0-9]+)\s+') {
|
||||||
|
$fileAlgo = Get-AlgorithmFromHashLength -HashString ($Matches[1]);
|
||||||
|
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'; }
|
||||||
|
}
|
||||||
|
|
||||||
|
$verifyItems = @();
|
||||||
|
foreach ($line in ($lines)) {
|
||||||
|
if ([string]::IsNullOrWhiteSpace($line) -or $line.StartsWith("#")) { continue; }
|
||||||
|
|
||||||
|
$expectedHash = $null;
|
||||||
|
$targetFile = $null;
|
||||||
|
|
||||||
|
if ($line -match '^([a-fA-F0-9]+)\s+[* ]?(.*)$') {
|
||||||
|
$expectedHash = $Matches[1].ToLower();
|
||||||
|
$targetFile = $Matches[2].Trim().TrimStart('*');
|
||||||
|
}
|
||||||
|
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})[* ]?(.*)$') {
|
||||||
|
$expectedHash = $Matches[1].ToLower();
|
||||||
|
$targetFile = $Matches[2].Trim().TrimStart('*');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($expectedHash -and $targetFile) {
|
||||||
|
$targetFullPath = Join-Path -Path ($baseDir) -ChildPath ($targetFile);
|
||||||
|
$verifyItems += [PSCustomObject]@{
|
||||||
|
ExpectedHash = $expectedHash
|
||||||
|
TargetFile = $targetFile
|
||||||
|
TargetFullPath = $targetFullPath
|
||||||
|
BaseDir = $baseDir
|
||||||
};
|
};
|
||||||
$allItems += $item;
|
}
|
||||||
};
|
}
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
# --- DIRECT MATCH MODE (-m / -Match) ---
|
$totalFiles = $verifyItems.Count;
|
||||||
if ($PSCmdlet.ParameterSetName -eq 'Match') {
|
$currentIndex = 0;
|
||||||
if ([string]::IsNullOrWhiteSpace($Algorithm)) {
|
|
||||||
$Algorithm = Get-AlgorithmFromHashLength -HashString ($Match);
|
|
||||||
if (-not $Algorithm) { return; };
|
|
||||||
};
|
|
||||||
|
|
||||||
$expectedHash = $Match.Trim().ToLower();
|
$verifyItems | ForEach-Object -Parallel {
|
||||||
|
$item = $_
|
||||||
|
$algo = $using:fileAlgo
|
||||||
|
|
||||||
foreach ($item in ($allItems)) {
|
if (Test-Path -Path ($item.TargetFullPath) -PathType 'Leaf') {
|
||||||
$actualHash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
|
$actualHash = (Get-FileHash -Path ($item.TargetFullPath) -Algorithm ($algo)).Hash.ToLower();
|
||||||
$targetFile = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
|
|
||||||
if (-not $targetFile) { $targetFile = $item.Name; };
|
|
||||||
if ($targetFile.StartsWith(".\")) { $targetFile = $targetFile.Substring(2); };
|
|
||||||
|
|
||||||
if ($actualHash -eq $expectedHash) {
|
$displayPath = Resolve-Path -Path ($item.TargetFullPath) -Relative -ErrorAction 'SilentlyContinue';
|
||||||
Write-Host "${targetFile}: " -NoNewline;
|
if (-not $displayPath) { $displayPath = $item.TargetFile; }
|
||||||
|
if ($displayPath.StartsWith(".\") -or $displayPath.StartsWith("./")) { $displayPath = $displayPath.Substring(2); }
|
||||||
|
|
||||||
|
[PSCustomObject]@{
|
||||||
|
DisplayPath = $displayPath
|
||||||
|
Status = if ($actualHash -eq $item.ExpectedHash) { "OK" } else { "FAILED" }
|
||||||
|
IsError = $false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$displayPath = $item.TargetFile;
|
||||||
|
if ($item.BaseDir -ne (Get-Location).Path) {
|
||||||
|
$displayPath = Join-Path -Path (Resolve-Path -Path ($item.BaseDir) -Relative -ErrorAction 'SilentlyContinue') -ChildPath ($item.TargetFile);
|
||||||
|
if ($displayPath.StartsWith(".\") -or $displayPath.StartsWith("./")) { $displayPath = $displayPath.Substring(2); }
|
||||||
|
}
|
||||||
|
[PSCustomObject]@{
|
||||||
|
DisplayPath = $displayPath
|
||||||
|
Status = "FAILED open or read"
|
||||||
|
IsError = $true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} -ThrottleLimit 16 | ForEach-Object {
|
||||||
|
$currentIndex++
|
||||||
|
if ($totalFiles -gt 0) {
|
||||||
|
Write-Progress -Activity "Verifying Checksums ($fileAlgo)" -Status "Processing file $currentIndex of $totalFiles" -PercentComplete (($currentIndex / $totalFiles) * 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "$($_.DisplayPath): " -NoNewline;
|
||||||
|
if ($_.IsError) {
|
||||||
|
Write-Host "$($_.Status)" -ForegroundColor Red;
|
||||||
|
} elseif ($_.Status -eq "OK") {
|
||||||
Write-Host "OK" -ForegroundColor Green;
|
Write-Host "OK" -ForegroundColor Green;
|
||||||
} else {
|
} else {
|
||||||
Write-Host "${targetFile}: " -NoNewline;
|
|
||||||
Write-Host "FAILED" -ForegroundColor Red;
|
Write-Host "FAILED" -ForegroundColor Red;
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
# --- VERIFICATION MODE (-c / -Check) ---
|
|
||||||
elseif ($PSCmdlet.ParameterSetName -eq 'Verify') {
|
|
||||||
$checkFiles = @();
|
|
||||||
if ($Recursive) {
|
|
||||||
$checkFileName = Split-Path -Path ($Check) -Leaf;
|
|
||||||
$checkFiles = Get-ChildItem -Path '.\' -Filter ($checkFileName) -Recurse -File -ErrorAction 'SilentlyContinue';
|
|
||||||
} else {
|
|
||||||
if (Test-Path -Path ($Check) -PathType 'Leaf') {
|
|
||||||
$checkFiles = @(Get-Item -Path ($Check));
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
if (-not $checkFiles -or $checkFiles.Count -eq 0) {
|
|
||||||
Write-Error "checksum: $($Check)`: No such file or directory";
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
foreach ($cf in ($checkFiles)) {
|
|
||||||
$lines = Get-Content -Path ($cf.FullName);
|
|
||||||
$baseDir =$cf.DirectoryName;
|
|
||||||
|
|
||||||
$fileAlgo =$Algorithm;
|
|
||||||
if ([string]::IsNullOrWhiteSpace($fileAlgo)) {
|
|
||||||
foreach ($line in ($lines)) {
|
|
||||||
if ([string]::IsNullOrWhiteSpace($line) -or$line.StartsWith("#")) { continue; };
|
|
||||||
if ($line -match '^([a-fA-F0-9]+)\s+') {
|
|
||||||
$fileAlgo = Get-AlgorithmFromHashLength -HashString ($Matches[1]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if ([string]::IsNullOrWhiteSpace($fileAlgo)) {$fileAlgo = 'SHA256'; };
|
|
||||||
};
|
|
||||||
|
|
||||||
foreach ($line in ($lines)) {
|
|
||||||
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);
|
|
||||||
|
|
||||||
if (Test-Path -Path ($targetFullPath) -PathType 'Leaf') {$actualHash = (Get-FileHash -Path ($targetFullPath) -Algorithm ($fileAlgo)).Hash.ToLower();
|
|
||||||
|
|
||||||
$displayPath = Resolve-Path -Path ($targetFullPath) -Relative -ErrorAction 'SilentlyContinue';
|
|
||||||
if (-not $displayPath) { $displayPath =$targetFile; };
|
|
||||||
if ($displayPath.StartsWith(".\")) { $displayPath =$displayPath.Substring(2); };
|
|
||||||
|
|
||||||
if ($actualHash -eq$expectedHash) {
|
|
||||||
Write-Host "${displayPath}: " -NoNewline;
|
|
||||||
Write-Host "OK" -ForegroundColor Green;
|
|
||||||
} else {
|
|
||||||
Write-Host "${displayPath}: " -NoNewline;
|
|
||||||
Write-Host "FAILED" -ForegroundColor Red;
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
$displayPath =$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); };
|
|
||||||
};
|
|
||||||
Write-Host "${displayPath}: " -NoNewline;
|
|
||||||
Write-Host "FAILED open or read" -ForegroundColor Red;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
# --- GENERATION MODE ---
|
|
||||||
else {
|
|
||||||
if ([string]::IsNullOrWhiteSpace($Algorithm)) {$Algorithm = 'SHA256';
|
|
||||||
};
|
|
||||||
|
|
||||||
$wantOutFile = $SaveToFile.IsPresent -or (-not [string]::IsNullOrWhiteSpace($OutFile));
|
|
||||||
|
|
||||||
if ($wantOutFile) {
|
|
||||||
if ($Recursive -and$isWildcardGlobal) {
|
|
||||||
$groupedItems =$allItems | Group-Object DirectoryName;
|
|
||||||
|
|
||||||
foreach ($group in ($groupedItems)) {
|
|
||||||
$dirPath =$group.Name;
|
|
||||||
$filesInDir =$group.Group;
|
|
||||||
|
|
||||||
if ([string]::IsNullOrWhiteSpace($OutFile)) {
|
|
||||||
$currentOutFile = Join-Path -Path ($dirPath) -ChildPath "checksums.$($Algorithm.ToLower())";
|
|
||||||
} else {
|
|
||||||
$currentOutFile = Join-Path -Path ($dirPath) -ChildPath ($OutFile);
|
|
||||||
};
|
|
||||||
|
|
||||||
$null = New-Item -Path ($currentOutFile) -ItemType 'File' -Force;
|
|
||||||
|
|
||||||
foreach ($item in ($filesInDir)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();
|
|
||||||
$relativePath =$item.Name;
|
|
||||||
|
|
||||||
$outputLine = "$hash$relativePath";
|
|
||||||
Add-Content -Path ($currentOutFile) -Value ($outputLine);
|
|
||||||
Write-Output $outputLine;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$singleOutFile =$null;
|
|
||||||
|
|
||||||
if ([string]::IsNullOrWhiteSpace($OutFile)) {
|
|
||||||
if ($isWildcardGlobal) {$singleOutFile = "checksums.$($Algorithm.ToLower())";
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$singleOutFile =$OutFile;
|
|
||||||
};
|
|
||||||
|
|
||||||
if ($singleOutFile) {
|
|
||||||
$null = New-Item -Path ($singleOutFile) -ItemType 'File' -Force;
|
|
||||||
};
|
|
||||||
|
|
||||||
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(".\")) {
|
|
||||||
$relativePath =$relativePath.Substring(2);
|
|
||||||
};
|
|
||||||
|
|
||||||
$outputLine = "$hash$relativePath";
|
|
||||||
|
|
||||||
if ($singleOutFile) {
|
|
||||||
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);
|
|
||||||
};
|
|
||||||
|
|
||||||
Write-Output $outputLine;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
else {
|
Write-Progress -Activity "Verifying Checksums ($fileAlgo)" -Completed;
|
||||||
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(".\")) {
|
|
||||||
$relativePath =$relativePath.Substring(2);
|
|
||||||
};
|
|
||||||
|
|
||||||
$outputLine = "$hash$relativePath";
|
|
||||||
Write-Output $outputLine;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
# --- GENERATION MODE ---
|
||||||
|
else {
|
||||||
|
if ([string]::IsNullOrWhiteSpace($Algorithm)) {
|
||||||
|
$Algorithm = 'SHA256';
|
||||||
|
}
|
||||||
|
|
||||||
end {}
|
$wantCustomOut = $PSBoundParameters.ContainsKey('OutFile');
|
||||||
|
$wantAutoOut = $Save.IsPresent;
|
||||||
|
|
||||||
|
if ($wantCustomOut -or $wantAutoOut) {
|
||||||
|
if ($Recursive -and $isWildcardGlobal) {
|
||||||
|
$groupedItems = $allItems | Group-Object DirectoryName;
|
||||||
|
|
||||||
|
foreach ($group in ($groupedItems)) {
|
||||||
|
$dirPath = $group.Name;
|
||||||
|
$filesInDir = $group.Group;
|
||||||
|
|
||||||
|
if ($wantCustomOut) {
|
||||||
|
$currentOutFile = Join-Path -Path ($dirPath) -ChildPath ($OutFile);
|
||||||
|
} else {
|
||||||
|
$currentOutFile = Join-Path -Path ($dirPath) -ChildPath "checksums.$($Algorithm.ToLower())";
|
||||||
|
}
|
||||||
|
|
||||||
|
$totalFiles = $filesInDir.Count;
|
||||||
|
$currentIndex = 0;
|
||||||
|
$linesToWrite = @();
|
||||||
|
|
||||||
|
$filesInDir | ForEach-Object -Parallel {
|
||||||
|
$item = $_
|
||||||
|
$algo = $using:Algorithm
|
||||||
|
|
||||||
|
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).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);
|
||||||
|
}
|
||||||
|
|
||||||
|
[PSCustomObject]@{
|
||||||
|
Item = $item
|
||||||
|
Hash = $hash
|
||||||
|
RelativePath = $relativePath
|
||||||
|
OutputLine = "${hash} ${relativePath}"
|
||||||
|
ItemOutFile = "$($item.FullName).$($algo.ToLower())"
|
||||||
|
}
|
||||||
|
} -ThrottleLimit 16 | ForEach-Object {
|
||||||
|
$currentIndex++;
|
||||||
|
if ($showProgress -and $totalFiles -gt 0) {
|
||||||
|
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Status "Directory: $(Split-Path $dirPath -Leaf) [$currentIndex/$totalFiles]" -PercentComplete (($currentIndex / $totalFiles) * 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
$linesToWrite += $_.OutputLine;
|
||||||
|
Write-Output $_.OutputLine;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($showProgress) {
|
||||||
|
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set-Content -Path ($currentOutFile) -Value $linesToWrite -Encoding utf8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if ($wantCustomOut -or ($wantAutoOut -and $isWildcardGlobal)) {
|
||||||
|
if ($wantCustomOut) {
|
||||||
|
$singleOutFile = $OutFile;
|
||||||
|
} else {
|
||||||
|
$singleOutFile = "checksums.$($Algorithm.ToLower())";
|
||||||
|
}
|
||||||
|
|
||||||
|
$totalFiles = $allItems.Count;
|
||||||
|
$currentIndex = 0;
|
||||||
|
$linesToWrite = @();
|
||||||
|
|
||||||
|
$allItems | ForEach-Object -Parallel {
|
||||||
|
$item = $_
|
||||||
|
$algo = $using:Algorithm
|
||||||
|
|
||||||
|
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).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);
|
||||||
|
}
|
||||||
|
|
||||||
|
[PSCustomObject]@{
|
||||||
|
Item = $item
|
||||||
|
Hash = $hash
|
||||||
|
RelativePath = $relativePath
|
||||||
|
OutputLine = "${hash} ${relativePath}"
|
||||||
|
ItemOutFile = "$($item.FullName).$($algo.ToLower())"
|
||||||
|
}
|
||||||
|
} -ThrottleLimit 16 | ForEach-Object {
|
||||||
|
$currentIndex++;
|
||||||
|
if ($showProgress -and $totalFiles -gt 0) {
|
||||||
|
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Status "Processing file $currentIndex of $totalFiles" -PercentComplete (($currentIndex / $totalFiles) * 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
$linesToWrite += $_.OutputLine;
|
||||||
|
Write-Output $_.OutputLine;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($showProgress) {
|
||||||
|
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set-Content -Path ($singleOutFile) -Value $linesToWrite -Encoding utf8;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$totalFiles = $allItems.Count;
|
||||||
|
$currentIndex = 0;
|
||||||
|
|
||||||
|
$allItems | ForEach-Object -Parallel {
|
||||||
|
$item = $_
|
||||||
|
$algo = $using:Algorithm
|
||||||
|
|
||||||
|
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).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);
|
||||||
|
}
|
||||||
|
|
||||||
|
[PSCustomObject]@{
|
||||||
|
Item = $item
|
||||||
|
Hash = $hash
|
||||||
|
RelativePath = $relativePath
|
||||||
|
OutputLine = "${hash} ${relativePath}"
|
||||||
|
ItemOutFile = "$($item.FullName).$($algo.ToLower())"
|
||||||
|
}
|
||||||
|
} -ThrottleLimit 16 | ForEach-Object {
|
||||||
|
$currentIndex++;
|
||||||
|
if ($showProgress -and $totalFiles -gt 0) {
|
||||||
|
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Status "Processing file $currentIndex of $totalFiles" -PercentComplete (($currentIndex / $totalFiles) * 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Output $_.OutputLine;
|
||||||
|
Set-Content -Path ($_.ItemOutFile) -Value $_.OutputLine -Encoding utf8;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($showProgress) {
|
||||||
|
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$totalFiles = $allItems.Count;
|
||||||
|
$currentIndex = 0;
|
||||||
|
|
||||||
|
$allItems | ForEach-Object -Parallel {
|
||||||
|
$item = $_
|
||||||
|
$algo = $using:Algorithm
|
||||||
|
|
||||||
|
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).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);
|
||||||
|
}
|
||||||
|
|
||||||
|
[PSCustomObject]@{
|
||||||
|
Item = $item
|
||||||
|
Hash = $hash
|
||||||
|
RelativePath = $relativePath
|
||||||
|
OutputLine = "${hash} ${relativePath}"
|
||||||
|
ItemOutFile = "$($item.FullName).$($algo.ToLower())"
|
||||||
|
}
|
||||||
|
} -ThrottleLimit 16 | ForEach-Object {
|
||||||
|
$currentIndex++;
|
||||||
|
if ($showProgress -and $totalFiles -gt 0) {
|
||||||
|
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Status "Processing file $currentIndex of $totalFiles" -PercentComplete (($currentIndex / $totalFiles) * 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Output $_.OutputLine;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($showProgress) {
|
||||||
|
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
param (
|
||||||
|
[Parameter(Mandatory = $true, Position = 0)]
|
||||||
|
[string]$Ext1,
|
||||||
|
|
||||||
|
[Parameter(Mandatory = $true, Position = 1)]
|
||||||
|
[string]$Ext2,
|
||||||
|
|
||||||
|
[int]$Jobs = 4 # Number of simultaneous conversions
|
||||||
|
)
|
||||||
|
|
||||||
|
$sourceExt = $Ext1.TrimStart('.')
|
||||||
|
$targetExt = $Ext2.TrimStart('.')
|
||||||
|
$audioExts = @('opus', 'ogg', 'flac', 'wav')
|
||||||
|
|
||||||
|
$files = Get-ChildItem -File -Recurse -Filter "*.$sourceExt"
|
||||||
|
|
||||||
|
if ($files.Count -eq 0) {
|
||||||
|
Write-Host "No files matching *.$sourceExt found." -ForegroundColor Yellow
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "Starting conversion using $Jobs parallel jobs..." -ForegroundColor Green
|
||||||
|
|
||||||
|
$files | ForEach-Object -Parallel {
|
||||||
|
$file = $_
|
||||||
|
$targetExt = $using:targetExt
|
||||||
|
$audioExts = $using:audioExts
|
||||||
|
|
||||||
|
$outputPath = Join-Path -Path $file.DirectoryName -ChildPath "$($file.BaseName).$targetExt"
|
||||||
|
$relativePath = Resolve-Path -Path $file.FullName -Relative
|
||||||
|
|
||||||
|
Write-Host "Processing: $relativePath" -ForegroundColor Cyan
|
||||||
|
|
||||||
|
if ($audioExts -contains $targetExt.ToLower()) {
|
||||||
|
ffmpeg -y -i $file.FullName -c:a libopus -b:a 128k -vbr on $outputPath 2>$null
|
||||||
|
} else {
|
||||||
|
# -preset 8 drastically speeds up SVT-AV1 while maintaining high quality
|
||||||
|
ffmpeg -y -i $file.FullName -c:v libsvtav1 -crf 26 -preset 8 -c:a libopus -b:a 128k $outputPath 2>$null
|
||||||
|
}
|
||||||
|
} -ThrottleLimit $Jobs
|
||||||
|
|
||||||
|
Write-Host "Finished processing $($files.Count) file(s)." -ForegroundColor Green
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
# Step 1: Recursively delete all existing .sha256 files
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
Write-Host "Deleting existing .sha256 files recursively..." -ForegroundColor Yellow
|
||||||
|
Get-ChildItem -Path . -Recurse -Filter "*.sha256" -File | Remove-Item -Force
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
# Step 2: Run checksums in current directory and every subdirectory
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
Write-Host "Generating checksums in each subfolder..." -ForegroundColor Cyan
|
||||||
|
|
||||||
|
# Gather current working directory and all child directories
|
||||||
|
$allDirs = @((Get-Item .).FullName) + (Get-ChildItem -Path . -Recurse -Directory | Select-Object -ExpandProperty FullName)
|
||||||
|
|
||||||
|
foreach ($dir in $allDirs) {
|
||||||
|
Push-Location $dir
|
||||||
|
try {
|
||||||
|
Write-Host "Updating checksums in: $dir" -ForegroundColor Gray
|
||||||
|
checksum * -o
|
||||||
|
} finally {
|
||||||
|
Pop-Location
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
# Step 3: Recursively verify checksums in each directory
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
Write-Host "Verifying checksums in each subfolder..." -ForegroundColor Cyan
|
||||||
|
|
||||||
|
foreach ($dir in $allDirs) {
|
||||||
|
Push-Location $dir
|
||||||
|
try {
|
||||||
|
if (Test-Path "checksums.sha256") {
|
||||||
|
Write-Host "Verifying: $dir" -ForegroundColor Gray
|
||||||
|
checksum -c checksums.sha256
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
Pop-Location
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "All conversions, checksum creations, and verifications completed successfully." -ForegroundColor Green
|
||||||
Reference in New Issue
Block a user