github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/encrypt/crypto/option_test.go (about)

     1  package crypto
     2  
     3  import (
     4  	"encoding/base32"
     5  	"encoding/base64"
     6  	"regexp"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func Test_Option_Base64(t *testing.T) {
    13  	for _, testkey := range testKeyList {
    14  		ciphertext, err := CFBEncrypt(plaintext, testkey, Base64(base64.URLEncoding))
    15  		assert.Nil(t, err)
    16  
    17  		t.Log(string(ciphertext))
    18  		base64Pattern := regexp.MustCompile(`^[A-Za-z0-9_\-]+=*$`)
    19  		assert.Regexp(t, base64Pattern, string(ciphertext))
    20  
    21  		decrypted, err := CFBDecrypt(ciphertext, testkey, Base64(base64.URLEncoding))
    22  		assert.Nil(t, err)
    23  		assert.Equal(t, plaintext, decrypted)
    24  	}
    25  }
    26  
    27  func Test_Option_Base32(t *testing.T) {
    28  	for _, testkey := range testKeyList {
    29  		ciphertext, err := CBCEncrypt(plaintext, testkey,
    30  			Base32(base32.HexEncoding.WithPadding(base32.NoPadding)))
    31  		assert.Nil(t, err)
    32  
    33  		t.Log(string(ciphertext))
    34  		base32Pattern := regexp.MustCompile(`^[A-Za-z0-9]+$`)
    35  		assert.Regexp(t, base32Pattern, string(ciphertext))
    36  
    37  		decrypted, err := CBCDecrypt(ciphertext, testkey,
    38  			Base32(base32.HexEncoding.WithPadding(base32.NoPadding)))
    39  		assert.Nil(t, err)
    40  		assert.Equal(t, plaintext, decrypted)
    41  	}
    42  }