github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/commons/cryptos/des/des_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 des_test
    19  
    20  import (
    21  	"fmt"
    22  	"github.com/aacfactory/fns/commons/cryptos/ciphers"
    23  	"github.com/aacfactory/fns/commons/cryptos/des"
    24  	"testing"
    25  	"time"
    26  )
    27  
    28  func TestNewCBC(t *testing.T) {
    29  	ebc, ebcErr := des.NewEBC([]byte("12345123"), ciphers.PKCS7)
    30  	if ebcErr != nil {
    31  		t.Error(ebcErr)
    32  		return
    33  	}
    34  	p := []byte(time.Now().String())
    35  	e, encodeErr := ebc.Encrypt(p)
    36  	if encodeErr != nil {
    37  		t.Error(encodeErr)
    38  		return
    39  	}
    40  	pp, decodeErr := ebc.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 TestNewEBC(t *testing.T) {
    49  	cbc, cbcErr := des.NewCBC([]byte("12345123"), []byte("67890678"), ciphers.PKCS7)
    50  	if cbcErr != nil {
    51  		t.Error(cbcErr)
    52  		return
    53  	}
    54  	p := []byte(time.Now().String())
    55  	e, encodeErr := cbc.Encrypt(p)
    56  	if encodeErr != nil {
    57  		t.Error(encodeErr)
    58  		return
    59  	}
    60  	pp, decodeErr := cbc.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  }