github.com/iikira/iikira-go-utils@v0.0.0-20230610031953-f2cb11cde33a/utils/bdcrypto/aes_test.go (about)

     1  package bdcrypto
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  var (
    11  	bkey      = []byte("asfasfawefwfgagasfasfawefwfgag")
    12  	plaintext = "1111"
    13  )
    14  
    15  func TestAesStream(t *testing.T) {
    16  	var key [32]byte
    17  	copy(key[:], bkey)
    18  
    19  	rd, err := Aes256CFBEncrypt(key, strings.NewReader(plaintext))
    20  	if err != nil {
    21  		t.Error(err)
    22  	}
    23  
    24  	by, _ := ioutil.ReadAll(rd)
    25  
    26  	t.Log("encd:", by)
    27  
    28  	rd, err = Aes256CFBDecrypt(key, bytes.NewReader(by))
    29  	by, _ = ioutil.ReadAll(rd)
    30  	t.Log("decd:", by)
    31  
    32  }
    33  
    34  func TestAesBlock(t *testing.T) {
    35  	var key [16]byte
    36  	copy(key[:], bkey)
    37  
    38  	ciphertext, err := Aes128ECBEncrypt(key, []byte(plaintext))
    39  	if err != nil {
    40  		t.Log(err)
    41  	}
    42  
    43  	t.Log("encd:", ciphertext)
    44  
    45  	plain, err := Aes128ECBDecrypt(key, ciphertext)
    46  	if err != nil {
    47  		t.Log(err)
    48  	}
    49  
    50  	t.Log("decd:", plain)
    51  }