The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/4371

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
`distrobuilder` uses `DownloadFileSha256` to download and verify image tarballs. Gentoo however only provides sha512 checksums which makes the function unusable.

This PR adds a second function `DownloadFileSha512` which takes care of files with sha512 checksums.

Signed-off-by: Thomas Hipp <thomas.h...@canonical.com>
From 334b6d90cd848416b6daf44eb0daa8e2850897a2 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.h...@canonical.com>
Date: Wed, 28 Mar 2018 22:28:40 +0200
Subject: [PATCH] shared: Add downloader with sha512 verification

Signed-off-by: Thomas Hipp <thomas.h...@canonical.com>
---
 shared/util.go | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/shared/util.go b/shared/util.go
index 31cece0ec..54001b622 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -5,10 +5,12 @@ import (
        "bytes"
        "crypto/rand"
        "crypto/sha256"
+       "crypto/sha512"
        "encoding/gob"
        "encoding/hex"
        "encoding/json"
        "fmt"
+       "hash"
        "io"
        "io/ioutil"
        "net/http"
@@ -899,6 +901,14 @@ func EscapePathFstab(path string) string {
 }
 
 func DownloadFileSha256(httpClient *http.Client, useragent string, progress 
func(progress ioprogress.ProgressData), canceler *cancel.Canceler, filename 
string, url string, hash string, target io.WriteSeeker) (int64, error) {
+       return downloadFileSha(httpClient, useragent, progress, canceler, 
filename, url, hash, sha256.New(), target)
+}
+
+func DownloadFileSha512(httpClient *http.Client, useragent string, progress 
func(progress ioprogress.ProgressData), canceler *cancel.Canceler, filename 
string, url string, hash string, target io.WriteSeeker) (int64, error) {
+       return downloadFileSha(httpClient, useragent, progress, canceler, 
filename, url, hash, sha512.New(), target)
+}
+
+func downloadFileSha(httpClient *http.Client, useragent string, progress 
func(progress ioprogress.ProgressData), canceler *cancel.Canceler, filename 
string, url string, hash string, sha hash.Hash, target io.WriteSeeker) (int64, 
error) {
        // Always seek to the beginning
        target.Seek(0, 0)
 
@@ -942,13 +952,12 @@ func DownloadFileSha256(httpClient *http.Client, 
useragent string, progress func
                }
        }
 
-       sha256 := sha256.New()
-       size, err := io.Copy(io.MultiWriter(target, sha256), body)
+       size, err := io.Copy(io.MultiWriter(target, sha), body)
        if err != nil {
                return -1, err
        }
 
-       result := fmt.Sprintf("%x", sha256.Sum(nil))
+       result := fmt.Sprintf("%x", sha.Sum(nil))
        if result != hash {
                return -1, fmt.Errorf("Hash mismatch for %s: %s != %s", url, 
result, hash)
        }
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to