[lxc-devel] [lxd/master] shared: Add downloader with sha512 verification
monstermunchkin on Github
lxc-bot at linuxcontainers.org
Wed Mar 28 20:43:49 UTC 2018
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 624 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180328/23a76094/attachment.bin>
-------------- next part --------------
From 334b6d90cd848416b6daf44eb0daa8e2850897a2 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at 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.hipp at 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)
}
More information about the lxc-devel
mailing list