I encrypt the user password and save the db. Then, to log in, compare the hashed password and a simple password, I get a crypto/bcrypt: hashedPassword is not the hash of the given password error crypto/bcrypt: hashedPassword is not the hash of the given password . What's wrong?
func encryptPassword(password string) (string, error) { bytePass := []byte(password) hashedPassword, err := bcrypt.GenerateFromPassword(bytePass, bcrypt.DefaultCost) if err != nil { log.Printf("ERROR:EncryptPassword: %s", err.Error()) } return string(hashedPassword), err } func (i *Impl) Register(user User) bool { hashedPass, err := encryptPassword(user.Password) if err != nil { return false } user.Password = hashedPass if err := i.DB.Create(&user).Error; err != nil { log.Printf("ERROR:Register: %s", err.Error()) return false } return true } func (i *Impl) Login(email string, password string) (User, error) { var user User i.DB.Where("email = ?", email).First(&user) err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)) if err != nil { log.Printf("ERROR:Login: %s", err.Error()) return User{}, err } return user, err }
source share