fix encipher return errors
This commit is contained in:
parent
8bff8e7377
commit
26048b1b41
|
@ -6,7 +6,6 @@ import (
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -29,7 +28,7 @@ func New() {
|
||||||
|
|
||||||
func GenerateTokenAes(id uint, identity, client, role string, extend map[string]string) (string, error) {
|
func GenerateTokenAes(id uint, identity, client, role string, extend map[string]string) (string, error) {
|
||||||
if (JwtSecretLen == 16 || JwtSecretLen == 24 || JwtSecretLen == 32) == false {
|
if (JwtSecretLen == 16 || JwtSecretLen == 24 || JwtSecretLen == 32) == false {
|
||||||
return "", errors.New("JwtSecret lenght must 16/24/32.")
|
return "", exception.ErrAuthSecret
|
||||||
}
|
}
|
||||||
expireTime := time.Now().Add(vars.JwtExpireDay)
|
expireTime := time.Now().Add(vars.JwtExpireDay)
|
||||||
claims := types.JwtClaims{
|
claims := types.JwtClaims{
|
||||||
|
@ -43,7 +42,7 @@ func GenerateTokenAes(id uint, identity, client, role string, extend map[string]
|
||||||
|
|
||||||
byte, err := json.Marshal(claims)
|
byte, err := json.Marshal(claims)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", exception.ErrJsonEncode
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := AesEncryptCBC(byte)
|
token, err := AesEncryptCBC(byte)
|
||||||
|
@ -57,7 +56,10 @@ func AesEncryptCBC(plan []byte) (string, error) {
|
||||||
|
|
||||||
// 分组秘钥
|
// 分组秘钥
|
||||||
// NewCipher该函数限制了输入k的长度必须为16, 24或者32
|
// NewCipher该函数限制了输入k的长度必须为16, 24或者32
|
||||||
block, _ := aes.NewCipher(JwtSecret)
|
block, err := aes.NewCipher(JwtSecret)
|
||||||
|
if err != nil {
|
||||||
|
return "", exception.ErrAuthSecret
|
||||||
|
}
|
||||||
// 获取秘钥块的长度
|
// 获取秘钥块的长度
|
||||||
blockSize := block.BlockSize()
|
blockSize := block.BlockSize()
|
||||||
// 补全码
|
// 补全码
|
||||||
|
@ -73,17 +75,17 @@ func AesEncryptCBC(plan []byte) (string, error) {
|
||||||
|
|
||||||
func AesDecryptCBC(cryted string) (b []byte, err error) {
|
func AesDecryptCBC(cryted string) (b []byte, err error) {
|
||||||
if (JwtSecretLen == 16 || JwtSecretLen == 24 || JwtSecretLen == 32) == false {
|
if (JwtSecretLen == 16 || JwtSecretLen == 24 || JwtSecretLen == 32) == false {
|
||||||
return b, errors.New("JwtSecret lenght must 16/24/32.")
|
return nil, exception.ErrAuthSecret
|
||||||
}
|
}
|
||||||
// 转成字节数组
|
// 转成字节数组
|
||||||
crytedByte, err := base64.StdEncoding.DecodeString(cryted)
|
crytedByte, err := base64.StdEncoding.DecodeString(cryted)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil, exception.ErrBase64Decode
|
||||||
}
|
}
|
||||||
// 分组秘钥
|
// 分组秘钥
|
||||||
block, err := aes.NewCipher(JwtSecret)
|
block, err := aes.NewCipher(JwtSecret)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil, exception.ErrAuthSecret
|
||||||
}
|
}
|
||||||
// 获取秘钥块的长度
|
// 获取秘钥块的长度
|
||||||
blockSize := block.BlockSize()
|
blockSize := block.BlockSize()
|
||||||
|
|
|
@ -3,7 +3,7 @@ package exception
|
||||||
// jwt custom error code ,begin:200
|
// jwt custom error code ,begin:200
|
||||||
var (
|
var (
|
||||||
ErrAuthSecret = Errorf(200, "Auth JwtSecret Error")
|
ErrAuthSecret = Errorf(200, "Auth JwtSecret Error")
|
||||||
ErrAuthDecode = Errorf(201, "Auth Token Base64 Decode Error")
|
ErrBase64Decode = Errorf(201, "Auth Token Base64 Decode Error")
|
||||||
ErrAuthNotFound = Errorf(202, "Auth Token Not Found")
|
ErrAuthNotFound = Errorf(202, "Auth Token Not Found")
|
||||||
ErrAuthParseFail = Errorf(203, "Auth Parse Fail")
|
ErrAuthParseFail = Errorf(203, "Auth Parse Fail")
|
||||||
ErrAuthId = Errorf(204, "Auth Id Not Passed")
|
ErrAuthId = Errorf(204, "Auth Id Not Passed")
|
||||||
|
@ -11,5 +11,7 @@ var (
|
||||||
ErrAuthTokenChanged = Errorf(206, "Auth Token Changed")
|
ErrAuthTokenChanged = Errorf(206, "Auth Token Changed")
|
||||||
ErrAuthIdType = Errorf(207, "Auth Id Type Error")
|
ErrAuthIdType = Errorf(207, "Auth Id Type Error")
|
||||||
ErrAuthExpire = Errorf(208, "Auth Token Expire")
|
ErrAuthExpire = Errorf(208, "Auth Token Expire")
|
||||||
ErrAuthClient = Errorf(208, "Auth Token Client Not Passed")
|
ErrAuthClient = Errorf(209, "Auth Token Client Not Passed")
|
||||||
|
ErrJsonDecode = Errorf(210, "Auth JSON Decode Error")
|
||||||
|
ErrJsonEncode = Errorf(211, "Auth JSON Encode Error")
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue