mirror of
https://github.com/bjdgyc/anylink.git
synced 2025-08-07 18:51:49 +08:00
更改目录结构
This commit is contained in:
40
server/pkg/utils/password_hash.go
Normal file
40
server/pkg/utils/password_hash.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
mt "math/rand"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func PasswordHash(password string) (string, error) {
|
||||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
return string(bytes), err
|
||||
}
|
||||
|
||||
func PasswordVerify(password, hash string) bool {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// $sha-256$salt-key$hash-abcd
|
||||
// $sha-512$salt-key$hash-abcd
|
||||
const (
|
||||
saltSize = 16
|
||||
delmiter = "$"
|
||||
)
|
||||
|
||||
func RandSecret(min int, max int) (string, error) {
|
||||
rb := make([]byte, randInt(min, max))
|
||||
_, err := rand.Read(rb)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return base64.URLEncoding.EncodeToString(rb), nil
|
||||
}
|
||||
|
||||
func randInt(min int, max int) int {
|
||||
return min + mt.Intn(max-min)
|
||||
}
|
Reference in New Issue
Block a user