github.com/zhongdalu/gf@v1.0.0/g/crypto/gdes/gdes_test.go (about)

     1  // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  package gdes_test
     8  
     9  import (
    10  	"encoding/hex"
    11  	"testing"
    12  
    13  	"github.com/zhongdalu/gf/g/crypto/gdes"
    14  	"github.com/zhongdalu/gf/g/test/gtest"
    15  )
    16  
    17  var (
    18  	errKey     = []byte("1111111111111234123456789")
    19  	errIv      = []byte("123456789")
    20  	errPadding = 5
    21  )
    22  
    23  func TestDesECB(t *testing.T) {
    24  	gtest.Case(t, func() {
    25  		key := []byte("11111111")
    26  		text := []byte("12345678")
    27  		padding := gdes.NOPADDING
    28  		result := "858b176da8b12503"
    29  		// encrypt test
    30  		cipherText, err := gdes.DesECBEncrypt(key, text, padding)
    31  		gtest.AssertEQ(err, nil)
    32  		gtest.AssertEQ(hex.EncodeToString(cipherText), result)
    33  		// decrypt test
    34  		clearText, err := gdes.DesECBDecrypt(key, cipherText, padding)
    35  		gtest.AssertEQ(err, nil)
    36  		gtest.AssertEQ(string(clearText), "12345678")
    37  
    38  		// encrypt err test. when throw exception,the err is not equal nil and the string is nil
    39  		errEncrypt, err := gdes.DesECBEncrypt(key, text, errPadding)
    40  		gtest.AssertNE(err, nil)
    41  		gtest.AssertEQ(errEncrypt, nil)
    42  		errEncrypt, err = gdes.DesECBEncrypt(errKey, text, padding)
    43  		gtest.AssertNE(err, nil)
    44  		gtest.AssertEQ(errEncrypt, nil)
    45  		// err decrypt test.
    46  		errDecrypt, err := gdes.DesECBDecrypt(errKey, cipherText, padding)
    47  		gtest.AssertNE(err, nil)
    48  		gtest.AssertEQ(errDecrypt, nil)
    49  		errDecrypt, err = gdes.DesECBDecrypt(key, cipherText, errPadding)
    50  		gtest.AssertNE(err, nil)
    51  		gtest.AssertEQ(errDecrypt, nil)
    52  	})
    53  
    54  	gtest.Case(t, func() {
    55  		key := []byte("11111111")
    56  		text := []byte("12345678")
    57  		padding := gdes.PKCS5PADDING
    58  		errPadding := 5
    59  		result := "858b176da8b12503ad6a88b4fa37833d"
    60  		cipherText, err := gdes.DesECBEncrypt(key, text, padding)
    61  		gtest.AssertEQ(err, nil)
    62  		gtest.AssertEQ(hex.EncodeToString(cipherText), result)
    63  		// decrypt test
    64  		clearText, err := gdes.DesECBDecrypt(key, cipherText, padding)
    65  		gtest.AssertEQ(err, nil)
    66  		gtest.AssertEQ(string(clearText), "12345678")
    67  
    68  		// err test
    69  		errEncrypt, err := gdes.DesECBEncrypt(key, text, errPadding)
    70  		gtest.AssertNE(err, nil)
    71  		gtest.AssertEQ(errEncrypt, nil)
    72  		errDecrypt, err := gdes.DesECBDecrypt(errKey, cipherText, padding)
    73  		gtest.AssertNE(err, nil)
    74  		gtest.AssertEQ(errDecrypt, nil)
    75  	})
    76  }
    77  
    78  func Test3DesECB(t *testing.T) {
    79  	gtest.Case(t, func() {
    80  		key := []byte("1111111111111234")
    81  		text := []byte("1234567812345678")
    82  		padding := gdes.NOPADDING
    83  		result := "a23ee24b98c26263a23ee24b98c26263"
    84  		// encrypt test
    85  		cipherText, err := gdes.TripleDesECBEncrypt(key, text, padding)
    86  		gtest.AssertEQ(err, nil)
    87  		gtest.AssertEQ(hex.EncodeToString(cipherText), result)
    88  		// decrypt test
    89  		clearText, err := gdes.TripleDesECBDecrypt(key, cipherText, padding)
    90  		gtest.AssertEQ(err, nil)
    91  		gtest.AssertEQ(string(clearText), "1234567812345678")
    92  		// err test
    93  		errEncrypt, err := gdes.DesECBEncrypt(key, text, errPadding)
    94  		gtest.AssertNE(err, nil)
    95  		gtest.AssertEQ(errEncrypt, nil)
    96  	})
    97  
    98  	gtest.Case(t, func() {
    99  		key := []byte("111111111111123412345678")
   100  		text := []byte("123456789")
   101  		padding := gdes.PKCS5PADDING
   102  		errPadding := 5
   103  		result := "37989b1effc07a6d00ff89a7d052e79f"
   104  		// encrypt test
   105  		cipherText, err := gdes.TripleDesECBEncrypt(key, text, padding)
   106  		gtest.AssertEQ(err, nil)
   107  		gtest.AssertEQ(hex.EncodeToString(cipherText), result)
   108  		// decrypt test
   109  		clearText, err := gdes.TripleDesECBDecrypt(key, cipherText, padding)
   110  		gtest.AssertEQ(err, nil)
   111  		gtest.AssertEQ(string(clearText), "123456789")
   112  		// err test, when key is err, but text and padding is right
   113  		errEncrypt, err := gdes.TripleDesECBEncrypt(errKey, text, padding)
   114  		gtest.AssertNE(err, nil)
   115  		gtest.AssertEQ(errEncrypt, nil)
   116  		// when padding is err,but key and text is right
   117  		errEncrypt, err = gdes.TripleDesECBEncrypt(key, text, errPadding)
   118  		gtest.AssertNE(err, nil)
   119  		gtest.AssertEQ(errEncrypt, nil)
   120  		// decrypt err test,when key is err
   121  		errEncrypt, err = gdes.TripleDesECBDecrypt(errKey, text, padding)
   122  		gtest.AssertNE(err, nil)
   123  		gtest.AssertEQ(errEncrypt, nil)
   124  	})
   125  }
   126  
   127  func TestDesCBC(t *testing.T) {
   128  	gtest.Case(t, func() {
   129  		key := []byte("11111111")
   130  		text := []byte("1234567812345678")
   131  		padding := gdes.NOPADDING
   132  		iv := []byte("12345678")
   133  		result := "40826a5800608c87585ca7c9efabee47"
   134  		// encrypt test
   135  		cipherText, err := gdes.DesCBCEncrypt(key, text, iv, padding)
   136  		gtest.AssertEQ(err, nil)
   137  		gtest.AssertEQ(hex.EncodeToString(cipherText), result)
   138  		// decrypt test
   139  		clearText, err := gdes.DesCBCDecrypt(key, cipherText, iv, padding)
   140  		gtest.AssertEQ(err, nil)
   141  		gtest.AssertEQ(string(clearText), "1234567812345678")
   142  		// encrypt err test.
   143  		errEncrypt, err := gdes.DesCBCEncrypt(errKey, text, iv, padding)
   144  		gtest.AssertNE(err, nil)
   145  		gtest.AssertEQ(errEncrypt, nil)
   146  		// the iv is err
   147  		errEncrypt, err = gdes.DesCBCEncrypt(key, text, errIv, padding)
   148  		gtest.AssertNE(err, nil)
   149  		gtest.AssertEQ(errEncrypt, nil)
   150  		// the padding is err
   151  		errEncrypt, err = gdes.DesCBCEncrypt(key, text, iv, errPadding)
   152  		gtest.AssertNE(err, nil)
   153  		gtest.AssertEQ(errEncrypt, nil)
   154  		// decrypt err test. the key is err
   155  		errDecrypt, err := gdes.DesCBCDecrypt(errKey, cipherText, iv, padding)
   156  		gtest.AssertNE(err, nil)
   157  		gtest.AssertEQ(errDecrypt, nil)
   158  		// the iv is err
   159  		errDecrypt, err = gdes.DesCBCDecrypt(key, cipherText, errIv, padding)
   160  		gtest.AssertNE(err, nil)
   161  		gtest.AssertEQ(errDecrypt, nil)
   162  		// the padding is err
   163  		errDecrypt, err = gdes.DesCBCDecrypt(key, cipherText, iv, errPadding)
   164  		gtest.AssertNE(err, nil)
   165  		gtest.AssertEQ(errDecrypt, nil)
   166  	})
   167  
   168  	gtest.Case(t, func() {
   169  		key := []byte("11111111")
   170  		text := []byte("12345678")
   171  		padding := gdes.PKCS5PADDING
   172  		iv := []byte("12345678")
   173  		result := "40826a5800608c87100a25d86ac7c52c"
   174  		// encrypt test
   175  		cipherText, err := gdes.DesCBCEncrypt(key, text, iv, padding)
   176  		gtest.AssertEQ(err, nil)
   177  		gtest.AssertEQ(hex.EncodeToString(cipherText), result)
   178  		// decrypt test
   179  		clearText, err := gdes.DesCBCDecrypt(key, cipherText, iv, padding)
   180  		gtest.AssertEQ(err, nil)
   181  		gtest.AssertEQ(string(clearText), "12345678")
   182  		// err test
   183  		errEncrypt, err := gdes.DesCBCEncrypt(key, text, errIv, padding)
   184  		gtest.AssertNE(err, nil)
   185  		gtest.AssertEQ(errEncrypt, nil)
   186  	})
   187  }
   188  
   189  func Test3DesCBC(t *testing.T) {
   190  	gtest.Case(t, func() {
   191  		key := []byte("1111111112345678")
   192  		text := []byte("1234567812345678")
   193  		padding := gdes.NOPADDING
   194  		iv := []byte("12345678")
   195  		result := "bfde1394e265d5f738d5cab170c77c88"
   196  		// encrypt test
   197  		cipherText, err := gdes.TripleDesCBCEncrypt(key, text, iv, padding)
   198  		gtest.AssertEQ(err, nil)
   199  		gtest.AssertEQ(hex.EncodeToString(cipherText), result)
   200  		// decrypt test
   201  		clearText, err := gdes.TripleDesCBCDecrypt(key, cipherText, iv, padding)
   202  		gtest.AssertEQ(err, nil)
   203  		gtest.AssertEQ(string(clearText), "1234567812345678")
   204  		// encrypt err test
   205  		errEncrypt, err := gdes.TripleDesCBCEncrypt(errKey, text, iv, padding)
   206  		gtest.AssertNE(err, nil)
   207  		gtest.AssertEQ(errEncrypt, nil)
   208  		// the iv is err
   209  		errEncrypt, err = gdes.TripleDesCBCEncrypt(key, text, errIv, padding)
   210  		gtest.AssertNE(err, nil)
   211  		gtest.AssertEQ(errEncrypt, nil)
   212  		// the padding is err
   213  		errEncrypt, err = gdes.TripleDesCBCEncrypt(key, text, iv, errPadding)
   214  		gtest.AssertNE(err, nil)
   215  		gtest.AssertEQ(errEncrypt, nil)
   216  		// decrypt err test
   217  		errDecrypt, err := gdes.TripleDesCBCDecrypt(errKey, cipherText, iv, padding)
   218  		gtest.AssertNE(err, nil)
   219  		gtest.AssertEQ(errDecrypt, nil)
   220  		// the iv is err
   221  		errDecrypt, err = gdes.TripleDesCBCDecrypt(key, cipherText, errIv, padding)
   222  		gtest.AssertNE(err, nil)
   223  		gtest.AssertEQ(errDecrypt, nil)
   224  		// the padding is err
   225  		errDecrypt, err = gdes.TripleDesCBCDecrypt(key, cipherText, iv, errPadding)
   226  		gtest.AssertNE(err, nil)
   227  		gtest.AssertEQ(errDecrypt, nil)
   228  	})
   229  	gtest.Case(t, func() {
   230  		key := []byte("111111111234567812345678")
   231  		text := []byte("12345678")
   232  		padding := gdes.PKCS5PADDING
   233  		iv := []byte("12345678")
   234  		result := "40826a5800608c87100a25d86ac7c52c"
   235  		// encrypt test
   236  		cipherText, err := gdes.TripleDesCBCEncrypt(key, text, iv, padding)
   237  		gtest.AssertEQ(err, nil)
   238  		gtest.AssertEQ(hex.EncodeToString(cipherText), result)
   239  		// decrypt test
   240  		clearText, err := gdes.TripleDesCBCDecrypt(key, cipherText, iv, padding)
   241  		gtest.AssertEQ(err, nil)
   242  		gtest.AssertEQ(string(clearText), "12345678")
   243  	})
   244  
   245  }