parent
30c963a5f0
commit
61a1c93f0f
89 changed files with 15859 additions and 2 deletions
17
server/libs/encrypt/encrypter_hmac.go
Normal file
17
server/libs/encrypt/encrypter_hmac.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package encrypt
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
type HmacEncryptor struct {
|
||||
Key []byte
|
||||
}
|
||||
|
||||
func (encryptor *HmacEncryptor) Encrypt(content []byte) string {
|
||||
mac := hmac.New(sha256.New, encryptor.Key)
|
||||
mac.Write(content)
|
||||
return hex.EncodeToString(mac.Sum(nil))
|
||||
}
|
5
server/libs/encrypt/encryptor.go
Normal file
5
server/libs/encrypt/encryptor.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package encrypt
|
||||
|
||||
type Encryptor interface {
|
||||
Encrypt(content []byte) string
|
||||
}
|
53
server/libs/encrypt/jwt.go
Normal file
53
server/libs/encrypt/jwt.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package encrypt
|
||||
|
||||
import (
|
||||
"github.com/robbert229/jwt"
|
||||
)
|
||||
|
||||
func JwtEncrypterMaker(secret string) TokenEncrypter {
|
||||
return &JwtEncrypter{
|
||||
alg: jwt.HmacSha256(secret),
|
||||
claims: jwt.NewClaim(),
|
||||
}
|
||||
}
|
||||
|
||||
type JwtEncrypter struct {
|
||||
alg jwt.Algorithm
|
||||
claims *jwt.Claims
|
||||
}
|
||||
|
||||
func (encrypter *JwtEncrypter) Add(key string, value string) bool {
|
||||
encrypter.claims.Set(key, value)
|
||||
return true
|
||||
}
|
||||
|
||||
func (encrypter *JwtEncrypter) FromStr(token string) bool {
|
||||
claims, err := encrypter.alg.Decode(token)
|
||||
// TODO: should return error or error info will lost
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
encrypter.claims = claims
|
||||
return true
|
||||
}
|
||||
|
||||
func (encrypter *JwtEncrypter) Get(key string) (string, bool) {
|
||||
iValue, err := encrypter.claims.Get(key)
|
||||
// TODO: should return error or error info will lost
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
return iValue.(string), true
|
||||
}
|
||||
|
||||
func (encrypter *JwtEncrypter) ToStr() (string, bool) {
|
||||
token, err := encrypter.alg.Encode(encrypter.claims)
|
||||
|
||||
// TODO: should return error or error info will lost
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return token, true
|
||||
}
|
11
server/libs/encrypt/token_encrypter.go
Normal file
11
server/libs/encrypt/token_encrypter.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package encrypt
|
||||
|
||||
type EncrypterMaker func(string) TokenEncrypter
|
||||
|
||||
// TODO: name should be Encrypter?
|
||||
type TokenEncrypter interface {
|
||||
Add(string, string) bool
|
||||
FromStr(string) bool
|
||||
Get(string) (string, bool)
|
||||
ToStr() (string, bool)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue