first commit
This commit is contained in:
29
services/auth/password.go
Normal file
29
services/auth/password.go
Normal file
@@ -0,0 +1,29 @@
|
||||
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[:])
|
||||
}
|
||||
Reference in New Issue
Block a user