Skip to content

Commit

Permalink
Update Checksum method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake committed Dec 13, 2021
1 parent 674c261 commit 438eb27
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package version
import (
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"hash"
Expand Down Expand Up @@ -201,7 +202,8 @@ func (pkg *Package) VerifyChecksum(filename string) (err error) {
if _, err := io.Copy(h, f); err != nil {
return err
}
if pkg.Checksum != fmt.Sprintf("%x", h.Sum(nil)) {

if pkg.Checksum != hex.EncodeToString(h.Sum(nil)) {
return ErrChecksumNotMatched
}
return nil
Expand Down
22 changes: 22 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package version
import (
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -177,3 +178,24 @@ func TestVerifyChecksum(t *testing.T) {
})
})
}

var (
target = "2db6a5d25815b56072465a2cacc8ed426c18f1d5fc26c1fc8c4f5a7188658264"
source = []byte{45, 182, 165, 210, 88, 21, 181, 96, 114, 70, 90, 44, 172, 200, 237, 66, 108, 24, 241, 213, 252, 38, 193, 252, 140, 79, 90, 113, 136, 101, 130, 100}
)

func BenchmarkEqualHexFmt(b *testing.B) {
for i := 0; i < b.N; i++ {
if target == fmt.Sprintf("%x", source) {

}
}
}

func BenchmarkEqualHexEncodeToString(b *testing.B) {
for i := 0; i < b.N; i++ {
if target == hex.EncodeToString(source) {

}
}
}

0 comments on commit 438eb27

Please sign in to comment.