github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/install/utils.go (about)

     1  package install
     2  
     3  import "encoding/base64"
     4  import "crypto/rand"
     5  import "golang.org/x/crypto/bcrypt"
     6  
     7  const saltLength int = 32
     8  
     9  // Generate a cryptographically secure set of random bytes..
    10  func GenerateSafeString(length int) (string, error) {
    11  	rb := make([]byte, length)
    12  	_, err := rand.Read(rb)
    13  	if err != nil {
    14  		return "", err
    15  	}
    16  	return base64.StdEncoding.EncodeToString(rb), nil
    17  }
    18  
    19  // Generate a bcrypt hash
    20  // Note: The salt is in the hash, therefore the salt value is blank
    21  func BcryptGeneratePassword(password string) (hash string, salt string, err error) {
    22  	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
    23  	if err != nil {
    24  		return "", "", err
    25  	}
    26  	return string(hashedPassword), salt, nil
    27  }