12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package encoding
- import (
- "testing"
- )
- func TestAES(t *testing.T) {
- origin := "foo"
- key := "@MKU^AAOPH%%$#VJ<%G&j*o#$XZSWQ@L"
- encrypted, err := AESEncrypt(origin, key)
- if err != nil {
- t.Fatal(err)
- }
- if encrypted != "gp4ij9IRlDNQLDNQFwPOhg==" {
- t.Fatal("加密密文不正确")
- }
- decrypted, err := AESDecrypt(encrypted, key)
- if err != nil {
- t.Fatal(err)
- }
- if decrypted != origin {
- t.Fatal("解密内容和加密内容不一致")
- }
- }
- func TestMD5(t *testing.T) {
- origin := "foo"
- salt := "foo-salt"
- md5Str := MD5(origin, "")
- if md5Str != "666f6fd41d8cd98f00b204e9800998ecf8427e" {
- t.Fatal("md5字符串不正确")
- }
- md5WithSaltStr := MD5(origin, salt)
- if md5WithSaltStr != "666f6f666f6f2d73616c74d41d8cd98f00b204e9800998ecf8427e" {
- t.Fatal("md5字符串不正确")
- }
- }
- func TestSHA256(t *testing.T) {
- origin := "foo"
- salt := "foo-salt"
- sha256Str := SHA256(origin, "")
- if sha256Str != "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae" {
- t.Fatal("sha256字符串不正确")
- }
- sha256WithSaltStr := SHA256(origin, salt)
- if sha256WithSaltStr != "941189487b27649bf72f232b199c9b859d5e7b26f162f2efe201a9c7a12ce5a2" {
- t.Fatal("sha256字符串不正确")
- }
- }
- func TestSHA1(t *testing.T) {
- origin := "foo"
- salt := "foo-salt"
- sha1Str := SHA1(origin, "")
- if sha1Str != "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" {
- t.Fatal("sha1字符串不正确")
- }
- sha1WithSaltStr := SHA1(origin, salt)
- if sha1WithSaltStr != "fad8aee9202a46f2b42519e09e845b939ce3936a" {
- t.Fatal("sha1字符串不正确")
- }
- }
- func TestBase64(t *testing.T) {
- origin := "foo"
- encoded := Base64Encode([]byte(origin))
- if encoded != "Zm9v" {
- t.Fatal("base64加密失败")
- }
- decoded, err := Base64Decode(encoded)
- if err != nil {
- t.Fatal(err)
- }
- if string(decoded) != origin {
- t.Fatal("base64解密失败")
- }
- }
|