Initial commit

This commit is contained in:
Donny
2019-04-22 20:46:32 +08:00
commit 49ab8aadd1
25441 changed files with 4055000 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["encoding_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = ["//vendor/github.com/smartystreets/goconvey/convey:go_default_library"],
)
go_library(
name = "go_default_library",
srcs = ["encoding.go"],
importpath = "go-common/app/common/openplatform/encoding",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,105 @@
package encoding
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/json"
"errors"
)
//EncryptConfig 加密配置
type EncryptConfig struct {
Key string
IV string
}
//JSON JSON格式化增加对[]byte的支持
func JSON(o interface{}) string {
var v []byte
switch o1 := o.(type) {
case [][]byte:
o2 := make([]interface{}, len(o1))
for i := 0; i < len(o1); i++ {
o2[i] = string(o1[i])
}
v, _ = json.Marshal(o2)
case []interface{}:
for i := 0; i < len(o1); i++ {
if b, ok := o1[i].([]byte); ok {
o1[i] = string(b)
}
}
v, _ = json.Marshal(o1)
default:
v, _ = json.Marshal(o)
}
if len(v) > 0 {
return string(v)
}
return ""
}
//填充到BlockSize整数倍长度如果正好就是对的长度再多填充一个BlockSize长度
func pad(src []byte) []byte {
padding := aes.BlockSize - len(src)%aes.BlockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}
//去除填充的字节
func unpad(src []byte) ([]byte, error) {
length := len(src)
if length == 0 {
return []byte{0}, nil
}
unpadding := int(src[length-1])
if unpadding > length {
return nil, errors.New("unpad error. This could happen when incorrect encryption key is used")
}
return src[:(length - unpadding)], nil
}
//Encrypt 加密
func Encrypt(text string, c *EncryptConfig) (string, error) {
if "" == text {
return "", nil
}
block, err := aes.NewCipher([]byte(c.Key))
if err != nil {
return "", err
}
msg := pad([]byte(text))
ciphertext := make([]byte, len(msg))
mode := cipher.NewCBCEncrypter(block, []byte(c.IV))
mode.CryptBlocks(ciphertext, msg)
finalMsg := base64.StdEncoding.EncodeToString(ciphertext)
return finalMsg, nil
}
//Decrypt 解密
func Decrypt(text string, c *EncryptConfig) (string, error) {
if "" == text {
return "", nil
}
block, err := aes.NewCipher([]byte(c.Key))
if err != nil {
return "", err
}
decodedMsg, err := base64.StdEncoding.DecodeString(text)
if err != nil {
return "", err
}
if (len(decodedMsg) % aes.BlockSize) != 0 {
return "", errors.New("blocksize must be multipe of decoded message length")
}
msg := decodedMsg
mode := cipher.NewCBCDecrypter(block, []byte(c.IV))
mode.CryptBlocks(msg, msg)
unpadMsg, err := unpad(msg)
if err != nil {
return "", err
}
return string(unpadMsg), nil
}

View File

@@ -0,0 +1,28 @@
package encoding
import (
"testing"
"github.com/smartystreets/goconvey/convey"
)
var c = &EncryptConfig{Key: "bilibili_key_GYl", IV: "biliBiliIv123456"}
const (
plat = "13291831554"
enc = "dWLVOucFkBBmmXmTm6so5A=="
)
func TestEncrypt(t *testing.T) {
convey.Convey("Encrypt", t, func() {
s, _ := Encrypt(plat, c)
convey.So(s, convey.ShouldEqual, enc)
})
}
func TestDecrypt(t *testing.T) {
convey.Convey("Decrypt", t, func() {
s, _ := Decrypt(enc, c)
convey.So(s, convey.ShouldEqual, plat)
})
}