Skip to content

Commit d872232

Browse files
authored
hash: use generic instantiation (#1538)
* hash: use generic instantiation This enables doing something like opencontainers/go-digest#71 (comment) to override the hashing method (example: replace with sha256simd). When an importer does not overriden, the library should behave exactly the same. * catch a few more usages
1 parent 1e09daa commit d872232

File tree

8 files changed

+26
-31
lines changed

8 files changed

+26
-31
lines changed

pkg/legacy/tarball/write.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ package tarball
1717
import (
1818
"archive/tar"
1919
"bytes"
20-
"crypto/sha256"
21-
"encoding/hex"
2220
"encoding/json"
2321
"fmt"
2422
"io"
@@ -63,8 +61,9 @@ func v1LayerID(layer v1.Layer, parentID string, rawConfig []byte) (string, error
6361
if len(rawConfig) != 0 {
6462
s = fmt.Sprintf("%s %s", s, string(rawConfig))
6563
}
66-
rawDigest := sha256.Sum256([]byte(s))
67-
return hex.EncodeToString(rawDigest[:]), nil
64+
65+
h, _, _ := v1.SHA256(strings.NewReader(s))
66+
return h.Hex, nil
6867
}
6968

7069
// newTopV1Layer creates a new v1Layer for a layer other than the top layer in a v1 image tarball.

pkg/registry/manifest.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ package registry
1616

1717
import (
1818
"bytes"
19-
"crypto/sha256"
20-
"encoding/hex"
2119
"encoding/json"
2220
"fmt"
2321
"io"
@@ -110,9 +108,8 @@ func (m *manifests) handle(resp http.ResponseWriter, req *http.Request) *regErro
110108
Message: "Unknown manifest",
111109
}
112110
}
113-
rd := sha256.Sum256(m.blob)
114-
d := "sha256:" + hex.EncodeToString(rd[:])
115-
resp.Header().Set("Docker-Content-Digest", d)
111+
h, _, _ := v1.SHA256(bytes.NewReader(m.blob))
112+
resp.Header().Set("Docker-Content-Digest", h.String())
116113
resp.Header().Set("Content-Type", m.contentType)
117114
resp.Header().Set("Content-Length", fmt.Sprint(len(m.blob)))
118115
resp.WriteHeader(http.StatusOK)
@@ -137,9 +134,8 @@ func (m *manifests) handle(resp http.ResponseWriter, req *http.Request) *regErro
137134
Message: "Unknown manifest",
138135
}
139136
}
140-
rd := sha256.Sum256(m.blob)
141-
d := "sha256:" + hex.EncodeToString(rd[:])
142-
resp.Header().Set("Docker-Content-Digest", d)
137+
h, _, _ := v1.SHA256(bytes.NewReader(m.blob))
138+
resp.Header().Set("Docker-Content-Digest", h.String())
143139
resp.Header().Set("Content-Type", m.contentType)
144140
resp.Header().Set("Content-Length", fmt.Sprint(len(m.blob)))
145141
resp.WriteHeader(http.StatusOK)
@@ -153,8 +149,8 @@ func (m *manifests) handle(resp http.ResponseWriter, req *http.Request) *regErro
153149
}
154150
b := &bytes.Buffer{}
155151
io.Copy(b, req.Body)
156-
rd := sha256.Sum256(b.Bytes())
157-
digest := "sha256:" + hex.EncodeToString(rd[:])
152+
h, _, _ := v1.SHA256(bytes.NewReader(b.Bytes()))
153+
digest := h.String()
158154
mf := manifest{
159155
blob: b.Bytes(),
160156
contentType: req.Header.Get("Content-Type"),

pkg/registry/registry_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
package registry_test
1616

1717
import (
18-
"crypto/sha256"
19-
"encoding/hex"
18+
"bytes"
2019
"fmt"
2120
"io"
2221
"log"
@@ -27,6 +26,7 @@ import (
2726
"testing"
2827

2928
"github.com/google/go-containerregistry/pkg/registry"
29+
v1 "github.com/google/go-containerregistry/pkg/v1"
3030
)
3131

3232
const (
@@ -47,8 +47,8 @@ const (
4747
)
4848

4949
func sha256String(s string) string {
50-
h := sha256.Sum256([]byte(s))
51-
return hex.EncodeToString(h[:])
50+
h, _, _ := v1.SHA256(bytes.NewReader([]byte(s)))
51+
return h.Hex
5252
}
5353

5454
func TestCalls(t *testing.T) {

pkg/v1/hash.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package v1
1616

1717
import (
18-
"crypto/sha256"
18+
"crypto"
1919
"encoding/hex"
2020
"encoding/json"
2121
"fmt"
@@ -78,7 +78,7 @@ func (h *Hash) UnmarshalText(text []byte) error {
7878
func Hasher(name string) (hash.Hash, error) {
7979
switch name {
8080
case "sha256":
81-
return sha256.New(), nil
81+
return crypto.SHA256.New(), nil
8282
default:
8383
return nil, fmt.Errorf("unsupported hash: %q", name)
8484
}
@@ -111,7 +111,7 @@ func (h *Hash) parse(unquoted string) error {
111111

112112
// SHA256 computes the Hash of the provided io.Reader's content.
113113
func SHA256(r io.Reader) (Hash, int64, error) {
114-
hasher := sha256.New()
114+
hasher := crypto.SHA256.New()
115115
n, err := io.Copy(hasher, r)
116116
if err != nil {
117117
return Hash{}, 0, err

pkg/v1/random/image.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ package random
1717
import (
1818
"archive/tar"
1919
"bytes"
20+
"crypto"
2021
"crypto/rand"
21-
"crypto/sha256"
2222
"encoding/hex"
2323
"fmt"
2424
"io"
@@ -84,7 +84,7 @@ func Layer(byteSize int64, mt types.MediaType) (v1.Layer, error) {
8484

8585
// Hash the contents as we write it out to the buffer.
8686
var b bytes.Buffer
87-
hasher := sha256.New()
87+
hasher := crypto.SHA256.New()
8888
mw := io.MultiWriter(&b, hasher)
8989

9090
// Write a single file with a random name and random contents.

pkg/v1/remote/write_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package remote
1717
import (
1818
"bytes"
1919
"context"
20-
"crypto/sha256"
20+
"crypto"
2121
"encoding/hex"
2222
"errors"
2323
"fmt"
@@ -659,7 +659,7 @@ func TestStreamLayer(t *testing.T) {
659659
t.Errorf("URL; got %v, want %v", r.URL.Path, expectedPath)
660660
}
661661

662-
h := sha256.New()
662+
h := crypto.SHA256.New()
663663
s, err := io.Copy(h, r.Body)
664664
if err != nil {
665665
t.Errorf("Reading body: %v", err)

pkg/v1/stream/layer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package stream
1818
import (
1919
"bufio"
2020
"compress/gzip"
21-
"crypto/sha256"
21+
"crypto"
2222
"encoding/hex"
2323
"errors"
2424
"hash"
@@ -166,8 +166,8 @@ type compressedReader struct {
166166
func newCompressedReader(l *Layer) (*compressedReader, error) {
167167
// Collect digests of compressed and uncompressed stream and size of
168168
// compressed stream.
169-
h := sha256.New()
170-
zh := sha256.New()
169+
h := crypto.SHA256.New()
170+
zh := crypto.SHA256.New()
171171
count := &countWriter{}
172172

173173
// gzip.Writer writes to the output stream via pipe, a hasher to

pkg/v1/validate/layer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package validate
1717
import (
1818
"archive/tar"
1919
"compress/gzip"
20-
"crypto/sha256"
20+
"crypto"
2121
"encoding/hex"
2222
"errors"
2323
"fmt"
@@ -104,7 +104,7 @@ func computeLayer(layer v1.Layer) (*computedLayer, error) {
104104
}
105105

106106
// Keep track of compressed digest.
107-
digester := sha256.New()
107+
digester := crypto.SHA256.New()
108108
// Everything read from compressed is written to digester to compute digest.
109109
hashCompressed := io.TeeReader(compressed, digester)
110110

@@ -132,7 +132,7 @@ func computeLayer(layer v1.Layer) (*computedLayer, error) {
132132
if err != nil {
133133
return nil, err
134134
}
135-
diffider := sha256.New()
135+
diffider := crypto.SHA256.New()
136136
hashUncompressed := io.TeeReader(uncompressed, diffider)
137137

138138
// Ensure there aren't duplicate file paths.

0 commit comments

Comments
 (0)