Skip to content

Commit ee6f4c9

Browse files
perf: use hex.EncodeToString instead of fmt.Sprintf("%x", ...) (#3132)
Signed-off-by: Matías Insaurralde <matias@chainloop.dev>
1 parent c4ce682 commit ee6f4c9

8 files changed

Lines changed: 26 additions & 13 deletions

File tree

app/artifact-cas/internal/service/bytestream.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/sha256"
2222
"encoding/base64"
2323
"encoding/gob"
24+
"encoding/hex"
2425
"fmt"
2526
"hash"
2627
"io"
@@ -326,5 +327,5 @@ func (sw *streamWriter) Write(data []byte) (int, error) {
326327

327328
// GetChecksum retrieves the sha256 checksum of the read contents
328329
func (sw *streamWriter) GetChecksum() string {
329-
return fmt.Sprintf("%x", sw.gotChecksum.Sum(nil))
330+
return hex.EncodeToString(sw.gotChecksum.Sum(nil))
330331
}

app/artifact-cas/internal/service/download.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package service
1818
import (
1919
"bytes"
2020
"crypto/sha256"
21+
"encoding/hex"
2122
"fmt"
2223
"io"
2324
"net/http"
@@ -125,7 +126,7 @@ func (s *DownloadService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
125126
}
126127

127128
// Verify the checksum
128-
if got, want := fmt.Sprintf("%x", gotChecksum.Sum(nil)), wantChecksum.Hex; got != want {
129+
if got, want := hex.EncodeToString(gotChecksum.Sum(nil)), wantChecksum.Hex; got != want {
129130
msg := fmt.Sprintf("checksums mismatch: got: %s, want: %s", got, want)
130131
s.log.Info(msg)
131132
http.Error(w, msg, http.StatusUnauthorized)

app/cli/cmd/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cmd
1818
import (
1919
"context"
2020
"crypto/sha256"
21+
"encoding/hex"
2122
"errors"
2223
"fmt"
2324
"os"
@@ -490,7 +491,8 @@ func extractCmdLineFromCommand(cmd *cobra.Command) string {
490491
// hashControlPlaneURL returns a hash of the control plane URL
491492
func hashControlPlaneURL() (url string, hash string) {
492493
url = viper.GetString(confOptions.controlplaneAPI.viperKey)
493-
return url, fmt.Sprintf("%x", sha256.Sum256([]byte(url)))
494+
sum := sha256.Sum256([]byte(url))
495+
return url, hex.EncodeToString(sum[:])
494496
}
495497

496498
func apiInsecure() bool {

app/cli/cmd/version.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2023 The Chainloop Authors.
2+
// Copyright 2023-2026 The Chainloop Authors.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package cmd
1818
import (
1919
"context"
2020
"crypto/sha256"
21+
"encoding/hex"
2122
"fmt"
2223
"hash"
2324
"io"
@@ -88,7 +89,7 @@ func executableInfo() (*info, error) {
8889

8990
return &info{
9091
Version: Version,
91-
Digest: fmt.Sprintf("sha256:%x", h.Sum(nil)),
92+
Digest: "sha256:" + hex.EncodeToString(h.Sum(nil)),
9293
}, nil
9394
}
9495

app/cli/pkg/action/artifact_download.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2023-2025 The Chainloop Authors.
2+
// Copyright 2023-2026 The Chainloop Authors.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package action
1818
import (
1919
"context"
2020
"crypto/sha256"
21+
"encoding/hex"
2122
"errors"
2223
"fmt"
2324
"io"
@@ -106,7 +107,7 @@ func (a *ArtifactDownload) Run(downloadPath, outputFile, digest string) error {
106107
return errors.New("problem downloading file")
107108
}
108109

109-
if got, want := fmt.Sprintf("%x", hash.Sum(nil)), h.Hex; got != want {
110+
if got, want := hex.EncodeToString(hash.Sum(nil)), h.Hex; got != want {
110111
return fmt.Errorf("checksums mismatch: got: %s, expected: %s", got, want)
111112
}
112113

app/cli/pkg/action/attestation_push.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"bytes"
2020
"context"
2121
"crypto/sha256"
22+
"encoding/hex"
2223
"encoding/json"
2324
"fmt"
2425
"os"
@@ -348,8 +349,9 @@ func uploadPolicyEvaluationsBundle(ctx context.Context, evaluations []*v1.Policy
348349
return nil, fmt.Errorf("marshaling policy evaluation bundle: %w", err)
349350
}
350351

351-
hexDigest := fmt.Sprintf("%x", sha256.Sum256(data))
352-
digest := fmt.Sprintf("sha256:%s", hexDigest)
352+
sum := sha256.Sum256(data)
353+
hexDigest := hex.EncodeToString(sum[:])
354+
digest := "sha256:" + hexDigest
353355

354356
if _, err := uploader.Upload(ctx, bytes.NewReader(data), "policy-evaluations.json", digest); err != nil {
355357
return nil, fmt.Errorf("uploading policy evaluation bundle: %w", err)

app/controlplane/pkg/biz/signing.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"crypto/sha256"
2323
"crypto/x509"
2424
"crypto/x509/pkix"
25+
"encoding/hex"
2526
"errors"
2627
"fmt"
2728
"net/url"
@@ -229,7 +230,8 @@ func (s *SigningUseCase) GetTrustedRoot(ctx context.Context) (*TrustedRoot, erro
229230
if len(chain) == 0 {
230231
continue
231232
}
232-
keyID := fmt.Sprintf("%x", sha256.Sum256(chain[0].SubjectKeyId))
233+
keyIDSum := sha256.Sum256(chain[0].SubjectKeyId)
234+
keyID := hex.EncodeToString(keyIDSum[:])
233235
for _, cert := range chain {
234236
pemCert, err := cryptoutils.MarshalCertificateToPEM(cert)
235237
if err != nil {
@@ -244,7 +246,8 @@ func (s *SigningUseCase) GetTrustedRoot(ctx context.Context) (*TrustedRoot, erro
244246
if len(authority.CertChain) == 0 {
245247
continue
246248
}
247-
authorityKeyID := fmt.Sprintf("%x", sha256.Sum256(authority.CertChain[0].SubjectKeyId))
249+
authorityKeyIDSum := sha256.Sum256(authority.CertChain[0].SubjectKeyId)
250+
authorityKeyID := hex.EncodeToString(authorityKeyIDSum[:])
248251
for _, cert := range authority.CertChain {
249252
pemCert, err := cryptoutils.MarshalCertificateToPEM(cert)
250253
if err != nil {

pkg/attestation/verifier/verifier.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2025 The Chainloop Authors.
2+
// Copyright 2025-2026 The Chainloop Authors.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"crypto/sha256"
2121
"crypto/x509"
22+
"encoding/hex"
2223
"errors"
2324
"fmt"
2425

@@ -67,7 +68,8 @@ func VerifyBundle(ctx context.Context, bundleBytes []byte, tr *TrustedRoot) erro
6768
hasVerificationMaterial = true
6869
signingCert := vc.Certificate()
6970

70-
aki := fmt.Sprintf("%x", sha256.Sum256(signingCert.AuthorityKeyId))
71+
akiSum := sha256.Sum256(signingCert.AuthorityKeyId)
72+
aki := hex.EncodeToString(akiSum[:])
7173
chain, ok := tr.Keys[aki]
7274
if !ok {
7375
return fmt.Errorf("trusted root not found for signing key with AKI %s", aki)

0 commit comments

Comments
 (0)