Skip to content

Commit fb94ff8

Browse files
committed
refactor(download): move hash-related functions to seperate lib file, update imports
1 parent 08d22f2 commit fb94ff8

9 files changed

Lines changed: 73 additions & 77 deletions

File tree

bin/checkhashes.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ param(
4242

4343
. "$PSScriptRoot\..\lib\core.ps1"
4444
. "$PSScriptRoot\..\lib\manifest.ps1"
45+
. "$PSScriptRoot\..\lib\hash.ps1" # 'get_hash'
4546
. "$PSScriptRoot\..\lib\buckets.ps1"
4647
. "$PSScriptRoot\..\lib\autoupdate.ps1"
4748
. "$PSScriptRoot\..\lib\json.ps1"

lib/autoupdate.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Must included with 'json.ps1'
22

3+
. "$PSScriptRoot\..\lib\hash.ps1" # 'get_hash'
4+
35
function format_hash([String] $hash) {
46
$hash = $hash.toLower()
57

lib/download.ps1

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Description: Functions for downloading files
22

33
. "$PSScriptRoot\..\lib\core.ps1"
4+
. "$PSScriptRoot\..\lib\hash.ps1" # 'hash_for_url' 'check_hash'
45
. "$PSScriptRoot\..\lib\virustotal.ps1"
56

67
## Meta downloader
@@ -733,69 +734,6 @@ function url_remote_filename($url) {
733734
return $basename
734735
}
735736

736-
### Hash-related functions
737-
738-
function hash_for_url($manifest, $url, $arch) {
739-
$hashes = @(hash $manifest $arch) | Where-Object { $_ -ne $null }
740-
741-
if ($hashes.length -eq 0) { return $null }
742-
743-
$urls = @(script:url $manifest $arch)
744-
745-
$index = [array]::IndexOf($urls, $url)
746-
if ($index -eq -1) { abort "Couldn't find hash in manifest for '$url'." }
747-
748-
@($hashes)[$index]
749-
}
750-
751-
function check_hash($file, $hash, $app_name) {
752-
# returns (ok, err)
753-
if (!$hash) {
754-
warn "Warning: No hash in manifest. SHA256 for '$(fname $file)' is:`n $((Get-FileHash -Path $file -Algorithm SHA256).Hash.ToLower())"
755-
return $true, $null
756-
}
757-
758-
Write-Host 'Checking hash of ' -NoNewline
759-
Write-Host $(url_remote_filename $url) -ForegroundColor Cyan -NoNewline
760-
Write-Host ' ... ' -NoNewline
761-
$algorithm, $expected = get_hash $hash
762-
if ($null -eq $algorithm) {
763-
return $false, "Hash type '$algorithm' isn't supported."
764-
}
765-
766-
$actual = (Get-FileHash -Path $file -Algorithm $algorithm).Hash.ToLower()
767-
$expected = $expected.ToLower()
768-
769-
if ($actual -ne $expected) {
770-
$msg = "Hash check failed!`n"
771-
$msg += "App: $app_name`n"
772-
$msg += "URL: $url`n"
773-
if (Test-Path $file) {
774-
$msg += "First bytes: $((get_magic_bytes_pretty $file ' ').ToUpper())`n"
775-
}
776-
if ($expected -or $actual) {
777-
$msg += "Expected: $expected`n"
778-
$msg += "Actual: $actual"
779-
}
780-
return $false, $msg
781-
}
782-
Write-Host 'ok.' -f Green
783-
return $true, $null
784-
}
785-
786-
function get_hash([String] $multihash) {
787-
$type, $hash = $multihash -split ':'
788-
if (!$hash) {
789-
# no type specified, assume sha256
790-
$type, $hash = 'sha256', $multihash
791-
}
792-
793-
if (@('md5', 'sha1', 'sha256', 'sha512') -notcontains $type) {
794-
return $null, "Hash type '$type' isn't supported."
795-
}
796-
797-
return $type, $hash.ToLower()
798-
}
799737

800738
# Setup proxy globally
801739
setup_proxy

lib/hash.ps1

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
### Hash-related functions
2+
3+
function hash_for_url($manifest, $url, $arch) {
4+
$hashes = @(hash $manifest $arch) | Where-Object { $_ -ne $null }
5+
6+
if ($hashes.length -eq 0) { return $null }
7+
8+
$urls = @(script:url $manifest $arch)
9+
10+
$index = [array]::IndexOf($urls, $url)
11+
if ($index -eq -1) { abort "Couldn't find hash in manifest for '$url'." }
12+
13+
@($hashes)[$index]
14+
}
15+
16+
function check_hash($file, $hash, $app_name) {
17+
# returns (ok, err)
18+
if (!$hash) {
19+
warn "Warning: No hash in manifest. SHA256 for '$(fname $file)' is:`n $((Get-FileHash -Path $file -Algorithm SHA256).Hash.ToLower())"
20+
return $true, $null
21+
}
22+
23+
Write-Host 'Checking hash of ' -NoNewline
24+
Write-Host $(url_remote_filename $url) -ForegroundColor Cyan -NoNewline
25+
Write-Host ' ... ' -NoNewline
26+
$algorithm, $expected = get_hash $hash
27+
if ($null -eq $algorithm) {
28+
return $false, "Hash type '$algorithm' isn't supported."
29+
}
30+
31+
$actual = (Get-FileHash -Path $file -Algorithm $algorithm).Hash.ToLower()
32+
$expected = $expected.ToLower()
33+
34+
if ($actual -ne $expected) {
35+
$msg = "Hash check failed!`n"
36+
$msg += "App: $app_name`n"
37+
$msg += "URL: $url`n"
38+
if (Test-Path $file) {
39+
$msg += "First bytes: $((get_magic_bytes_pretty $file ' ').ToUpper())`n"
40+
}
41+
if ($expected -or $actual) {
42+
$msg += "Expected: $expected`n"
43+
$msg += "Actual: $actual"
44+
}
45+
return $false, $msg
46+
}
47+
Write-Host 'ok.' -f Green
48+
return $true, $null
49+
}
50+
51+
function get_hash([String] $multihash) {
52+
$type, $hash = $multihash -split ':'
53+
if (!$hash) {
54+
# no type specified, assume sha256
55+
$type, $hash = 'sha256', $multihash
56+
}
57+
58+
if (@('md5', 'sha1', 'sha256', 'sha512') -notcontains $type) {
59+
return $null, "Hash type '$type' isn't supported."
60+
}
61+
62+
return $type, $hash.ToLower()
63+
}

lib/install.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
. "$PSScriptRoot\..\lib\hash.ps1" # 'check_hash'
2+
13
function nightly_version($quiet = $false) {
24
if (!$quiet) {
35
warn "This is a nightly version. Downloaded files won't be verified."

lib/virustotal.ps1

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
. "$PSScriptRoot\json.ps1" # 'json_path'
2-
# . "$PSScriptRoot\download.ps1" # 'hash_for_url'
2+
. "$PSScriptRoot\..\lib\hash.ps1" # 'hash_for_url'
33

44
# Error codes
55
$script:_ERR_UNSAFE = 2
@@ -314,16 +314,3 @@ function virustotal_check_app($app, $manifest, $architecture, $api_key, $scan) {
314314
Check-VirusTotalUrl $app $url $hash $api_key $scan
315315
}
316316
}
317-
318-
function hash_for_url($manifest, $url, $arch) {
319-
$hashes = @(hash $manifest $arch) | Where-Object { $_ -ne $null }
320-
321-
if ($hashes.length -eq 0) { return $null }
322-
323-
$urls = @(script:url $manifest $arch)
324-
325-
$index = [array]::IndexOf($urls, $url)
326-
if ($index -eq -1) { abort "Couldn't find hash in manifest for '$url'." }
327-
328-
@($hashes)[$index]
329-
}

libexec/scoop-download.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
. "$PSScriptRoot\..\lib\getopt.ps1"
2323
. "$PSScriptRoot\..\lib\json.ps1" # 'autoupdate.ps1' (indirectly)
24+
. "$PSScriptRoot\..\lib\hash.ps1" # 'hash_for_url' 'check_hash'
2425
. "$PSScriptRoot\..\lib\autoupdate.ps1" # 'generate_user_manifest' (indirectly)
2526
. "$PSScriptRoot\..\lib\versions.ps1" # 'Select-CurrentVersion'
2627
. "$PSScriptRoot\..\lib\manifest.ps1" # 'generate_user_manifest' 'Get-Manifest'

libexec/scoop-install.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
. "$PSScriptRoot\..\lib\json.ps1" # 'autoupdate.ps1' 'manifest.ps1' (indirectly)
3333
. "$PSScriptRoot\..\lib\autoupdate.ps1" # 'generate_user_manifest' (indirectly)
3434
. "$PSScriptRoot\..\lib\manifest.ps1" # 'generate_user_manifest' 'Get-Manifest' 'Select-CurrentVersion' (indirectly)
35+
. "$PSScriptRoot\..\lib\hash.ps1" # 'check_hash'
3536
. "$PSScriptRoot\..\lib\system.ps1"
3637
. "$PSScriptRoot\..\lib\install.ps1"
3738
. "$PSScriptRoot\..\lib\download.ps1"

libexec/scoop-update.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
. "$PSScriptRoot\..\lib\psmodules.ps1"
2323
. "$PSScriptRoot\..\lib\decompress.ps1"
2424
. "$PSScriptRoot\..\lib\manifest.ps1"
25+
. "$PSScriptRoot\..\lib\hash.ps1" # 'hash_for_url' 'check_hash'
2526
. "$PSScriptRoot\..\lib\versions.ps1"
2627
. "$PSScriptRoot\..\lib\depends.ps1"
2728
. "$PSScriptRoot\..\lib\install.ps1"

0 commit comments

Comments
 (0)