encoding_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package encoding
  2. import (
  3. "testing"
  4. )
  5. func TestAES(t *testing.T) {
  6. origin := "foo"
  7. key := "@MKU^AAOPH%%$#VJ<%G&j*o#$XZSWQ@L"
  8. encrypted, err := AESEncrypt(origin, key)
  9. if err != nil {
  10. t.Fatal(err)
  11. }
  12. if encrypted != "gp4ij9IRlDNQLDNQFwPOhg==" {
  13. t.Fatal("加密密文不正确")
  14. }
  15. decrypted, err := AESDecrypt(encrypted, key)
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. if decrypted != origin {
  20. t.Fatal("解密内容和加密内容不一致")
  21. }
  22. }
  23. func TestMD5(t *testing.T) {
  24. origin := "foo"
  25. salt := "foo-salt"
  26. md5Str := MD5(origin, "")
  27. if md5Str != "666f6fd41d8cd98f00b204e9800998ecf8427e" {
  28. t.Fatal("md5字符串不正确")
  29. }
  30. md5WithSaltStr := MD5(origin, salt)
  31. if md5WithSaltStr != "666f6f666f6f2d73616c74d41d8cd98f00b204e9800998ecf8427e" {
  32. t.Fatal("md5字符串不正确")
  33. }
  34. }
  35. func TestSHA256(t *testing.T) {
  36. origin := "foo"
  37. salt := "foo-salt"
  38. sha256Str := SHA256(origin, "")
  39. if sha256Str != "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae" {
  40. t.Fatal("sha256字符串不正确")
  41. }
  42. sha256WithSaltStr := SHA256(origin, salt)
  43. if sha256WithSaltStr != "941189487b27649bf72f232b199c9b859d5e7b26f162f2efe201a9c7a12ce5a2" {
  44. t.Fatal("sha256字符串不正确")
  45. }
  46. }
  47. func TestSHA1(t *testing.T) {
  48. origin := "foo"
  49. salt := "foo-salt"
  50. sha1Str := SHA1(origin, "")
  51. if sha1Str != "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" {
  52. t.Fatal("sha1字符串不正确")
  53. }
  54. sha1WithSaltStr := SHA1(origin, salt)
  55. if sha1WithSaltStr != "fad8aee9202a46f2b42519e09e845b939ce3936a" {
  56. t.Fatal("sha1字符串不正确")
  57. }
  58. }
  59. func TestBase64(t *testing.T) {
  60. origin := "foo"
  61. encoded := Base64Encode([]byte(origin))
  62. if encoded != "Zm9v" {
  63. t.Fatal("base64加密失败")
  64. }
  65. decoded, err := Base64Decode(encoded)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. if string(decoded) != origin {
  70. t.Fatal("base64解密失败")
  71. }
  72. }