aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorJason Song <i@wolfogre.com>2022-11-28 19:19:18 +0800
committerGitHub <noreply@github.com>2022-11-28 11:19:18 +0000
commit9607750b5e9001ab379fa8deab0dadbb6219c66e (patch)
treedbdd22fbe52114b852d75f7f108342570bd81eeb /models
parente81ccc406bf723a5a58d685e7782f281736affd4 (diff)
downloadgitea-9607750b5e9001ab379fa8deab0dadbb6219c66e.tar.gz
gitea-9607750b5e9001ab379fa8deab0dadbb6219c66e.zip
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.
Diffstat (limited to 'models')
-rw-r--r--models/auth/twofactor.go3
-rw-r--r--models/migrations/base/hash.go4
-rw-r--r--models/migrations/v1_14/v166.go4
-rw-r--r--models/user/user.go2
4 files changed, 7 insertions, 6 deletions
diff --git a/models/auth/twofactor.go b/models/auth/twofactor.go
index 179d315364..5b3a9d011a 100644
--- a/models/auth/twofactor.go
+++ b/models/auth/twofactor.go
@@ -9,6 +9,7 @@ import (
"crypto/subtle"
"encoding/base32"
"encoding/base64"
+ "encoding/hex"
"fmt"
"code.gitea.io/gitea/models/db"
@@ -78,7 +79,7 @@ func (t *TwoFactor) GenerateScratchToken() (string, error) {
// HashToken return the hashable salt
func HashToken(token, salt string) string {
tempHash := pbkdf2.Key([]byte(token), []byte(salt), 10000, 50, sha256.New)
- return fmt.Sprintf("%x", tempHash)
+ return hex.EncodeToString(tempHash)
}
// VerifyScratchToken verifies if the specified scratch token is valid.
diff --git a/models/migrations/base/hash.go b/models/migrations/base/hash.go
index 164f826b45..00fd1efd4a 100644
--- a/models/migrations/base/hash.go
+++ b/models/migrations/base/hash.go
@@ -5,12 +5,12 @@ package base
import (
"crypto/sha256"
- "fmt"
+ "encoding/hex"
"golang.org/x/crypto/pbkdf2"
)
func HashToken(token, salt string) string {
tempHash := pbkdf2.Key([]byte(token), []byte(salt), 10000, 50, sha256.New)
- return fmt.Sprintf("%x", tempHash)
+ return hex.EncodeToString(tempHash)
}
diff --git a/models/migrations/v1_14/v166.go b/models/migrations/v1_14/v166.go
index 1eb7263347..f797930d6d 100644
--- a/models/migrations/v1_14/v166.go
+++ b/models/migrations/v1_14/v166.go
@@ -5,7 +5,7 @@ package v1_14 //nolint
import (
"crypto/sha256"
- "fmt"
+ "encoding/hex"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/bcrypt"
@@ -53,7 +53,7 @@ func RecalculateUserEmptyPWD(x *xorm.Engine) (err error) {
tempPasswd = pbkdf2.Key([]byte(passwd), []byte(salt), 10000, 50, sha256.New)
}
- return fmt.Sprintf("%x", tempPasswd)
+ return hex.EncodeToString(tempPasswd)
}
// ValidatePassword checks if given password matches the one belongs to the user.
diff --git a/models/user/user.go b/models/user/user.go
index 7e3ae388fb..915e4243cf 100644
--- a/models/user/user.go
+++ b/models/user/user.go
@@ -401,7 +401,7 @@ func hashPassword(passwd, salt, algo string) (string, error) {
tempPasswd = pbkdf2.Key([]byte(passwd), saltBytes, 10000, 50, sha256.New)
}
- return fmt.Sprintf("%x", tempPasswd), nil
+ return hex.EncodeToString(tempPasswd), nil
}
// SetPassword hashes a password using the algorithm defined in the config value of PASSWORD_HASH_ALGO