diff --git a/CHANGELOG.md b/CHANGELOG.md index fecb9eef35..b2e431a222 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [Unreleased](https://github.com/ScoopInstaller/Scoop/compare/master...develop) + +### Features + +- **autoupdate**: Try decoding base64 encoded hash values in `format_hash` ([#6271](https://github.com/ScoopInstaller/Scoop/issues/6271)) + ## [v0.5.3](https://github.com/ScoopInstaller/Scoop/compare/v0.5.2...v0.5.3) - 2024-12-31 ### Bug Fixes diff --git a/lib/autoupdate.ps1 b/lib/autoupdate.ps1 index 5cef1622a0..2dccc0d3be 100644 --- a/lib/autoupdate.ps1 +++ b/lib/autoupdate.ps1 @@ -1,6 +1,21 @@ # Must included with 'json.ps1' function format_hash([String] $hash) { + + # convert base64 encoded hash values to hex string + if ($hash -match '^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$') { + $base64 = $matches[0] + if (!($hash -match '^[a-fA-F0-9]+$') -and $hash.Length -notin @(32, 40, 64, 128)) { + try { + $hash = ([System.Convert]::FromBase64String($base64) | ForEach-Object { $_.ToString('x2') }) -join '' + } catch { + $hash = $hash + } + } + } + + debug $hash + $hash = $hash.toLower() switch ($hash.Length) { 32 { $hash = "md5:$hash" } # md5 @@ -74,18 +89,6 @@ function find_hash_in_textfile([String] $url, [Hashtable] $substitutions, [Strin $hash = $matches[1] -replace '\s', '' } - # convert base64 encoded hash values - if ($hash -match '^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$') { - $base64 = $matches[0] - if (!($hash -match '^[a-fA-F0-9]+$') -and $hash.Length -notin @(32, 40, 64, 128)) { - try { - $hash = ([System.Convert]::FromBase64String($base64) | ForEach-Object { $_.ToString('x2') }) -join '' - } catch { - $hash = $hash - } - } - } - # find hash with filename in $hashfile if ($hash.Length -eq 0) { $filenameRegex = "([a-fA-F0-9]{32,128})[\x20\t]+.*`$basename(?:\s|$)|`$basename[\x20\t]+.*?([a-fA-F0-9]{32,128})" @@ -129,6 +132,7 @@ function find_hash_in_json([String] $url, [Hashtable] $substitutions, [String] $ if (!$hash) { $hash = json_path_legacy $json $jsonpath $substitutions } + return format_hash $hash } @@ -186,8 +190,7 @@ function find_hash_in_headers([String] $url) { $res = $req.GetResponse() if (([int]$res.StatusCode -ge 300) -and ([int]$res.StatusCode -lt 400)) { if ($res.Headers['Digest'] -match 'SHA-256=([^,]+)' -or $res.Headers['Digest'] -match 'SHA=([^,]+)' -or $res.Headers['Digest'] -match 'MD5=([^,]+)') { - $hash = ([System.Convert]::FromBase64String($matches[1]) | ForEach-Object { $_.ToString('x2') }) -join '' - debug $hash + $hash = $matches[1] } } $res.Close()