app-container: introduce NewHashSHA256

Simple helper to generate a Hash type from a byte slice.
This commit is contained in:
Brandon Philips
2014-11-28 12:58:42 -05:00
parent 10011c6c23
commit 44912ffdb3
4 changed files with 26 additions and 9 deletions
+8
View File
@@ -1,6 +1,7 @@
package types
import (
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
@@ -69,3 +70,10 @@ func (h Hash) MarshalJSON() ([]byte, error) {
}
return json.Marshal(h.String())
}
func NewHashSHA256(b []byte) *Hash {
h := sha256.New()
h.Write(b)
nh, _ := NewHash(fmt.Sprintf("sha256-%x", h.Sum(nil)))
return nh
}
+3 -1
View File
@@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"
"net/http"
"github.com/coreos-inc/rkt/app-container/schema/types"
)
func NewRemote(name string, mirrors []string) *Remote {
@@ -34,7 +36,7 @@ func (r *Remote) Unmarshal(data []byte) {
}
func (r Remote) Hash() string {
return sha256sum(r.Name)
return types.NewHashSHA256([]byte(r.Name)).String()
}
func (r Remote) Type() int64 {
-8
View File
@@ -3,9 +3,7 @@ package cas
import (
"compress/bzip2"
"compress/gzip"
"crypto/sha256"
"errors"
"fmt"
"io"
"net/url"
"strings"
@@ -24,12 +22,6 @@ func blockTransform(s string) []string {
return pathSlice
}
func sha256sum(s string) string {
h := sha256.New()
io.WriteString(h, s)
return fmt.Sprintf("sha256-%x", h.Sum(nil))
}
func parseAlways(s string) *url.URL {
u, _ := url.Parse(s)
return u
+15
View File
@@ -43,6 +43,21 @@ func findImages(args []string, ds *cas.Store) (out []string, err error) {
if err == nil {
continue
}
// import the local file if it exists
file, err := os.Open(img)
if err == nil {
hash := types.NewHashSHA256([]byte(img)).String()
key, err := ds.WriteACI(hash, file)
file.Close()
if err != nil {
return nil, fmt.Errorf("%s: %v", img, err)
}
out[i] = key
continue
}
// download if it is a URL
u, err := url.Parse(img)
if err != nil {
return nil, fmt.Errorf("%s: not a valid URL or hash", img)