From 26048b1b41aad479df51025ce79df5f0975ad803 Mon Sep 17 00:00:00 2001 From: yanweidong Date: Wed, 28 Feb 2024 13:47:23 +0800 Subject: [PATCH] fix encipher return errors --- encipher/aes.go | 16 +++++++++------- exception/auth.go | 6 ++++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/encipher/aes.go b/encipher/aes.go index de87055..9a54182 100644 --- a/encipher/aes.go +++ b/encipher/aes.go @@ -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() diff --git a/exception/auth.go b/exception/auth.go index f63187c..ef51fed 100644 --- a/exception/auth.go +++ b/exception/auth.go @@ -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") )