Skip to content

Commit 9607750

Browse files
authored
Replace fmt.Sprintf with hex.EncodeToString (#21960)
`hex.EncodeToString` has better performance than `fmt.Sprintf("%x", []byte)`, we should use it as much as possible. I'm not an extreme fan of performance, so I think there are some exceptions: - `fmt.Sprintf("%x", func(...)[N]byte())` - We can't slice the function return value directly, and it's not worth adding lines. ```diff func A()[20]byte { ... } - a := fmt.Sprintf("%x", A()) - a := hex.EncodeToString(A()[:]) // invalid + tmp := A() + a := hex.EncodeToString(tmp[:]) ``` - `fmt.Sprintf("%X", []byte)` - `strings.ToUpper(hex.EncodeToString(bytes))` has even worse performance.
1 parent e81ccc4 commit 9607750

File tree

9 files changed

+30
-28
lines changed

9 files changed

+30
-28
lines changed

models/auth/twofactor.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"crypto/subtle"
1010
"encoding/base32"
1111
"encoding/base64"
12+
"encoding/hex"
1213
"fmt"
1314

1415
"code.gitea.io/gitea/models/db"
@@ -78,7 +79,7 @@ func (t *TwoFactor) GenerateScratchToken() (string, error) {
7879
// HashToken return the hashable salt
7980
func HashToken(token, salt string) string {
8081
tempHash := pbkdf2.Key([]byte(token), []byte(salt), 10000, 50, sha256.New)
81-
return fmt.Sprintf("%x", tempHash)
82+
return hex.EncodeToString(tempHash)
8283
}
8384

8485
// VerifyScratchToken verifies if the specified scratch token is valid.

models/migrations/base/hash.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ package base
55

66
import (
77
"crypto/sha256"
8-
"fmt"
8+
"encoding/hex"
99

1010
"golang.org/x/crypto/pbkdf2"
1111
)
1212

1313
func HashToken(token, salt string) string {
1414
tempHash := pbkdf2.Key([]byte(token), []byte(salt), 10000, 50, sha256.New)
15-
return fmt.Sprintf("%x", tempHash)
15+
return hex.EncodeToString(tempHash)
1616
}

models/migrations/v1_14/v166.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package v1_14 //nolint
55

66
import (
77
"crypto/sha256"
8-
"fmt"
8+
"encoding/hex"
99

1010
"golang.org/x/crypto/argon2"
1111
"golang.org/x/crypto/bcrypt"
@@ -53,7 +53,7 @@ func RecalculateUserEmptyPWD(x *xorm.Engine) (err error) {
5353
tempPasswd = pbkdf2.Key([]byte(passwd), []byte(salt), 10000, 50, sha256.New)
5454
}
5555

56-
return fmt.Sprintf("%x", tempPasswd)
56+
return hex.EncodeToString(tempPasswd)
5757
}
5858

5959
// ValidatePassword checks if given password matches the one belongs to the user.

models/user/user.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ func hashPassword(passwd, salt, algo string) (string, error) {
401401
tempPasswd = pbkdf2.Key([]byte(passwd), saltBytes, 10000, 50, sha256.New)
402402
}
403403

404-
return fmt.Sprintf("%x", tempPasswd), nil
404+
return hex.EncodeToString(tempPasswd), nil
405405
}
406406

407407
// SetPassword hashes a password using the algorithm defined in the config value of PASSWORD_HASH_ALGO

modules/packages/hashed_buffer_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package packages
55

66
import (
7-
"fmt"
7+
"encoding/hex"
88
"io"
99
"strings"
1010
"testing"
@@ -36,10 +36,10 @@ func TestHashedBuffer(t *testing.T) {
3636
assert.Equal(t, c.Data, string(data))
3737

3838
hashMD5, hashSHA1, hashSHA256, hashSHA512 := buf.Sums()
39-
assert.Equal(t, c.HashMD5, fmt.Sprintf("%x", hashMD5))
40-
assert.Equal(t, c.HashSHA1, fmt.Sprintf("%x", hashSHA1))
41-
assert.Equal(t, c.HashSHA256, fmt.Sprintf("%x", hashSHA256))
42-
assert.Equal(t, c.HashSHA512, fmt.Sprintf("%x", hashSHA512))
39+
assert.Equal(t, c.HashMD5, hex.EncodeToString(hashMD5))
40+
assert.Equal(t, c.HashSHA1, hex.EncodeToString(hashSHA1))
41+
assert.Equal(t, c.HashSHA256, hex.EncodeToString(hashSHA256))
42+
assert.Equal(t, c.HashSHA512, hex.EncodeToString(hashSHA512))
4343

4444
assert.NoError(t, buf.Close())
4545
}

modules/packages/multi_hasher_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package packages
55

66
import (
7-
"fmt"
7+
"encoding/hex"
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
@@ -24,10 +24,10 @@ func TestMultiHasherSums(t *testing.T) {
2424

2525
hashMD5, hashSHA1, hashSHA256, hashSHA512 := h.Sums()
2626

27-
assert.Equal(t, expectedMD5, fmt.Sprintf("%x", hashMD5))
28-
assert.Equal(t, expectedSHA1, fmt.Sprintf("%x", hashSHA1))
29-
assert.Equal(t, expectedSHA256, fmt.Sprintf("%x", hashSHA256))
30-
assert.Equal(t, expectedSHA512, fmt.Sprintf("%x", hashSHA512))
27+
assert.Equal(t, expectedMD5, hex.EncodeToString(hashMD5))
28+
assert.Equal(t, expectedSHA1, hex.EncodeToString(hashSHA1))
29+
assert.Equal(t, expectedSHA256, hex.EncodeToString(hashSHA256))
30+
assert.Equal(t, expectedSHA512, hex.EncodeToString(hashSHA512))
3131
})
3232

3333
t.Run("State", func(t *testing.T) {
@@ -45,9 +45,9 @@ func TestMultiHasherSums(t *testing.T) {
4545

4646
hashMD5, hashSHA1, hashSHA256, hashSHA512 := h2.Sums()
4747

48-
assert.Equal(t, expectedMD5, fmt.Sprintf("%x", hashMD5))
49-
assert.Equal(t, expectedSHA1, fmt.Sprintf("%x", hashSHA1))
50-
assert.Equal(t, expectedSHA256, fmt.Sprintf("%x", hashSHA256))
51-
assert.Equal(t, expectedSHA512, fmt.Sprintf("%x", hashSHA512))
48+
assert.Equal(t, expectedMD5, hex.EncodeToString(hashMD5))
49+
assert.Equal(t, expectedSHA1, hex.EncodeToString(hashSHA1))
50+
assert.Equal(t, expectedSHA256, hex.EncodeToString(hashSHA256))
51+
assert.Equal(t, expectedSHA512, hex.EncodeToString(hashSHA512))
5252
})
5353
}

routers/api/packages/maven/maven.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"crypto/sha1"
99
"crypto/sha256"
1010
"crypto/sha512"
11+
"encoding/hex"
1112
"encoding/xml"
1213
"errors"
13-
"fmt"
1414
"io"
1515
"net/http"
1616
"path/filepath"
@@ -128,7 +128,7 @@ func serveMavenMetadata(ctx *context.Context, params parameters) {
128128
tmp := sha512.Sum512(xmlMetadataWithHeader)
129129
hash = tmp[:]
130130
}
131-
ctx.PlainText(http.StatusOK, fmt.Sprintf("%x", hash))
131+
ctx.PlainText(http.StatusOK, hex.EncodeToString(hash))
132132
return
133133
}
134134

routers/api/packages/pypi/pypi.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package pypi
55

66
import (
7-
"fmt"
7+
"encoding/hex"
88
"io"
99
"net/http"
1010
"regexp"
@@ -118,7 +118,7 @@ func UploadPackageFile(ctx *context.Context) {
118118

119119
_, _, hashSHA256, _ := buf.Sums()
120120

121-
if !strings.EqualFold(ctx.Req.FormValue("sha256_digest"), fmt.Sprintf("%x", hashSHA256)) {
121+
if !strings.EqualFold(ctx.Req.FormValue("sha256_digest"), hex.EncodeToString(hashSHA256)) {
122122
apiError(ctx, http.StatusBadRequest, "hash mismatch")
123123
return
124124
}

services/packages/packages.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package packages
55

66
import (
77
"context"
8+
"encoding/hex"
89
"errors"
910
"fmt"
1011
"io"
@@ -229,10 +230,10 @@ func NewPackageBlob(hsr packages_module.HashedSizeReader) *packages_model.Packag
229230

230231
return &packages_model.PackageBlob{
231232
Size: hsr.Size(),
232-
HashMD5: fmt.Sprintf("%x", hashMD5),
233-
HashSHA1: fmt.Sprintf("%x", hashSHA1),
234-
HashSHA256: fmt.Sprintf("%x", hashSHA256),
235-
HashSHA512: fmt.Sprintf("%x", hashSHA512),
233+
HashMD5: hex.EncodeToString(hashMD5),
234+
HashSHA1: hex.EncodeToString(hashSHA1),
235+
HashSHA256: hex.EncodeToString(hashSHA256),
236+
HashSHA512: hex.EncodeToString(hashSHA512),
236237
}
237238
}
238239

0 commit comments

Comments
 (0)