!1 Merge back to master

Merge pull request !1 from dev branch
This commit is contained in:
hekk 2018-05-27 21:32:55 +08:00
parent 30c963a5f0
commit 61a1c93f0f
89 changed files with 15859 additions and 2 deletions

View 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))
}

View file

@ -0,0 +1,5 @@
package encrypt
type Encryptor interface {
Encrypt(content []byte) string
}

View 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
}

View 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)
}