You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package encrypt
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/md5"
|
|
"crypto/sha1"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"github.com/alexandrevicenzi/unchained"
|
|
"github.com/rs/xid"
|
|
"time"
|
|
)
|
|
|
|
// UserPasswd 员工密码
|
|
func UserPasswd(passwd string) (pwd string) {
|
|
pwd, _ = unchained.MakePassword(
|
|
passwd,
|
|
unchained.GetRandomString(unchained.DefaultSaltSize),
|
|
unchained.PBKDF2SHA256Hasher,
|
|
)
|
|
return
|
|
}
|
|
|
|
// CheckUserPasswd 验证员工密码
|
|
func CheckUserPasswd(passwd, encode string) (bool, error) {
|
|
return unchained.CheckPassword(passwd, encode)
|
|
}
|
|
|
|
// ProjectSecret 项目私钥生成方法
|
|
func ProjectSecret(projectId int64) string {
|
|
var buf = bytes.NewBufferString(fmt.Sprintf("secret:%d%d%s", projectId, time.Now().Unix(), xid.New().String()))
|
|
hash := md5.New()
|
|
hash.Write(buf.Bytes())
|
|
return hex.EncodeToString(hash.Sum(nil))
|
|
}
|
|
|
|
// ProjectToken 项目token生成方法
|
|
func ProjectToken() string {
|
|
var buf = bytes.NewBufferString(fmt.Sprintf("token:%d%s", time.Now().Unix(), xid.New().String()))
|
|
hash := sha1.New()
|
|
hash.Write(buf.Bytes())
|
|
return hex.EncodeToString(hash.Sum(nil))
|
|
}
|
|
|
|
// ProjectEncryptStr 项目encrypt_str生成方法
|
|
func ProjectEncryptStr(hashId string) string {
|
|
var buf = bytes.NewBufferString(fmt.Sprintf("encrypt_str:%d%s", time.Now().Unix(), hashId))
|
|
hash := sha1.New()
|
|
hash.Write(buf.Bytes())
|
|
return hex.EncodeToString(hash.Sum(nil))
|
|
}
|
|
|
|
// ProjectThirdSecret 项目third_secret生成方法
|
|
func ProjectThirdSecret(hashId string) string {
|
|
var buf = bytes.NewBufferString(fmt.Sprintf("third_secret:%d%s", time.Now().Unix(), hashId))
|
|
hash := sha1.New()
|
|
hash.Write(buf.Bytes())
|
|
return hex.EncodeToString(hash.Sum(nil))
|
|
}
|