|
1 | 1 | # Description: Functions for downloading files |
2 | 2 |
|
3 | 3 | . "$PSScriptRoot\..\lib\core.ps1" |
| 4 | +. "$PSScriptRoot\..\lib\hash.ps1" # 'hash_for_url' 'check_hash' |
4 | 5 | . "$PSScriptRoot\..\lib\virustotal.ps1" |
5 | 6 |
|
6 | 7 | ## Meta downloader |
@@ -733,69 +734,6 @@ function url_remote_filename($url) { |
733 | 734 | return $basename |
734 | 735 | } |
735 | 736 |
|
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 | | -} |
799 | 737 |
|
800 | 738 | # Setup proxy globally |
801 | 739 | setup_proxy |
0 commit comments