func validateCredentials(attemptedPassword string, actualPasswordHash string) (bool, error) {
if err := bcrypt.CompareHashAndPassword([]byte(actualPasswordHash), []byte(attemptedPassword)); err != nil {
fmt.Printf("validateCredentials error is %v", err)
return false, nil
}
return true, nil
}
我写了一个看起来像这样的测试:
func TestAuth_validateCredentials(t *testing.T) {
var actualPassword = "hello123"
actualPasswordHash, _ := bcrypt.GenerateFromPassword([]byte(actualPassword), bcrypt.MinCost)
if valid, err := validateCredentials("username", string(actualPasswordHash)); err != nil {
t.Error("Unexpted error: ", err)
} else if valid {
t.Error("Expected invalid when password is wrong")
}
}
我一直收到这个错误:
validateCredentials错误是crypto / bcrypt:hashedPassword不是 给定passwordPASS的哈希值
我在这里做错了什么?
答案 0 :(得分:1)
您正在传递"username"
而不是actualPassword
if valid, err := validateCredentials(actualPassword, string(actualPasswordHash))