diff --git a/images/win/scripts/ImageHelpers/ImageHelpers.psm1 b/images/win/scripts/ImageHelpers/ImageHelpers.psm1 index a83ab7831..779ca579b 100644 --- a/images/win/scripts/ImageHelpers/ImageHelpers.psm1 +++ b/images/win/scripts/ImageHelpers/ImageHelpers.psm1 @@ -55,4 +55,6 @@ Export-ModuleMember -Function @( 'Get-WindowsUpdatesHistory' 'New-ItemPath' 'Get-ModuleVersionAsJob' + 'Use-ChecksumСomparison' + 'Get-HashFromGitHubReleaseBody' ) diff --git a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 index e727de9ca..c482515f8 100644 --- a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 +++ b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 @@ -630,3 +630,60 @@ function Get-GitHubPackageDownloadUrl { return $downloadUrl } +function Use-ChecksumСomparison { + param ( + [Parameter(Mandatory=$true)] + [string]$LocalFileHash, + [Parameter(Mandatory=$true)] + [string]$DistributorFileHash + ) + + Write-Verbose "Performing checksum verification" + + if ($LocalFileHash -ne $DistributorFileHash) { + throw "Checksum verification failed. Expected hash: $DistributorFileHash; Actual hash: $LocalFileHash." + } else { + Write-Verbose "Checksum verification passed" + } +} + +function Get-HashFromGitHubReleaseBody { + param ( + [string]$RepoOwner, + [string]$RepoName, + [Parameter(Mandatory=$true)] + [string]$FileName, + [string]$Url, + [string]$Version = "latest", + [boolean]$IsPrerelease = $false, + [int]$SearchInCount = 100, + [string]$Delimiter = '|', + [int]$WordNumber = 1 + ) + + if ($Url) { + $releaseUrl = $Url + } else { + if ($Version -eq "latest") { + $releaseUrl = "https://api.github.com/repos/${RepoOwner}/${RepoName}/releases/latest" + } else { + $json = Invoke-RestMethod -Uri "https://api.github.com/repos/${RepoOwner}/${RepoName}/releases?per_page=${SearchInCount}" + $tags = $json.Where{ $_.prerelease -eq $IsPrerelease }.tag_name + $tag = $tags -match $Version + if (-not $tag) { + throw "Failed to get a tag name for version $Version." + } + $releaseUrl = "https://api.github.com/repos/${RepoOwner}/${RepoName}/releases/tag/$tag" + } + } + $body = (Invoke-RestMethod -Uri $releaseUrl).body -replace('`', "") -join "`n" + $matchingLine = $body.Split("`n") | Where-Object { $_ -like "*$FileName*" } + if ([string]::IsNullOrEmpty($matchingLine)) { + throw "File name '$FileName' not found in release body." + } + $result = $matchingLine.Split($Delimiter)[$WordNumber] -replace "[^a-zA-Z0-9]", "" + if ([string]::IsNullOrEmpty($result)) { + throw "Empty result. Check Split method parameters (delimiter and/or word number) for the matching line." + } + return $result +}