gosora/install/utils.go

38 lines
952 B
Go
Raw Normal View History

2017-07-12 11:05:18 +00:00
package main
import "encoding/base64"
import "crypto/rand"
import "golang.org/x/crypto/bcrypt"
// Generate a cryptographically secure set of random bytes..
func GenerateSafeString(length int) (string, error) {
rb := make([]byte,length)
_, err := rand.Read(rb)
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(rb), nil
}
func BcryptGeneratePassword(password string) (hashed_password string, salt string, err error) {
salt, err = GenerateSafeString(saltLength)
if err != nil {
return "", "", err
}
password = password + salt
hashed_password, err = BcryptGeneratePasswordNoSalt(password)
if err != nil {
return "", "", err
}
return hashed_password, salt, nil
}
func BcryptGeneratePasswordNoSalt(password string) (hash string, err error) {
hashed_password, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(hashed_password), nil
}