I use the bcrypt.GenerateFromPassword () method to encrypt, but it is different every time. How should I save the database?

I use the bcrypt.GenerateFromPassword () method to encrypt, but it is different every time. How should I save the database?

package main

import (
    "golang.org/x/crypto/bcrypt"
    "fmt"
)

func main() {
    password := []byte("MyDarkSecret")

    // Hashing the password with the default cost of 10
    hashedPassword, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(hashedPassword))

    // Comparing the password with the hash
    err = bcrypt.CompareHashAndPassword(hashedPassword, password)
    fmt.Println(err) // nil means it is a match
}
Oct.14,2021

because bcrypt encryption uses random salt, the result of each hash of the same string is different.

If you save

, just convert the result into a string and store it. You can update it if you want. Anyway, the encryption results of the same string are all equivalent

.
Menu