Vestmore_GO/utils/auth/check_customer_password.go
2024-04-11 14:57:47 +08:00

40 lines
960 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package auth
import (
"crypto/md5"
"crypto/sha1"
"fmt"
)
// sha1($customer['salt'] . md5($customer['salt'] . $password))
func HashPassword(salt, password string) string {
saltBytes := []byte(salt)
passwordBytes := []byte(password)
// 计算 md5($customer['salt'] . $password)
md5Hash := md5.Sum(append(saltBytes, passwordBytes...))
// 计算 $customer['salt'] . md5($customer['salt'] . $password)
combined := append(saltBytes, []byte(fmt.Sprintf("%x", md5Hash))...)
// 计算 sha1($customer['salt'] . md5($customer['salt'] . $password))
sha1Hash := sha1.Sum(combined)
return fmt.Sprintf("%x", sha1Hash)
}
func CheckPassword(custPassword, salt, randomPassword string, password string) bool {
if custPassword == HashPassword(salt, password) {
return true
}
// 验证密码规则2
if password == fmt.Sprintf("%x", sha1.Sum([]byte(randomPassword))) {
return true
}
// 如果两个条件都不满足则返回false
return false
}