github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/commons/cryptos/aes/aes_test.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package aes_test
    19  
    20  import (
    21  	"fmt"
    22  	"github.com/aacfactory/fns/commons/cryptos/aes"
    23  	"github.com/aacfactory/fns/commons/cryptos/ciphers"
    24  	"testing"
    25  	"time"
    26  )
    27  
    28  func TestCBC(t *testing.T) {
    29  	cbc, cbcErr := aes.NewCBC([]byte("1234512345123451"), []byte("6789067890678906"), ciphers.PKCS7)
    30  	if cbcErr != nil {
    31  		t.Error(cbcErr)
    32  		return
    33  	}
    34  	p := []byte(time.Now().String())
    35  	e, encodeErr := cbc.Encrypt(p)
    36  	if encodeErr != nil {
    37  		t.Error(encodeErr)
    38  		return
    39  	}
    40  	pp, decodeErr := cbc.Decrypt(e)
    41  	if decodeErr != nil {
    42  		t.Error(decodeErr)
    43  		return
    44  	}
    45  	fmt.Println(string(p) == string(pp), string(p), string(pp))
    46  }
    47  
    48  func TestEBC(t *testing.T) {
    49  	ebc, ebcErr := aes.NewEBC([]byte("1234512345123451"), ciphers.PKCS7)
    50  	if ebcErr != nil {
    51  		t.Error(ebcErr)
    52  		return
    53  	}
    54  	p := []byte(time.Now().String())
    55  	e, encodeErr := ebc.Encrypt(p)
    56  	if encodeErr != nil {
    57  		t.Error(encodeErr)
    58  		return
    59  	}
    60  	pp, decodeErr := ebc.Decrypt(e)
    61  	if decodeErr != nil {
    62  		t.Error(decodeErr)
    63  		return
    64  	}
    65  	fmt.Println(string(p) == string(pp), string(p), string(pp))
    66  }