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

     1  package xcrypto
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestDesCBC(t *testing.T) {
    10  	cipherText, err := DesCBCEncrypt(origin, key[:8], iv[:8])
    11  	if err != nil {
    12  		t.Fatalf("des cbc encrypt err:%v", err)
    13  	}
    14  
    15  	originText, err := DesCBCDecrypt(cipherText, key[:8], iv[:8])
    16  	if err != nil {
    17  		t.Fatalf("des cbc decrypt err:%v", err)
    18  	}
    19  	assert.Equal(t, originText, origin)
    20  }
    21  
    22  func TestDesCFB(t *testing.T) {
    23  
    24  	// 加密
    25  	cipherText, err := DesCFBEncrypt(origin, key[:8], iv[:8])
    26  	if err != nil {
    27  		t.Fatal("des cfb encrypt err: ", err)
    28  	}
    29  
    30  	// 解密
    31  	originText, err := DesCFBDecrypt(cipherText, key[:8], iv[:8])
    32  	if err != nil {
    33  		t.Fatal("des cfb decrypt err: ", err)
    34  	}
    35  	assert.Equal(t, originText, origin)
    36  }
    37  
    38  func TestDesCTR(t *testing.T) {
    39  
    40  	// 加密
    41  	cipherText, err := DesCTREncrypt(origin, key[:8], iv[:8])
    42  	if err != nil {
    43  		t.Fatal("des ctr encrypt err: ", err)
    44  	}
    45  
    46  	// 解密
    47  	originText, err := DesCTRDecrypt(cipherText, key[:8], iv[:8])
    48  	if err != nil {
    49  		t.Fatal("des ctr decrypt err: ", err)
    50  	}
    51  	assert.Equal(t, originText, origin)
    52  }
    53  
    54  func TestDesOFB(t *testing.T) {
    55  	// 加密
    56  	cipherText, err := DesOFBEncrypt(origin, key[:8], iv[:8])
    57  	if err != nil {
    58  		t.Fatal("des ofb encrypt err: ", err)
    59  	}
    60  
    61  	// 解密
    62  	originText, err := DesOFBDecrypt(cipherText, key[:8], iv[:8])
    63  	if err != nil {
    64  		t.Fatal("des ofb decrypt err: ", err)
    65  	}
    66  	assert.Equal(t, originText, origin)
    67  }
    68  
    69  func TestDesOFBStream(t *testing.T) {
    70  	// StreamReader方式加密
    71  	cipherText, err := DesOFBEncryptStreamReader(origin, key[:8], iv[:8])
    72  	if err != nil {
    73  		t.Fatal("des ofb stream encrypt err: ", err)
    74  	}
    75  
    76  	// StreamWriter方式解密
    77  	originText, err := DesOFBDecryptStreamWriter(cipherText, key[:8], iv[:8])
    78  	if err != nil {
    79  		t.Fatal("des ofb stream decrypt err: ", err)
    80  	}
    81  	assert.Equal(t, originText, origin)
    82  }