30 lines
587 B
Go
30 lines
587 B
Go
package auth
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func HashPassword(password string) (string, error) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(hash), nil
|
|
}
|
|
|
|
func ComparePasswords(hashed string, plain []byte) bool {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hashed), plain)
|
|
return err == nil
|
|
}
|
|
|
|
func HashWithMD5(password string) string {
|
|
salt := "545"
|
|
data := []byte(salt + password + salt)
|
|
hash := md5.Sum(data)
|
|
return hex.EncodeToString(hash[:])
|
|
}
|