BasicTypeAes

package module
v0.0.0-...-d353649 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

BasicTypeAes

Aes加解密适用于golang基础类型的封装

使用方法

func TestAesEncrypt(t *testing.T) {
	data := &Data{}
	var int64PlainData int64 = 100
	var stringPlainData string = "test example"
	var bytesPlainData []byte = []byte("sdagasdaa")
	var float64PlainData float64 = 123.456
	int64CipherData, err := data.Encrypt(int64PlainData)
	assert.Nil(t, err)
	assert.Equal(t, int(data.DataType), Int64Data)
	t.Logf("the int64CipherData is %v", int64CipherData)

	stringCipherData, err := data.Encrypt(stringPlainData)
	assert.Nil(t, err)
	assert.Equal(t, int(data.DataType), StringData)
	t.Logf("the stringCipherData is %v", stringCipherData)

	bytesCipherData, err := data.Encrypt(bytesPlainData)
	assert.Nil(t, err)
	assert.Equal(t, int(data.DataType), BytesData)
	t.Logf("the bytesCipherData is %v", bytesCipherData)

	float64CipherData, err := data.Encrypt(float64PlainData)
	assert.Nil(t, err)
	assert.Equal(t, int(data.DataType), Float64Data)
	t.Logf("the float64CipherData is %v", float64CipherData)

	num, err := data.DecryptInt64(int64CipherData)
	assert.Nil(t, err)
	assert.Equal(t, int(data.DataType), Int64Data)
	assert.Equal(t, num, int64PlainData)
	t.Logf("the num is %v", num)

	str, err := data.DecryptString(stringCipherData)
	assert.Nil(t, err)
	assert.Equal(t, int(data.DataType), StringData)
	assert.Equal(t, str, stringPlainData)
	t.Logf("the str is %v", str)

	bytes, err := data.DecryptBytes(bytesCipherData)
	assert.Nil(t, err)
	assert.Equal(t, int(data.DataType), BytesData)
	assert.Equal(t, bytes, bytesPlainData)
	t.Logf("the bytes is %v", string(bytes))

	float64Num, err := data.DecryptFloat64(float64CipherData)
	assert.Nil(t, err)
	assert.Equal(t, int(data.DataType), Float64Data)
	assert.Equal(t, float64Num, float64PlainData)
	t.Logf("the float64Num is %v", float64Num)

}

测试输出:

=== RUN   TestAesEncrypt
    TestAesEncrypt: controller_test.go:18: the int64CipherData is MS4wLjACAAAAAAAAAJL8WssDe6xNtrXRKnE8DgI
    TestAesEncrypt: controller_test.go:23: the stringCipherData is MS4wLjABAAAAAAAAAAzmsPvmdGv05Zr-r2UVOes
    TestAesEncrypt: controller_test.go:28: the bytesCipherData is MS4wLjADAAAAAAAAANWwL8Q6iJrjMosDa75d1yg
    TestAesEncrypt: controller_test.go:33: the float64CipherData is MS4wLjAEAAAAAAAAABVq-xWKB68hxLlHIAjVn5Q
    TestAesEncrypt: controller_test.go:39: the num is 100
    TestAesEncrypt: controller_test.go:45: the str is test example
    TestAesEncrypt: controller_test.go:51: the bytes is sdagasdaa
    TestAesEncrypt: controller_test.go:57: the float64Num is 123.456
--- PASS: TestAesEncrypt (0.00s)
PASS

Documentation

Index

Constants

View Source
const (
	StringData  = 1
	Int64Data   = 2
	BytesData   = 3
	Float64Data = 4
)
View Source
const SumKey = 1
View Source
const VersionLen = 5

Variables

View Source
var AesKeys []string
View Source
var Version = []byte("1.0.0")

Functions

func AesDecrypt

func AesDecrypt(crypted, key []byte) ([]byte, error)

AES解密

func AesEncrypt

func AesEncrypt(origData, key []byte) ([]byte, error)

AES加密

func Float64StrToBytes

func Float64StrToBytes(num float64) []byte

func GetRandomKeyIndex

func GetRandomKeyIndex() int32

func Int64StrToBytes

func Int64StrToBytes(num int64) []byte

func PKCS5Padding

func PKCS5Padding(plaintext []byte, blockSize int) []byte

填充明文

func PKCS5UnPadding

func PKCS5UnPadding(origData []byte) []byte

去除填充数据

func StringToBytes

func StringToBytes(str string) []byte

Types

type AesController

type AesController interface {
	Encrypt(plainData interface{}) (string, error)
	DecryptInt64(cipherStr string) (int64, error)
	DecryptFloat64(cipherStr string) (float64, error)
	DecryptString(cipherStr string) (string, error)
	DecryptBytes(cipherStr string) ([]byte, error)
}

type Data

type Data struct {
	CipherData []byte
	DataType   int32
	PlainData  []byte
	Version    []byte
	KeyIndex   int32
}

func (*Data) CheckNil

func (data *Data) CheckNil() bool

func (*Data) DecryptBytes

func (data *Data) DecryptBytes(cipherStr string) ([]byte, error)

func (*Data) DecryptFloat64

func (data *Data) DecryptFloat64(cipherStr string) (float64, error)

func (*Data) DecryptInt64

func (data *Data) DecryptInt64(cipherStr string) (int64, error)

func (*Data) DecryptString

func (data *Data) DecryptString(cipherStr string) (string, error)

func (*Data) Encrypt

func (data *Data) Encrypt(plainData interface{}) (string, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL