github.com/zhongdalu/gf@v1.0.0/g/crypto/gaes/gaes_test.go (about)

     1  // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  // go test *.go -bench=".*"
     8  
     9  package gaes_test
    10  
    11  import (
    12  	"testing"
    13  
    14  	"github.com/zhongdalu/gf/g/encoding/gbase64"
    15  
    16  	"github.com/zhongdalu/gf/g/crypto/gaes"
    17  	"github.com/zhongdalu/gf/g/test/gtest"
    18  )
    19  
    20  var (
    21  	content          = []byte("pibigstar")
    22  	content_16, _    = gbase64.DecodeString("v1jqsGHId/H8onlVHR8Vaw==")
    23  	content_24, _    = gbase64.DecodeString("0TXOaj5KMoLhNWmJ3lxY1A==")
    24  	content_32, _    = gbase64.DecodeString("qM/Waw1kkWhrwzek24rCSA==")
    25  	content_16_iv, _ = gbase64.DecodeString("DqQUXiHgW/XFb6Qs98+hrA==")
    26  	content_32_iv, _ = gbase64.DecodeString("ZuLgAOii+lrD5KJoQ7yQ8Q==")
    27  	// iv 长度必须等于blockSize,只能为16
    28  	iv         = []byte("Hello My GoFrame")
    29  	key_16     = []byte("1234567891234567")
    30  	key_17     = []byte("12345678912345670")
    31  	key_24     = []byte("123456789123456789123456")
    32  	key_32     = []byte("12345678912345678912345678912345")
    33  	keys       = []byte("12345678912345678912345678912346")
    34  	key_err    = []byte("1234")
    35  	key_32_err = []byte("1234567891234567891234567891234 ")
    36  
    37  	// cfb模式blockSize补位长度, add by zseeker
    38  	padding_size      = 16 - len(content)
    39  	content_16_cfb, _ = gbase64.DecodeString("oSmget3aBDT1nJnBp8u6kA==")
    40  )
    41  
    42  func TestEncrypt(t *testing.T) {
    43  	gtest.Case(t, func() {
    44  		data, err := gaes.Encrypt(content, key_16)
    45  		gtest.Assert(err, nil)
    46  		gtest.Assert(data, []byte(content_16))
    47  		data, err = gaes.Encrypt(content, key_24)
    48  		gtest.Assert(err, nil)
    49  		gtest.Assert(data, []byte(content_24))
    50  		data, err = gaes.Encrypt(content, key_32)
    51  		gtest.Assert(err, nil)
    52  		gtest.Assert(data, []byte(content_32))
    53  		data, err = gaes.Encrypt(content, key_16, iv)
    54  		gtest.Assert(err, nil)
    55  		gtest.Assert(data, []byte(content_16_iv))
    56  		data, err = gaes.Encrypt(content, key_32, iv)
    57  		gtest.Assert(err, nil)
    58  		gtest.Assert(data, []byte(content_32_iv))
    59  	})
    60  }
    61  
    62  func TestDecrypt(t *testing.T) {
    63  	gtest.Case(t, func() {
    64  		decrypt, err := gaes.Decrypt([]byte(content_16), key_16)
    65  		gtest.Assert(err, nil)
    66  		gtest.Assert(decrypt, content)
    67  
    68  		decrypt, err = gaes.Decrypt([]byte(content_24), key_24)
    69  		gtest.Assert(err, nil)
    70  		gtest.Assert(decrypt, content)
    71  
    72  		decrypt, err = gaes.Decrypt([]byte(content_32), key_32)
    73  		gtest.Assert(err, nil)
    74  		gtest.Assert(decrypt, content)
    75  
    76  		decrypt, err = gaes.Decrypt([]byte(content_16_iv), key_16, iv)
    77  		gtest.Assert(err, nil)
    78  		gtest.Assert(decrypt, content)
    79  
    80  		decrypt, err = gaes.Decrypt([]byte(content_32_iv), key_32, iv)
    81  		gtest.Assert(err, nil)
    82  		gtest.Assert(decrypt, content)
    83  
    84  		decrypt, err = gaes.Decrypt([]byte(content_32_iv), keys, iv)
    85  		gtest.Assert(err, "invalid padding")
    86  	})
    87  }
    88  
    89  func TestEncryptErr(t *testing.T) {
    90  	gtest.Case(t, func() {
    91  		// encrypt key error
    92  		_, err := gaes.Encrypt(content, key_err)
    93  		gtest.AssertNE(err, nil)
    94  	})
    95  }
    96  
    97  func TestDecryptErr(t *testing.T) {
    98  	gtest.Case(t, func() {
    99  		// decrypt key error
   100  		encrypt, err := gaes.Encrypt(content, key_16)
   101  		_, err = gaes.Decrypt(encrypt, key_err)
   102  		gtest.AssertNE(err, nil)
   103  
   104  		// decrypt content too short error
   105  		_, err = gaes.Decrypt([]byte("test"), key_16)
   106  		gtest.AssertNE(err, nil)
   107  
   108  		// decrypt content size error
   109  		_, err = gaes.Decrypt(key_17, key_16)
   110  		gtest.AssertNE(err, nil)
   111  	})
   112  }
   113  
   114  func TestPKCS5UnPaddingErr(t *testing.T) {
   115  	gtest.Case(t, func() {
   116  		// PKCS5UnPadding blockSize zero
   117  		_, err := gaes.PKCS5UnPadding(content, 0)
   118  		gtest.AssertNE(err, nil)
   119  
   120  		// PKCS5UnPadding src len zero
   121  		_, err = gaes.PKCS5UnPadding([]byte(""), 16)
   122  		gtest.AssertNE(err, nil)
   123  
   124  		// PKCS5UnPadding src len > blockSize
   125  		_, err = gaes.PKCS5UnPadding(key_17, 16)
   126  		gtest.AssertNE(err, nil)
   127  
   128  		// PKCS5UnPadding src len > blockSize
   129  		_, err = gaes.PKCS5UnPadding(key_32_err, 32)
   130  		gtest.AssertNE(err, nil)
   131  	})
   132  }
   133  
   134  func TestEncryptCFB(t *testing.T) {
   135  	gtest.Case(t, func() {
   136  		var padding int = 0
   137  		data, err := gaes.EncryptCFB(content, key_16, &padding, iv)
   138  		gtest.Assert(err, nil)
   139  		gtest.Assert(padding, padding_size)
   140  		gtest.Assert(data, []byte(content_16_cfb))
   141  	})
   142  }
   143  
   144  func TestDecryptCFB(t *testing.T) {
   145  	gtest.Case(t, func() {
   146  		decrypt, err := gaes.DecryptCFB([]byte(content_16_cfb), key_16, padding_size, iv)
   147  		gtest.Assert(err, nil)
   148  		gtest.Assert(decrypt, content)
   149  	})
   150  }