Skip to content

Commit

Permalink
pkg/hash: add FromHashTypeAndDigest function
Browse files Browse the repository at this point in the history
This allows creating a Hash from a hashType and digest.

We don't populate unused h.hash anymore, as ParseNixBase32 now uses
FromHashTypeAndDigest too, and the size checks are done there.
  • Loading branch information
flokli committed Oct 9, 2023
1 parent b0f8b73 commit bb87ee9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
10 changes: 0 additions & 10 deletions pkg/hash/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ func (h *Hash) Multihash() []byte {
func (h *Hash) NixString() string {
digest := h.Digest()

// This can only occur if the struct is filled manually
if h.hash.Size() != len(digest) {
panic("invalid digest length")
}

if hashStr, ok := hashtypeToNixHashString[h.HashType]; ok {
return fmt.Sprintf("%v:%v", hashStr, nixbase32.EncodeToString(digest))
}
Expand All @@ -47,11 +42,6 @@ func (h *Hash) NixString() string {
func (h *Hash) SRIString() string {
digest := h.Digest()

// This can only occur if the struct is filled manually
if h.hash.Size() != len(digest) {
panic("invalid digest length")
}

if hashStr, ok := hashtypeToNixHashString[h.HashType]; ok {
return fmt.Sprintf("%v-%v", hashStr, base64.StdEncoding.EncodeToString(digest))
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/hash/from.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package hash

import (
"fmt"

mh "github.com/multiformats/go-multihash/core"
)

// FromHashTypeAndDigest constructs a Hash from hashType and digest.
// hashType needs to be a supported multihash type,
// and the digest len needs to be correct, otherwise an error is returned.
func FromHashTypeAndDigest(hashType int, digest []byte) (*Hash, error) {
var expectedDigestSize int

switch hashType {
case mh.SHA1:
expectedDigestSize = 20
case mh.SHA2_256:
expectedDigestSize = 32
case mh.SHA2_512:
expectedDigestSize = 64
default:
return nil, fmt.Errorf("unknown hash type: %d", hashType)
}

if len(digest) != expectedDigestSize {
return nil, fmt.Errorf("wrong digest len, expected %d, got %d", expectedDigestSize, len(digest))
}

return &Hash{
HashType: hashType,
hash: nil,
bytesWritten: 0,
digest: digest,
}, nil
}
19 changes: 1 addition & 18 deletions pkg/hash/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,12 @@ func ParseNixBase32(s string) (*Hash, error) {
}

// The digest afterwards is nixbase32-encoded.
// Calculate the length of that string, in nixbase32 encoding
h, err := mh.GetHasher(uint64(hashType))
if err != nil {
return nil, err
}

digestLenBytes := h.Size()
encodedDigestLen := nixbase32.EncodedLen(digestLenBytes)

encodedDigestStr := s[i+1:]
if len(encodedDigestStr) != encodedDigestLen {
return nil, fmt.Errorf("invalid length for encoded digest line %v", s)
}

digest, err := nixbase32.DecodeString(encodedDigestStr)
if err != nil {
return nil, err
}

return &Hash{
HashType: hashType,
// even though the hash function is never written too, we still keep it around, for h.hash.Size() checks etc.
hash: h,
digest: digest,
}, nil
return FromHashTypeAndDigest(hashType, digest)
}

0 comments on commit bb87ee9

Please sign in to comment.