Files
LanMountainDesktop/scripts/Sign-FileMap.ps1

57 lines
1.5 KiB
PowerShell
Raw Normal View History

2026-04-16 01:59:21 +08:00
param(
[Parameter(Mandatory = $true)]
2026-04-16 01:59:21 +08:00
[string]$FilesJsonPath,
[Parameter(Mandatory = $true)]
2026-04-16 01:59:21 +08:00
[string]$PrivateKeyPath,
[Parameter(Mandatory = $false)]
2026-04-16 01:59:21 +08:00
[string]$OutputPath
)
$ErrorActionPreference = "Stop"
if ($PSVersionTable.PSVersion.Major -lt 7) {
throw "Sign-FileMap.ps1 requires PowerShell 7 or newer."
}
2026-04-16 01:59:21 +08:00
if (-not (Test-Path -LiteralPath $FilesJsonPath)) {
throw "Manifest file not found: $FilesJsonPath"
2026-04-16 01:59:21 +08:00
}
if (-not (Test-Path -LiteralPath $PrivateKeyPath)) {
throw "Private key file not found: $PrivateKeyPath"
2026-04-16 01:59:21 +08:00
}
if ([string]::IsNullOrWhiteSpace($OutputPath)) {
$OutputPath = "$FilesJsonPath.sig"
}
$resolvedManifestPath = (Resolve-Path -LiteralPath $FilesJsonPath).Path
$manifestBytes = [System.IO.File]::ReadAllBytes($resolvedManifestPath)
2026-04-16 01:59:21 +08:00
$privateKeyPem = Get-Content -LiteralPath $PrivateKeyPath -Raw
if ([string]::IsNullOrWhiteSpace($privateKeyPem)) {
throw "Private key PEM is empty: $PrivateKeyPath"
}
2026-04-16 01:59:21 +08:00
$rsa = [System.Security.Cryptography.RSA]::Create()
try {
$rsa.ImportFromPem($privateKeyPem)
$signatureBytes = $rsa.SignData(
$manifestBytes,
[System.Security.Cryptography.HashAlgorithmName]::SHA256,
[System.Security.Cryptography.RSASignaturePadding]::Pkcs1
)
}
finally {
$rsa.Dispose()
}
2026-04-16 01:59:21 +08:00
$signatureBase64 = [Convert]::ToBase64String($signatureBytes)
[System.IO.File]::WriteAllText($OutputPath, $signatureBase64, [System.Text.Encoding]::ASCII)
2026-04-16 01:59:21 +08:00
Write-Host "Signed manifest file."
Write-Host "Manifest: $FilesJsonPath"
Write-Host "Signature: $OutputPath"