40 lines
960 B
Go
40 lines
960 B
Go
|
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
|
|||
|
}
|