mirror of
https://github.com/xinliangnote/go-gin-api.git
synced 2024-04-21 12:31:46 +00:00
47 lines
784 B
Go
47 lines
784 B
Go
package password
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/md5"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
)
|
|
|
|
const (
|
|
saltPassword = "qkhPAGA13HocW3GAEWwb"
|
|
defaultPassword = "123456"
|
|
)
|
|
|
|
func GeneratePassword(str string) (password string) {
|
|
// md5
|
|
m := md5.New()
|
|
m.Write([]byte(str))
|
|
mByte := m.Sum(nil)
|
|
|
|
// hmac
|
|
h := hmac.New(sha256.New, []byte(saltPassword))
|
|
h.Write(mByte)
|
|
password = hex.EncodeToString(h.Sum(nil))
|
|
|
|
return
|
|
}
|
|
|
|
func ResetPassword() (password string) {
|
|
m := md5.New()
|
|
m.Write([]byte(defaultPassword))
|
|
mStr := hex.EncodeToString(m.Sum(nil))
|
|
|
|
password = GeneratePassword(mStr)
|
|
|
|
return
|
|
}
|
|
|
|
func GenerateLoginToken(id int32) (token string) {
|
|
m := md5.New()
|
|
m.Write([]byte(fmt.Sprintf("%d%s", id, saltPassword)))
|
|
token = hex.EncodeToString(m.Sum(nil))
|
|
|
|
return
|
|
}
|