Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature v2.0.0 #97

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 14 additions & 12 deletions TigoWeb/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ import (

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

// Decrypt 方法会先对原始数据进行base64解码,然后根据key进行解密,
// 解密失败则返回空
// - 此处以后会进行异常处理方面的优化
func Decrypt(src []byte, key []byte) []byte {
result, _ := base64.StdEncoding.DecodeString(string(src))
value, _ := decrypt(result, key)
return value
// - src: 加密后的数据
// - key: 加密时使用的密钥
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 Expand Up @@ -126,9 +128,9 @@ func UrlDecode(value string) (result string) {

// VoidFuncCall 调用某个指定的方法,通过反射获取某个变量的值,然后通过传入的方法名,调用这个变量中的方法;
// 这个方法只适用于没有入参,且无返回值的函数调用
// - instance: 实例
// - funcName: 需要调用的方法名
// - funcParams: 调用函数所需要的参数
// - instance: 实例
// - funcName: 需要调用的方法名
// - funcParams: 调用函数所需要的参数
func VoidFuncCall(instance reflect.Value, funcName string, funcParams ...reflect.Value) {
function := instance.MethodByName(funcName)
if function.IsValid() {
Expand Down