mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-03 12:20:27 +08:00
[waf]增加若干个编解码方法
This commit is contained in:
20
pkg/serverconfigs/filterconfigs/filter_dec2hex.go
Normal file
20
pkg/serverconfigs/filterconfigs/filter_dec2hex.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
type Dec2HexFilter struct {
|
||||
}
|
||||
|
||||
// 初始化
|
||||
func (this *Dec2HexFilter) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 执行过滤
|
||||
func (this *Dec2HexFilter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) {
|
||||
v := types.Int64(input)
|
||||
return fmt.Sprintf("%x", v), true, nil
|
||||
}
|
||||
15
pkg/serverconfigs/filterconfigs/filter_dec2hex_test.go
Normal file
15
pkg/serverconfigs/filterconfigs/filter_dec2hex_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDec2HexFilter_Do(t *testing.T) {
|
||||
filter := &Dec2HexFilter{}
|
||||
err := filter.Init()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(filter.Do("123456", nil))
|
||||
t.Log(filter.Do("1", nil))
|
||||
}
|
||||
21
pkg/serverconfigs/filterconfigs/filter_hex2dec.go
Normal file
21
pkg/serverconfigs/filterconfigs/filter_hex2dec.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
type Hex2DecFilter struct {
|
||||
}
|
||||
|
||||
// 初始化
|
||||
func (this *Hex2DecFilter) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 执行过滤
|
||||
func (this *Hex2DecFilter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) {
|
||||
n := new(big.Int)
|
||||
n.SetString(types.String(input), 16)
|
||||
return n.Uint64(), true, nil
|
||||
}
|
||||
29
pkg/serverconfigs/filterconfigs/filter_hex2dec_test.go
Normal file
29
pkg/serverconfigs/filterconfigs/filter_hex2dec_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHex2DecFilter_Do(t *testing.T) {
|
||||
filter := &Hex2DecFilter{}
|
||||
err := filter.Init()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(filter.Do("0e", nil))
|
||||
t.Log(filter.Do("e", nil))
|
||||
|
||||
{
|
||||
result, _, _ := filter.Do("123", nil)
|
||||
t.Logf("%x", result)
|
||||
}
|
||||
|
||||
{
|
||||
md5 := stringutil.Md5("123456")
|
||||
t.Log("md5:", md5)
|
||||
result, _, _ := filter.Do(md5, nil)
|
||||
t.Log(result)
|
||||
t.Logf("%x", result)
|
||||
}
|
||||
}
|
||||
20
pkg/serverconfigs/filterconfigs/filter_sha1.go
Normal file
20
pkg/serverconfigs/filterconfigs/filter_sha1.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
type Sha1Filter struct {
|
||||
}
|
||||
|
||||
// 初始化
|
||||
func (this *Sha1Filter) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 执行过滤
|
||||
func (this *Sha1Filter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) {
|
||||
return fmt.Sprintf("%x", sha1.Sum([]byte(types.String(input)))), true, nil
|
||||
}
|
||||
9
pkg/serverconfigs/filterconfigs/filter_sha1_test.go
Normal file
9
pkg/serverconfigs/filterconfigs/filter_sha1_test.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package filterconfigs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSha1Filter_Do(t *testing.T) {
|
||||
filter := &Sha1Filter{}
|
||||
t.Log(filter.Do("123456", nil))
|
||||
t.Log(filter.Do("", nil))
|
||||
}
|
||||
20
pkg/serverconfigs/filterconfigs/filter_sha256.go
Normal file
20
pkg/serverconfigs/filterconfigs/filter_sha256.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
type Sha256Filter struct {
|
||||
}
|
||||
|
||||
// 初始化
|
||||
func (this *Sha256Filter) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 执行过滤
|
||||
func (this *Sha256Filter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) {
|
||||
return fmt.Sprintf("%x", sha256.Sum256([]byte(types.String(input)))), true, nil
|
||||
}
|
||||
9
pkg/serverconfigs/filterconfigs/filter_sha256_test.go
Normal file
9
pkg/serverconfigs/filterconfigs/filter_sha256_test.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package filterconfigs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSha256Filter_Do(t *testing.T) {
|
||||
filter := &Sha256Filter{}
|
||||
t.Log(filter.Do("123456", nil))
|
||||
t.Log(filter.Do("", nil))
|
||||
}
|
||||
@@ -19,6 +19,10 @@ var allFilters = map[string]FilterInterface{
|
||||
"base64Encode": new(Base64EncodeFilter),
|
||||
"base64Decode": new(Base64DecodeFilter),
|
||||
"length": new(LengthFilter),
|
||||
"hex2dec": new(Hex2DecFilter),
|
||||
"dec2hex": new(Dec2HexFilter),
|
||||
"sha1": new(Sha1Filter),
|
||||
"sha256": new(Sha256Filter),
|
||||
}
|
||||
|
||||
// 查找Filter
|
||||
|
||||
Reference in New Issue
Block a user