github.com/gogf/gf/v2@v2.7.4/crypto/gdes/gdes_z_unit_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). 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/gogf/gf. 6 7 package gdes_test 8 9 import ( 10 "encoding/hex" 11 "testing" 12 13 "github.com/gogf/gf/v2/crypto/gdes" 14 "github.com/gogf/gf/v2/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.C(t, func(t *gtest.T) { 25 key := []byte("11111111") 26 text := []byte("12345678") 27 padding := gdes.NOPADDING 28 result := "858b176da8b12503" 29 // encrypt test 30 cipherText, err := gdes.EncryptECB(text, key, padding) 31 t.AssertEQ(err, nil) 32 t.AssertEQ(hex.EncodeToString(cipherText), result) 33 // decrypt test 34 clearText, err := gdes.DecryptECB(cipherText, key, padding) 35 t.AssertEQ(err, nil) 36 t.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.EncryptECB(text, key, errPadding) 40 t.AssertNE(err, nil) 41 t.AssertEQ(errEncrypt, nil) 42 errEncrypt, err = gdes.EncryptECB(text, errKey, padding) 43 t.AssertNE(err, nil) 44 t.AssertEQ(errEncrypt, nil) 45 // err decrypt test. 46 errDecrypt, err := gdes.DecryptECB(cipherText, errKey, padding) 47 t.AssertNE(err, nil) 48 t.AssertEQ(errDecrypt, nil) 49 errDecrypt, err = gdes.DecryptECB(cipherText, key, errPadding) 50 t.AssertNE(err, nil) 51 t.AssertEQ(errDecrypt, nil) 52 }) 53 54 gtest.C(t, func(t *gtest.T) { 55 key := []byte("11111111") 56 text := []byte("12345678") 57 padding := gdes.PKCS5PADDING 58 errPadding := 5 59 result := "858b176da8b12503ad6a88b4fa37833d" 60 cipherText, err := gdes.EncryptECB(text, key, padding) 61 t.AssertEQ(err, nil) 62 t.AssertEQ(hex.EncodeToString(cipherText), result) 63 // decrypt test 64 clearText, err := gdes.DecryptECB(cipherText, key, padding) 65 t.AssertEQ(err, nil) 66 t.AssertEQ(string(clearText), "12345678") 67 68 // err test 69 errEncrypt, err := gdes.EncryptECB(text, key, errPadding) 70 t.AssertNE(err, nil) 71 t.AssertEQ(errEncrypt, nil) 72 errDecrypt, err := gdes.DecryptECB(cipherText, errKey, padding) 73 t.AssertNE(err, nil) 74 t.AssertEQ(errDecrypt, nil) 75 }) 76 } 77 78 func Test3DesECB(t *testing.T) { 79 gtest.C(t, func(t *gtest.T) { 80 key := []byte("1111111111111234") 81 text := []byte("1234567812345678") 82 padding := gdes.NOPADDING 83 result := "a23ee24b98c26263a23ee24b98c26263" 84 // encrypt test 85 cipherText, err := gdes.EncryptECBTriple(text, key, padding) 86 t.AssertEQ(err, nil) 87 t.AssertEQ(hex.EncodeToString(cipherText), result) 88 // decrypt test 89 clearText, err := gdes.DecryptECBTriple(cipherText, key, padding) 90 t.AssertEQ(err, nil) 91 t.AssertEQ(string(clearText), "1234567812345678") 92 // err test 93 errEncrypt, err := gdes.EncryptECB(text, key, errPadding) 94 t.AssertNE(err, nil) 95 t.AssertEQ(errEncrypt, nil) 96 }) 97 98 gtest.C(t, func(t *gtest.T) { 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.EncryptECBTriple(text, key, padding) 106 t.AssertEQ(err, nil) 107 t.AssertEQ(hex.EncodeToString(cipherText), result) 108 // decrypt test 109 clearText, err := gdes.DecryptECBTriple(cipherText, key, padding) 110 t.AssertEQ(err, nil) 111 t.AssertEQ(string(clearText), "123456789") 112 // err test, when key is err, but text and padding is right 113 errEncrypt, err := gdes.EncryptECBTriple(text, errKey, padding) 114 t.AssertNE(err, nil) 115 t.AssertEQ(errEncrypt, nil) 116 // when padding is err,but key and text is right 117 errEncrypt, err = gdes.EncryptECBTriple(text, key, errPadding) 118 t.AssertNE(err, nil) 119 t.AssertEQ(errEncrypt, nil) 120 // decrypt err test,when key is err 121 errEncrypt, err = gdes.DecryptECBTriple(text, errKey, padding) 122 t.AssertNE(err, nil) 123 t.AssertEQ(errEncrypt, nil) 124 }) 125 } 126 127 func TestDesCBC(t *testing.T) { 128 gtest.C(t, func(t *gtest.T) { 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.EncryptCBC(text, key, iv, padding) 136 t.AssertEQ(err, nil) 137 t.AssertEQ(hex.EncodeToString(cipherText), result) 138 // decrypt test 139 clearText, err := gdes.DecryptCBC(cipherText, key, iv, padding) 140 t.AssertEQ(err, nil) 141 t.AssertEQ(string(clearText), "1234567812345678") 142 // encrypt err test. 143 errEncrypt, err := gdes.EncryptCBC(text, errKey, iv, padding) 144 t.AssertNE(err, nil) 145 t.AssertEQ(errEncrypt, nil) 146 // the iv is err 147 errEncrypt, err = gdes.EncryptCBC(text, key, errIv, padding) 148 t.AssertNE(err, nil) 149 t.AssertEQ(errEncrypt, nil) 150 // the padding is err 151 errEncrypt, err = gdes.EncryptCBC(text, key, iv, errPadding) 152 t.AssertNE(err, nil) 153 t.AssertEQ(errEncrypt, nil) 154 // decrypt err test. the key is err 155 errDecrypt, err := gdes.DecryptCBC(cipherText, errKey, iv, padding) 156 t.AssertNE(err, nil) 157 t.AssertEQ(errDecrypt, nil) 158 // the iv is err 159 errDecrypt, err = gdes.DecryptCBC(cipherText, key, errIv, padding) 160 t.AssertNE(err, nil) 161 t.AssertEQ(errDecrypt, nil) 162 // the padding is err 163 errDecrypt, err = gdes.DecryptCBC(cipherText, key, iv, errPadding) 164 t.AssertNE(err, nil) 165 t.AssertEQ(errDecrypt, nil) 166 }) 167 168 gtest.C(t, func(t *gtest.T) { 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.EncryptCBC(text, key, iv, padding) 176 t.AssertEQ(err, nil) 177 t.AssertEQ(hex.EncodeToString(cipherText), result) 178 // decrypt test 179 clearText, err := gdes.DecryptCBC(cipherText, key, iv, padding) 180 t.AssertEQ(err, nil) 181 t.AssertEQ(string(clearText), "12345678") 182 // err test 183 errEncrypt, err := gdes.EncryptCBC(text, key, errIv, padding) 184 t.AssertNE(err, nil) 185 t.AssertEQ(errEncrypt, nil) 186 }) 187 } 188 189 func Test3DesCBC(t *testing.T) { 190 gtest.C(t, func(t *gtest.T) { 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.EncryptCBCTriple(text, key, iv, padding) 198 t.AssertEQ(err, nil) 199 t.AssertEQ(hex.EncodeToString(cipherText), result) 200 // decrypt test 201 clearText, err := gdes.DecryptCBCTriple(cipherText, key, iv, padding) 202 t.AssertEQ(err, nil) 203 t.AssertEQ(string(clearText), "1234567812345678") 204 // encrypt err test 205 errEncrypt, err := gdes.EncryptCBCTriple(text, errKey, iv, padding) 206 t.AssertNE(err, nil) 207 t.AssertEQ(errEncrypt, nil) 208 // the iv is err 209 errEncrypt, err = gdes.EncryptCBCTriple(text, key, errIv, padding) 210 t.AssertNE(err, nil) 211 t.AssertEQ(errEncrypt, nil) 212 // the padding is err 213 errEncrypt, err = gdes.EncryptCBCTriple(text, key, iv, errPadding) 214 t.AssertNE(err, nil) 215 t.AssertEQ(errEncrypt, nil) 216 // decrypt err test 217 errDecrypt, err := gdes.DecryptCBCTriple(cipherText, errKey, iv, padding) 218 t.AssertNE(err, nil) 219 t.AssertEQ(errDecrypt, nil) 220 // the iv is err 221 errDecrypt, err = gdes.DecryptCBCTriple(cipherText, key, errIv, padding) 222 t.AssertNE(err, nil) 223 t.AssertEQ(errDecrypt, nil) 224 // the padding is err 225 errDecrypt, err = gdes.DecryptCBCTriple(cipherText, key, iv, errPadding) 226 t.AssertNE(err, nil) 227 t.AssertEQ(errDecrypt, nil) 228 }) 229 gtest.C(t, func(t *gtest.T) { 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.EncryptCBCTriple(text, key, iv, padding) 237 t.AssertEQ(err, nil) 238 t.AssertEQ(hex.EncodeToString(cipherText), result) 239 // decrypt test 240 clearText, err := gdes.DecryptCBCTriple(cipherText, key, iv, padding) 241 t.AssertEQ(err, nil) 242 t.AssertEQ(string(clearText), "12345678") 243 }) 244 245 }