Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pkg/storage/imagestore/imagestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package imagestore
import (
"bytes"
"crypto/sha256"
"crypto/sha512"
"encoding/json"
"errors"
"fmt"
"hash"
"io"
"path"
"path/filepath"
Expand Down Expand Up @@ -933,7 +935,14 @@ func (is *ImageStore) FullBlobUpload(repo string, body io.Reader, dstDigest godi

uuid := u.String()
src := is.BlobUploadPath(repo, uuid)
digester := sha256.New()

var digester hash.Hash
if dstDigest.Algorithm() == godigest.SHA256 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to add support for SHA384 too:

else if dstDigest.Algorithm() == godigest.SHA384 {
   digester = sha512.New384()
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not need to reference sha256, sha384, or sha512 in your code. The godigest library does that for you.

digester := dstDigest.Algorithm().Hash() # hash.Hash is returned

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course you should check that the algorithm is available with algDigest.Algorithm().Available() (otherwise it will panic).

Copy link

@ktarplee ktarplee Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main branch of godigest even allows one to register new algorithms if they choose. See this. So having the list of algorithms in Zot as a switch statement is not ideal. Let the library from open containers handle it for you.

digester = sha256.New()
} else if dstDigest.Algorithm() == godigest.SHA512 {
digester = sha512.New()
}

buf := new(bytes.Buffer)

_, err = buf.ReadFrom(body)
Expand All @@ -957,7 +966,7 @@ func (is *ImageStore) FullBlobUpload(repo string, body io.Reader, dstDigest godi
return "", -1, err
}

srcDigest := godigest.NewDigestFromEncoded(godigest.SHA256, fmt.Sprintf("%x", digester.Sum(nil)))
srcDigest := godigest.NewDigestFromEncoded(dstDigest.Algorithm(), fmt.Sprintf("%x", digester.Sum(nil)))
if srcDigest != dstDigest {
is.log.Error().Str("srcDigest", srcDigest.String()).
Str("dstDigest", dstDigest.String()).Msg("actual digest not equal to expected digest")
Expand Down