add service
All checks were successful
Deploy Smart Search Backend / deploy (push) Successful in 1m47s
All checks were successful
Deploy Smart Search Backend / deploy (push) Successful in 1m47s
This commit is contained in:
@@ -6,11 +6,12 @@ import (
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type Crypto struct {
|
||||
@@ -31,10 +32,19 @@ func (c *Crypto) EmailHash(email string) string {
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
const bcryptCost = 12
|
||||
|
||||
func PasswordHash(password string) string {
|
||||
h := sha512.New()
|
||||
h.Write([]byte(password))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(hash)
|
||||
}
|
||||
|
||||
func PasswordVerify(password, hash string) bool {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func (c *Crypto) getKey() []byte {
|
||||
|
||||
Reference in New Issue
Block a user