注册用户后,我尝试使用相同的密码登录,但由于错误无法登录。我通过加密密码和普通密码来比较功能。但我无法登录。
type request struct {
PhoneNumber string `json:"phone_number"`
Password string `json:"password"`
}
var body request
if err := c.BodyParser(&body); err != nil {
return c.Status(fiber.StatusUnprocessableEntity).SendString("Wrong credentials.")
}
var user model.User
err := model.DB.Where(model.User{PhoneNumber: body.PhoneNumber}).First(&user)
if err.Error != nil {
return c.Status(fiber.StatusUnprocessableEntity).SendString("Wrong credentials.")
}
matched := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(body.Password))
//matched := CheckPasswordHash(body.Password, user.Password)
fmt.Println(bcrypt.CompareHashAndPassword([]byte("$2a$14$AphYClfW8V1HZh4.6kSD1OpkTzvS9dTc/1qgKesbCUW5.9BMDmzMW"), []byte("123456")))
//check := CheckPasswordHash(body.Password, user.Password)
fmt.Println(matched)
fmt.Println("Hash pass: "+user.Password)
fmt.Println("Raw pass: "+body.Password)
fmt.Println(matched)
if matched != nil {
return c.Status(fiber.StatusUnprocessableEntity).SendString("Wrong credentials2.")
}
我还打印了我的哈希和普通密码以进行检查。但我都通过了。这是我的控制台打印:
crypto/bcrypt: hashedPassword is not the hash of the given password
crypto/bcrypt: hashedPassword is not the hash of the given password
Hash pass: $2a$14$AphYClfW8V1HZh4.6kSD1OpkTzvS9dTc/1qgKesbCUW5.9BMDmzMW
Raw pass: 123456
!exclude .idea
请帮助我,或者如果您有其他方法来加密密码并将其与普通密码进行比较,请告诉我。