diff --git a/builder/common/download.go b/builder/common/download.go index 661fe86b2..eaf2f22d5 100644 --- a/builder/common/download.go +++ b/builder/common/download.go @@ -3,6 +3,8 @@ package common import ( "bytes" "crypto/md5" + "crypto/sha1" + "crypto/sha256" "encoding/hex" "errors" "fmt" @@ -53,6 +55,10 @@ func HashForType(t string) hash.Hash { switch t { case "md5": return md5.New() + case "sha1": + return sha1.New() + case "sha256": + return sha256.New() default: return nil } diff --git a/builder/common/download_test.go b/builder/common/download_test.go index 2ba7f6b9a..055272b17 100644 --- a/builder/common/download_test.go +++ b/builder/common/download_test.go @@ -55,6 +55,32 @@ func TestHashForType(t *testing.T) { } } + if h := HashForType("sha1"); h == nil { + t.Fatalf("sha1 hash is nil") + } else { + h.Write([]byte("foo")) + result := h.Sum(nil) + + expected := "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" + actual := hex.EncodeToString(result) + if actual != expected { + t.Fatalf("bad hash: %s", actual) + } + } + + if h := HashForType("sha256"); h == nil { + t.Fatalf("sha256 hash is nil") + } else { + h.Write([]byte("foo")) + result := h.Sum(nil) + + expected := "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae" + actual := hex.EncodeToString(result) + if actual != expected { + t.Fatalf("bad hash: %s", actual) + } + } + if HashForType("fake") != nil { t.Fatalf("fake hash is not nil") }