Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sha512): add support for sha512 digest #1988

Closed
wants to merge 1 commit into from
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 @@
import (
"bytes"
"crypto/sha256"
"crypto/sha512"
"encoding/json"
"errors"
"fmt"
"hash"
"io"
"path"
"path/filepath"
Expand Down Expand Up @@ -933,7 +935,14 @@

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()
}

Check warning on line 944 in pkg/storage/imagestore/imagestore.go

View check run for this annotation

Codecov / codecov/patch

pkg/storage/imagestore/imagestore.go#L943-L944

Added lines #L943 - L944 were not covered by tests

buf := new(bytes.Buffer)

_, err = buf.ReadFrom(body)
Expand All @@ -957,7 +966,7 @@
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
Loading