crypto.go 425 B

1234567891011121314
  1. package common
  2. import "golang.org/x/crypto/bcrypt"
  3. func Password2Hash(password string) (string, error) {
  4. passwordBytes := []byte(password)
  5. hashedPassword, err := bcrypt.GenerateFromPassword(passwordBytes, bcrypt.DefaultCost)
  6. return string(hashedPassword), err
  7. }
  8. func ValidatePasswordAndHash(password string, hash string) bool {
  9. err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
  10. return err == nil
  11. }