Skip to content

Commit

Permalink
catch a few more usages
Browse files Browse the repository at this point in the history
  • Loading branch information
howardjohn committed Jan 24, 2023
1 parent bff7801 commit 62bb5a5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
7 changes: 3 additions & 4 deletions pkg/legacy/tarball/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package tarball
import (
"archive/tar"
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -63,8 +61,9 @@ func v1LayerID(layer v1.Layer, parentID string, rawConfig []byte) (string, error
if len(rawConfig) != 0 {
s = fmt.Sprintf("%s %s", s, string(rawConfig))
}
rawDigest := sha256.Sum256([]byte(s))
return hex.EncodeToString(rawDigest[:]), nil

h, _, _ := v1.SHA256(strings.NewReader(s))
return h.Hex, nil
}

// newTopV1Layer creates a new v1Layer for a layer other than the top layer in a v1 image tarball.
Expand Down
16 changes: 6 additions & 10 deletions pkg/registry/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package registry

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -110,9 +108,8 @@ func (m *manifests) handle(resp http.ResponseWriter, req *http.Request) *regErro
Message: "Unknown manifest",
}
}
rd := sha256.Sum256(m.blob)
d := "sha256:" + hex.EncodeToString(rd[:])
resp.Header().Set("Docker-Content-Digest", d)
h, _, _ := v1.SHA256(bytes.NewReader(m.blob))
resp.Header().Set("Docker-Content-Digest", h.String())
resp.Header().Set("Content-Type", m.contentType)
resp.Header().Set("Content-Length", fmt.Sprint(len(m.blob)))
resp.WriteHeader(http.StatusOK)
Expand All @@ -137,9 +134,8 @@ func (m *manifests) handle(resp http.ResponseWriter, req *http.Request) *regErro
Message: "Unknown manifest",
}
}
rd := sha256.Sum256(m.blob)
d := "sha256:" + hex.EncodeToString(rd[:])
resp.Header().Set("Docker-Content-Digest", d)
h, _, _ := v1.SHA256(bytes.NewReader(m.blob))
resp.Header().Set("Docker-Content-Digest", h.String())
resp.Header().Set("Content-Type", m.contentType)
resp.Header().Set("Content-Length", fmt.Sprint(len(m.blob)))
resp.WriteHeader(http.StatusOK)
Expand All @@ -153,8 +149,8 @@ func (m *manifests) handle(resp http.ResponseWriter, req *http.Request) *regErro
}
b := &bytes.Buffer{}
io.Copy(b, req.Body)
rd := sha256.Sum256(b.Bytes())
digest := "sha256:" + hex.EncodeToString(rd[:])
h, _, _ := v1.SHA256(bytes.NewReader(b.Bytes()))
digest := h.String()
mf := manifest{
blob: b.Bytes(),
contentType: req.Header.Get("Content-Type"),
Expand Down
8 changes: 4 additions & 4 deletions pkg/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
package registry_test

import (
"crypto/sha256"
"encoding/hex"
"bytes"
"fmt"
"io/ioutil"
"log"
Expand All @@ -27,6 +26,7 @@ import (
"testing"

"github.com/google/go-containerregistry/pkg/registry"
v1 "github.com/google/go-containerregistry/pkg/v1"
)

const (
Expand All @@ -47,8 +47,8 @@ const (
)

func sha256String(s string) string {
h := sha256.Sum256([]byte(s))
return hex.EncodeToString(h[:])
h, _, _ := v1.SHA256(bytes.NewReader([]byte(s)))
return h.Hex
}

func TestCalls(t *testing.T) {
Expand Down

0 comments on commit 62bb5a5

Please sign in to comment.