github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/auth/auth.go (about)

     1  package auth
     2  
     3  import "golang.org/x/crypto/bcrypt"
     4  
     5  // Encrypt encrypts the plain text with bcrypt.
     6  func Encrypt(source string) (string, error) {
     7  	hashEdBytes, err := bcrypt.GenerateFromPassword([]byte(source), bcrypt.DefaultCost)
     8  	return string(hashEdBytes), err
     9  }
    10  
    11  // Compare compares the encrypted text with the plain text if it's the same
    12  func Compare(hashedPassword, password string) error {
    13  	return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
    14  }