github.com/influx6/npkg@v0.8.8/ncrypt/crypt.go (about)

     1  package ncrypt
     2  
     3  import (
     4  	"golang.org/x/crypto/bcrypt"
     5  )
     6  
     7  var (
     8  	// DefaultHashComplexity defines the default complexity to be using for hashing
     9  	// using the dccrypt hashing method.
    10  	DefaultHashComplexity = 10
    11  )
    12  
    13  // BcryptAuthenticate attempts to validate expected value which is already encrypted using
    14  // the bcrypt hash.The expected value is the already hashed value and the provided
    15  // wukk be hashed and compared to validate if its a valid value.
    16  func BcryptAuthenticate(expected, provided []byte) error {
    17  	return bcrypt.CompareHashAndPassword(expected, provided)
    18  }
    19  
    20  // BcryptGenerate returns a value encrypted using the bcrypt hashing algorithmn, it takes
    21  // all provided values to generate final output.
    22  func BcryptGenerate(content []byte, hashComplexity int) ([]byte, error) {
    23  	if hashComplexity <= 0 {
    24  		hashComplexity = DefaultHashComplexity
    25  	}
    26  
    27  	return bcrypt.GenerateFromPassword(content, hashComplexity)
    28  }