fix encipher return errors

This commit is contained in:
yanweidong 2024-02-28 13:47:23 +08:00
parent 8bff8e7377
commit 26048b1b41
2 changed files with 13 additions and 9 deletions

View File

@ -6,7 +6,6 @@ import (
"crypto/cipher"
"encoding/base64"
"encoding/json"
"errors"
"strings"
"time"
@ -29,7 +28,7 @@ func New() {
func GenerateTokenAes(id uint, identity, client, role string, extend map[string]string) (string, error) {
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)
claims := types.JwtClaims{
@ -43,7 +42,7 @@ func GenerateTokenAes(id uint, identity, client, role string, extend map[string]
byte, err := json.Marshal(claims)
if err != nil {
return "", err
return "", exception.ErrJsonEncode
}
token, err := AesEncryptCBC(byte)
@ -57,7 +56,10 @@ func AesEncryptCBC(plan []byte) (string, error) {
// 分组秘钥
// NewCipher该函数限制了输入k的长度必须为16, 24或者32
block, _ := aes.NewCipher(JwtSecret)
block, err := aes.NewCipher(JwtSecret)
if err != nil {
return "", exception.ErrAuthSecret
}
// 获取秘钥块的长度
blockSize := block.BlockSize()
// 补全码
@ -73,17 +75,17 @@ func AesEncryptCBC(plan []byte) (string, error) {
func AesDecryptCBC(cryted string) (b []byte, err error) {
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)
if err != nil {
return
return nil, exception.ErrBase64Decode
}
// 分组秘钥
block, err := aes.NewCipher(JwtSecret)
if err != nil {
return
return nil, exception.ErrAuthSecret
}
// 获取秘钥块的长度
blockSize := block.BlockSize()

View File

@ -3,7 +3,7 @@ package exception
// jwt custom error code ,begin:200
var (
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")
ErrAuthParseFail = Errorf(203, "Auth Parse Fail")
ErrAuthId = Errorf(204, "Auth Id Not Passed")
@ -11,5 +11,7 @@ var (
ErrAuthTokenChanged = Errorf(206, "Auth Token Changed")
ErrAuthIdType = Errorf(207, "Auth Id Type Error")
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")
)