optimize use constant time comparision for use password valid check

pull/699/head
alimy 10 months ago
parent 33b174734d
commit 334f2582f6
No known key found for this signature in database
GPG Key ID: 6964A0C5FE15145A

@ -5,6 +5,7 @@
package web package web
import ( import (
"crypto/subtle"
"image" "image"
"math/rand" "math/rand"
"strings" "strings"
@ -88,15 +89,19 @@ func checkPassword(password string) error {
} }
// ValidPassword 检查密码是否一致 // ValidPassword 检查密码是否一致
func validPassword(dbPassword, password, salt string) bool { func validPassword(secret, password, salt string) bool {
return strings.Compare(dbPassword, utils.EncodeMD5(utils.EncodeMD5(password)+salt)) == 0 expected := utils.EncodeMD5(utils.EncodeMD5(password) + salt)
// Check that the secret matches the expected value.
// Use constant time comparison to avoid timing attacks.
return subtle.ConstantTimeCompare([]byte(secret), []byte(expected)) == 1
} }
// encryptPasswordAndSalt 密码加密&生成salt // encryptPasswordAndSalt 密码加密&生成salt
func encryptPasswordAndSalt(password string) (string, string) { func encryptPasswordAndSalt(password string) (string, string) {
salt := uuid.Must(uuid.NewV4()).String()[:8] salt := uuid.Must(uuid.NewV4()).String()[:8]
password = utils.EncodeMD5(utils.EncodeMD5(password) + salt) secret := utils.EncodeMD5(utils.EncodeMD5(password) + salt)
return password, salt return secret, salt
} }
// deleteOssObjects 删除推文的媒体内容, 宽松处理错误(就是不处理), 后续完善 // deleteOssObjects 删除推文的媒体内容, 宽松处理错误(就是不处理), 后续完善

Loading…
Cancel
Save