Skip to content

Commit

Permalink
add convenience method to compute checksum (reduce code)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Aizman <[email protected]>
  • Loading branch information
alex-aizman committed Jan 7, 2025
1 parent adba800 commit d7ca46b
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
# NOTE: See `lint-update-ci` target in Makefile.
version: v1.63.2
version: v1.63.4
args: --timeout=30m

- name: Lint
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ lint-update:
## See also: .github/workflows/lint.yml
lint-update-ci:
@rm -f $(GOPATH)/bin/golangci-lint
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.63.2
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.63.4

lint:
@([[ -x "$(command -v golangci-lint)" ]] && echo "Cannot find golangci-lint, run 'make lint-update' to install" && exit 1) || true
Expand Down
7 changes: 3 additions & 4 deletions ais/prxauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,14 @@ func (p *proxy) validateSecret(w http.ResponseWriter, r *http.Request) {
if _, err := p.parseURL(w, r, apc.URLPathTokens.L, 0, false); err != nil {
return
}
cksum := cos.NewCksumHash(cos.ChecksumSHA256)
cksum.H.Write([]byte(p.authn.secret))
cksum.Finalize()

cluConf := &authn.ServerConf{}
if err := cmn.ReadJSON(w, r, cluConf); err != nil {
return
}
if cksum.Val() != cluConf.Secret {

cksumVal := cos.ChecksumB2S(cos.UnsafeB(p.authn.secret), cos.ChecksumSHA256)
if cksumVal != cluConf.Secret {
p.writeErrf(w, r, "%s: invalid secret sha256(%q)", p, cos.SHead(cluConf.Secret))
}
}
Expand Down
6 changes: 2 additions & 4 deletions ais/test/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package integration_test

import (
"bytes"
"encoding/hex"
"fmt"
"io"
"math/rand/v2"
Expand Down Expand Up @@ -1605,9 +1604,8 @@ func TestPutObjectWithChecksum(t *testing.T) {
continue
}
fileName := basefileName + cksumType
hasher := cos.NewCksumHash(cksumType)
hasher.H.Write(objData)
cksumValue := hex.EncodeToString(hasher.H.Sum(nil))
cksumValue := cos.ChecksumB2S(objData, cksumType)

putArgs.Cksum = cos.NewCksum(cksumType, badCksumVal)
putArgs.ObjName = fileName

Expand Down
8 changes: 3 additions & 5 deletions ais/tgts3mpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,9 @@ func (t *target) completeMpt(w http.ResponseWriter, r *http.Request, items []str
if etag == "" {
debug.Assert(!remote)
debug.Assert(concatMD5 != "")
resMD5 := cos.NewCksumHash(cos.ChecksumMD5)
_, err = resMD5.H.Write([]byte(concatMD5))
debug.AssertNoErr(err)
resMD5.Finalize()
etag = `"` + resMD5.Value() + cmn.AwsMultipartDelim + strconv.Itoa(len(partList.Parts)) + `"`

resMD5Val := cos.ChecksumB2S(cos.UnsafeB(concatMD5), cos.ChecksumMD5)
etag = `"` + resMD5Val + cmn.AwsMultipartDelim + strconv.Itoa(len(partList.Parts)) + `"`
}

// .5 finalize
Expand Down
11 changes: 4 additions & 7 deletions cmd/authn/aisreq.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,17 @@ const (
func (m *mgr) validateSecret(clu *authn.CluACL) (err error) {
const tag = "validate-secret"
var (
secret = Conf.Secret()
cksum = cos.NewCksumHash(cos.ChecksumSHA256)
secret = Conf.Secret()
cksumVal = cos.ChecksumB2S(cos.UnsafeB(secret), cos.ChecksumSHA256)
body = cos.MustMarshal(&authn.ServerConf{Secret: cksumVal})
)
cksum.H.Write([]byte(secret))
cksum.Finalize()

body := cos.MustMarshal(&authn.ServerConf{Secret: cksum.Val()})
for _, u := range clu.URLs {
if err = m.call(http.MethodPost, u, apc.Tokens, body, tag); err == nil {
return
}
err = fmt.Errorf("failed to %s with %s: %v", tag, clu, err)
}
return
return err
}

// update list of revoked token on all clusters
Expand Down
10 changes: 9 additions & 1 deletion cmn/cos/cksum.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Package cos provides common low-level types and utilities for all aistore projects
/*
* Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
* Copyright (c) 2018-2025, NVIDIA CORPORATION. All rights reserved.
*/
package cos

Expand Down Expand Up @@ -90,6 +90,14 @@ var NoneCksum = NewCksum(ChecksumNone, "")
// CksumHash //
///////////////

// convenience method (compare with xxhash.Checksum64S)
func ChecksumB2S(in []byte, ty string) string {
cksum := NewCksumHash(ty)
cksum.H.Write(in)
cksum.Finalize()
return cksum.Val()
}

func NewCksumHash(ty string) (ck *CksumHash) {
ck = &CksumHash{}
ck.Init(ty)
Expand Down

0 comments on commit d7ca46b

Please sign in to comment.