github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/plugin/basic/encrypt/encrypt.go (about)

     1  package encrypt
     2  
     3  import "golang.org/x/crypto/bcrypt"
     4  
     5  //Hash implements root.Hash
     6  type Hash struct{}
     7  
     8  //Generate a salted hash for the input string
     9  func (c *Hash) Generate(s string) (string, error) {
    10  	saltedBytes := []byte(s)
    11  	hashedBytes, err := bcrypt.GenerateFromPassword(saltedBytes, bcrypt.DefaultCost)
    12  	if err != nil {
    13  		return "", err
    14  	}
    15  
    16  	hash := string(hashedBytes[:])
    17  	return hash, nil
    18  }
    19  
    20  func (c *Hash) Compare(hash string, s string) error {
    21  	incoming := []byte(s)
    22  	existing := []byte(hash)
    23  	return bcrypt.CompareHashAndPassword(existing, incoming)
    24  }