github.com/blend/go-sdk@v1.20240719.1/crypto/password.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package crypto
     9  
    10  import "golang.org/x/crypto/bcrypt"
    11  
    12  const (
    13  	bcryptHashingCost = 10
    14  )
    15  
    16  // HashPassword uses bcrypt to generate a salted hash for the provided password
    17  func HashPassword(password string) (string, error) {
    18  	bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcryptHashingCost)
    19  	if err != nil {
    20  		return "", err
    21  	}
    22  	return string(bytes), nil
    23  }
    24  
    25  // PasswordMatchesHash checks whether the provided password matches the provided hash
    26  func PasswordMatchesHash(password string, hash string) bool {
    27  	err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
    28  	return err == nil
    29  }