Skip to content

Commit

Permalink
增加加密部分的错误处理
Browse files Browse the repository at this point in the history
  • Loading branch information
karldoenitz committed Oct 11, 2023
1 parent 9cd28d7 commit 56bdab0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
13 changes: 11 additions & 2 deletions TigoWeb/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,37 @@ type Cookie struct {
// GetCookieEncodeValue 获取cookie加密值
// - IsSecurity如果设置为false,则返回原始值
// - IsSecurity如果设置为true,则返回加密后的值
//
// 如果加密失败,则抛出异常
func (cookie *Cookie) GetCookieEncodeValue() (result string) {
if !cookie.IsSecurity {
return cookie.Value
}
value := []byte(cookie.Value)
key := []byte(cookie.SecurityKey)
result = Encrypt(value, key)
var err error
result, err = Encrypt(value, key)
if err != nil {
logger.Error.Printf("get encode cookie error: %s", err.Error())
}
return result
}

// GetCookieDecodeValue 获取cookie解密值
// - IsSecurity如果设置为false,则返回原始值
// - IsSecurity如果设置为true,则返回加密后的值
//
// 如果解密失败,则抛出异常
func (cookie *Cookie) GetCookieDecodeValue() (result string) {
if !cookie.IsSecurity {
return cookie.Value
}
value := []byte(cookie.Value)
key := []byte(cookie.SecurityKey)
securityValue := Decrypt(value, key)
securityValue, err := Decrypt(value, key)
if err != nil {
logger.Error.Printf("get decode cookie error: %s", err.Error())
}
result = string(securityValue)
return result
}
Expand Down
16 changes: 7 additions & 9 deletions TigoWeb/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,21 @@ import (

// Encrypt 方法用来根据key对原始数据进行加密,并将加密结果进行base64编码,
// 加密失败则返回空
// - 此处以后会进行异常处理方面的优化
// - src: 原信息
// - key: 加密密钥
func Encrypt(src []byte, key []byte) string {
encryptValue, _ := encrypt(src, key)
return base64.StdEncoding.EncodeToString(encryptValue)
func Encrypt(src []byte, key []byte) (string, error) {
encryptValue, err := encrypt(src, key)
return base64.StdEncoding.EncodeToString(encryptValue), err
}

// Decrypt 方法会先对原始数据进行base64解码,然后根据key进行解密,
// 解密失败则返回空
// - 此处以后会进行异常处理方面的优化
// - src: 加密后的数据
// - key: 加密时使用的密钥
func Decrypt(src []byte, key []byte) []byte {
result, _ := base64.StdEncoding.DecodeString(string(src))
value, _ := decrypt(result, key)
return value
func Decrypt(src []byte, key []byte) ([]byte, error) {
result, err := base64.StdEncoding.DecodeString(string(src))
value, err := decrypt(result, key)
return value, err
}

// aes加密函数,
Expand Down

0 comments on commit 56bdab0

Please sign in to comment.