github.com/lovung/GoCleanArchitecture@v0.0.0-20210302152432-50d91fd29f9f/pkg/hasher/bcrypt.go (about)

     1  package hasher
     2  
     3  import (
     4  	"golang.org/x/crypto/bcrypt"
     5  )
     6  
     7  const bcryptCost = 12
     8  
     9  // HashPassword hash password before saving
    10  func bcryptHashPassword(password string) (string, error) {
    11  	bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
    12  	return string(bytes), err
    13  }
    14  
    15  // CheckPasswordHash verify the password
    16  func bcryptCheckPasswordHash(password, hash string) (bool, error) {
    17  	err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
    18  	return err == nil, err
    19  }