Skip to content

Commit

Permalink
Make chosen an array of crypto.Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Oct 25, 2024
1 parent e60572b commit 1074d64
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 21 deletions.
10 changes: 2 additions & 8 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ func bestHashes(checksums []*Checksum) (best []*Checksum) {
return best
}

func isChosen(h crypto.Hash) bool {
return slices.ContainsFunc(chosen, func(c *Checksum) bool {
return c.hash == h
})
}

func parseLine(line string, zeroTerminated bool) (*Checksums, error) {
var algorithm, file, digest string
if match := regex.bsd.FindStringSubmatch(line); match != nil {
Expand All @@ -73,12 +67,12 @@ func parseLine(line string, zeroTerminated bool) (*Checksums, error) {
/* Guess algorithm if not specified */
if algorithm == "" {
if len(chosen) == 1 {
algorithm = algorithms[chosen[0].hash].name
algorithm = algorithms[chosen[0]].name
} else {
algorithm = size2hash[len(sum)]
}
}
if hash, ok := name2Hash[algorithm]; !ok || err != nil || len(chosen) > 0 && !isChosen(hash) {
if hash, ok := name2Hash[algorithm]; !ok || err != nil || len(chosen) > 0 && !slices.Contains(chosen, hash) {
return nil, fmt.Errorf("invalid digest")
} else {
return &Checksums{
Expand Down
6 changes: 0 additions & 6 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ func Test_bestHashes(t *testing.T) {
}
}

func Test_isChosen(t *testing.T) {
if !isChosen(crypto.SHA256) {
t.Errorf("ischosen(%v) = false; want true", crypto.SHA256)
}
}

func Test_parseLine(t *testing.T) {
xwant := map[string]*Checksums{
"44301b466258398bfee1c974a4a40831 /etc/passwd": {
Expand Down
6 changes: 3 additions & 3 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
func hashF(f io.ReadCloser, checksums []*Checksum) []*Checksum {
if checksums == nil {
checksums = make([]*Checksum, len(chosen))
for i := range chosen {
checksums[i] = &Checksum{hash: chosen[i].hash}
for i, h := range chosen {
checksums[i] = &Checksum{hash: h}
}
}

Expand Down Expand Up @@ -101,7 +101,7 @@ func hashStdin() *Checksums {
func hashString(str string) *Checksums {
checksums := make([]*Checksum, len(chosen))
for i, h := range chosen {
checksums[i] = &Checksum{hash: h.hash}
checksums[i] = &Checksum{hash: h}
initHash(checksums[i])
checksums[i].Write([]byte(str))
checksums[i].sum = checksums[i].Sum(nil)
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,17 @@ func init() {
for _, h := range hashes {
available := h == BLAKE3 || h.Available()
if available && algorithms[h].check {
chosen = append(chosen, &Checksum{hash: h})
chosen = append(chosen, h)
}
name2Hash[algorithms[h].name] = h
}

if opts.check == "\x00" && len(chosen) == 0 {
// SHA-256 is default
chosen = append(chosen, &Checksum{hash: crypto.SHA256})
chosen = append(chosen, crypto.SHA256)
}
} else {
chosen = []*Checksum{{hash: hashes[0]}}
chosen = []crypto.Hash{hashes[0]}
name2Hash[algorithms[hashes[0]].name] = hashes[0]
}

Expand Down
2 changes: 1 addition & 1 deletion typesvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var hashes = []crypto.Hash{

var (
algorithms map[crypto.Hash]*Algorithm
chosen []*Checksum
chosen []crypto.Hash
format = template.New("format")
fsys fstest.MapFS
macKey []byte
Expand Down

0 comments on commit 1074d64

Please sign in to comment.