github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/util/xcrypto/aes_test.go (about)

     1  package xcrypto
     2  
     3  import (
     4  	"crypto/rand"
     5  	"io"
     6  	"log"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestAesCBC(t *testing.T) {
    13  	cipherText, err := AesCBCEncrypt(origin, key, iv)
    14  	if err != nil {
    15  		t.Fatalf("aes cbc encrypt err:%v", err)
    16  	}
    17  
    18  	originText, err := AesCBCDecrypt(cipherText, key, iv)
    19  	if err != nil {
    20  		t.Fatalf("aes cbc decrypt err:%v", err)
    21  	}
    22  	assert.Equal(t, originText, origin)
    23  }
    24  
    25  func TestAesCFB(t *testing.T) {
    26  
    27  	// 加密
    28  	cipherText, err := AesCFBEncrypt(origin, key, iv)
    29  	if err != nil {
    30  		t.Fatal("aes cfb encrypt err: ", err)
    31  	}
    32  
    33  	// 解密
    34  	originText, err := AesCFBDecrypt(cipherText, key, iv)
    35  	if err != nil {
    36  		t.Fatal("aes cfb decrypt err: ", err)
    37  	}
    38  	assert.Equal(t, originText, origin)
    39  }
    40  
    41  func TestAesCTR(t *testing.T) {
    42  
    43  	// 加密
    44  	cipherText, err := AesCTREncrypt(origin, key, iv)
    45  	if err != nil {
    46  		t.Fatal("aes ctr encrypt err: ", err)
    47  	}
    48  
    49  	// 解密
    50  	originText, err := AesCTRDecrypt(cipherText, key, iv)
    51  	if err != nil {
    52  		t.Fatal("aes ctr decrypt err: ", err)
    53  	}
    54  	assert.Equal(t, originText, origin)
    55  }
    56  
    57  func TestAesOFB(t *testing.T) {
    58  	// 加密
    59  	cipherText, err := AesOFBEncrypt(origin, key, iv)
    60  	if err != nil {
    61  		t.Fatal("aes ofb encrypt err: ", err)
    62  	}
    63  
    64  	// 解密
    65  	originText, err := AesOFBDecrypt(cipherText, key, iv)
    66  	if err != nil {
    67  		t.Fatal("aes ofb decrypt err: ", err)
    68  	}
    69  	assert.Equal(t, originText, origin)
    70  }
    71  
    72  func TestAesGCM(t *testing.T) {
    73  	// 初始化一个长度为12字节的空的[]byte,不要使用超过2^32个随机非字符,因为存在重复的风险
    74  	nonce := make([]byte, 12)
    75  	// 使用rand随机生成数据
    76  	if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
    77  		log.Fatal(err)
    78  	}
    79  	// StreamReader方式加密
    80  	cipherText, err := AesGCMEncrypt(origin, key, nonce)
    81  	if err != nil {
    82  		t.Fatal("aes gcm stream encrypt err: ", err)
    83  	}
    84  
    85  	// StreamWriter方式解密
    86  	originText, err := AesGCMDecrypt(cipherText, key, nonce)
    87  	if err != nil {
    88  		t.Fatal("aes gcm stream decrypt err: ", err)
    89  	}
    90  	assert.Equal(t, originText, origin)
    91  }
    92  
    93  func TestAesOFBStream(t *testing.T) {
    94  	// StreamReader方式加密
    95  	cipherText, err := AesOFBEncryptStreamReader(origin, key, iv)
    96  	if err != nil {
    97  		t.Fatal("aes ofb stream encrypt err: ", err)
    98  	}
    99  
   100  	// StreamWriter方式解密
   101  	originText, err := AesOFBDecryptStreamWriter(cipherText, key, iv)
   102  	if err != nil {
   103  		t.Fatal("aes ofb stream decrypt err: ", err)
   104  	}
   105  	assert.Equal(t, originText, origin)
   106  }