Skip to content

Commit

Permalink
Merge from golang-module/develop
Browse files Browse the repository at this point in the history
v0.2.2
  • Loading branch information
gouguoyin authored Dec 15, 2022
2 parents 0d6a17f + 0f058f1 commit d6e4fb5
Show file tree
Hide file tree
Showing 33 changed files with 907 additions and 437 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ name: "analysis"

on:
push:
branches: [ main ]
branches: [ develop ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
branches: [ develop ]
schedule:
- cron: '18 19 * * 6'

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: test

on:
push:
branches: [ main ]
branches: [ develop ]
pull_request:
branches: [ main ]
branches: [ develop ]

jobs:

Expand Down
4 changes: 2 additions & 2 deletions 3des.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// By3Des encrypts by 3des.
// 通过 3des 加密
func (e encrypter) By3Des(c *Cipher) encrypter {
func (e Encrypter) By3Des(c *Cipher) Encrypter {
block, err := des.NewTripleDESCipher(c.key)
if err != nil {
e.Error = invalid3DesKeyError()
Expand All @@ -26,7 +26,7 @@ func (e encrypter) By3Des(c *Cipher) encrypter {

// By3Des decrypts by 3des.
// 通过 3des 解密
func (d decrypter) By3Des(c *Cipher) decrypter {
func (d Decrypter) By3Des(c *Cipher) Decrypter {
if d.Error != nil {
return d
}
Expand Down
93 changes: 51 additions & 42 deletions 3des_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,63 @@ var tripleDesTests = []struct {
padding cipherPadding
input string
toHex string
toBase32 string
toBase64 string
}{
{CBC, "", "", "", "", ""},
{CBC, No, "hello world, go!", "7e9194cc827a325d49111eaa503110fe", "P2IZJTECPIZF2SIRD2VFAMIQ7Y======", "fpGUzIJ6Ml1JER6qUDEQ/g=="},
{CBC, Zero, "hello world", "7e9194cc827a325ddaee992a89c5cd8d", "P2IZJTECPIZF3WXOTEVITRONRU======", "fpGUzIJ6Ml3a7pkqicXNjQ=="},
{CBC, PKCS5, "hello world", "7e9194cc827a325db9c765859716cc97", "P2IZJTECPIZF3OOHMWCZOFWMS4======", "fpGUzIJ6Ml25x2WFlxbMlw=="},
{CBC, PKCS7, "hello world", "7e9194cc827a325db9c765859716cc97", "P2IZJTECPIZF3OOHMWCZOFWMS4======", "fpGUzIJ6Ml25x2WFlxbMlw=="},

{CFB, PKCS5, "", "", "", ""},
{CFB, No, "hello world, go!", "f52dc896ceff0ecc9393c19d4e1e7591", "6UW4RFWO74HMZE4TYGOU4HTVSE======", "9S3Ils7/DsyTk8GdTh51kQ=="},
{CFB, Zero, "hello world", "f52dc896ceff0ecc9393c1b16e791ab0", "6UW4RFWO74HMZE4TYGYW46I2WA======", "9S3Ils7/DsyTk8GxbnkasA=="},
{CFB, PKCS5, "hello world", "f52dc896ceff0ecc9393c1b46b7c1fb5", "6UW4RFWO74HMZE4TYG2GW7A7WU======", "9S3Ils7/DsyTk8G0a3wftQ=="},
{CFB, PKCS7, "hello world", "f52dc896ceff0ecc9393c1b46b7c1fb5", "6UW4RFWO74HMZE4TYG2GW7A7WU======", "9S3Ils7/DsyTk8G0a3wftQ=="},

{OFB, PKCS7, "", "", "", ""},
{OFB, No, "hello world, go!", "f52dc896ceff0eccf869cb59735c3766", "6UW4RFWO74HMZ6DJZNMXGXBXMY======", "9S3Ils7/Dsz4actZc1w3Zg=="},
{OFB, Zero, "hello world", "f52dc896ceff0eccf869cb75533b5847", "6UW4RFWO74HMZ6DJZN2VGO2YI4======", "9S3Ils7/Dsz4act1UztYRw=="},
{OFB, PKCS5, "hello world", "f52dc896ceff0eccf869cb70563e5d42", "6UW4RFWO74HMZ6DJZNYFMPS5II======", "9S3Ils7/Dsz4actwVj5dQg=="},
{OFB, PKCS7, "hello world", "f52dc896ceff0eccf869cb70563e5d42", "6UW4RFWO74HMZ6DJZNYFMPS5II======", "9S3Ils7/Dsz4actwVj5dQg=="},

{CTR, PKCS5, "", "", "", ""},
{CTR, No, "hello world, go!", "f52dc896ceff0ecc366b2281038f6f7f", "6UW4RFWO74HMYNTLEKAQHD3PP4======", "9S3Ils7/Dsw2ayKBA49vfw=="},
{CTR, Zero, "hello world", "f52dc896ceff0ecc366b22ad23e8005e", "6UW4RFWO74HMYNTLEKWSH2AALY======", "9S3Ils7/Dsw2ayKtI+gAXg=="},
{CTR, PKCS5, "hello world", "f52dc896ceff0ecc366b22a826ed055b", "6UW4RFWO74HMYNTLEKUCN3IFLM======", "9S3Ils7/Dsw2ayKoJu0FWw=="},
{CTR, PKCS7, "hello world", "f52dc896ceff0ecc366b22a826ed055b", "6UW4RFWO74HMYNTLEKUCN3IFLM======", "9S3Ils7/Dsw2ayKoJu0FWw=="},

{ECB, PKCS7, "", "", "", ""},
{ECB, No, "hello world, go!", "b8097975c76319c623be7c7aa6e0f3fc", "XAEXS5OHMMM4MI56PR5KNYHT7Q======", "uAl5dcdjGcYjvnx6puDz/A=="},
{ECB, Zero, "hello world", "b8097975c76319c61971a986e579cdf9", "XAEXS5OHMMM4MGLRVGDOK6ON7E======", "uAl5dcdjGcYZcamG5XnN+Q=="},
{ECB, PKCS5, "hello world", "b8097975c76319c6172687e0d90fd4d1", "XAEXS5OHMMM4MFZGQ7QNSD6U2E======", "uAl5dcdjGcYXJofg2Q/U0Q=="},
{ECB, PKCS7, "hello world", "b8097975c76319c6172687e0d90fd4d1", "XAEXS5OHMMM4MFZGQ7QNSD6U2E======", "uAl5dcdjGcYXJofg2Q/U0Q=="},
{CBC, PKCS7, "", "", ""},
{CBC, No, "hello world, go!", "7e9194cc827a325d49111eaa503110fe", "fpGUzIJ6Ml1JER6qUDEQ/g=="},
{CBC, Zero, "hello world", "7e9194cc827a325ddaee992a89c5cd8d", "fpGUzIJ6Ml3a7pkqicXNjQ=="},
{CBC, PKCS5, "hello world", "7e9194cc827a325db9c765859716cc97", "fpGUzIJ6Ml25x2WFlxbMlw=="},
{CBC, PKCS7, "hello world", "7e9194cc827a325db9c765859716cc97", "fpGUzIJ6Ml25x2WFlxbMlw=="},
{CBC, AnsiX923, "hello world", "7e9194cc827a325d2793bb48a7971825", "fpGUzIJ6Ml0nk7tIp5cYJQ=="},
{CBC, ISO97971, "hello world", "7e9194cc827a325d89d4f50218d6e511", "fpGUzIJ6Ml2J1PUCGNblEQ=="},

{CFB, PKCS7, "", "", ""},
{CFB, No, "hello world, go!", "f52dc896ceff0ecc9393c19d4e1e7591", "9S3Ils7/DsyTk8GdTh51kQ=="},
{CFB, Zero, "hello world", "f52dc896ceff0ecc9393c1b16e791ab0", "9S3Ils7/DsyTk8GxbnkasA=="},
{CFB, PKCS5, "hello world", "f52dc896ceff0ecc9393c1b46b7c1fb5", "9S3Ils7/DsyTk8G0a3wftQ=="},
{CFB, PKCS7, "hello world", "f52dc896ceff0ecc9393c1b46b7c1fb5", "9S3Ils7/DsyTk8G0a3wftQ=="},
{CFB, AnsiX923, "hello world", "f52dc896ceff0ecc9393c1b16e791ab5", "9S3Ils7/DsyTk8GxbnkatQ=="},
{CFB, ISO97971, "hello world", "f52dc896ceff0ecc9393c1316e791ab0", "9S3Ils7/DsyTk8ExbnkasA=="},

{OFB, PKCS7, "", "", ""},
{OFB, No, "hello world, go!", "f52dc896ceff0eccf869cb59735c3766", "9S3Ils7/Dsz4actZc1w3Zg=="},
{OFB, Zero, "hello world", "f52dc896ceff0eccf869cb75533b5847", "9S3Ils7/Dsz4act1UztYRw=="},
{OFB, PKCS5, "hello world", "f52dc896ceff0eccf869cb70563e5d42", "9S3Ils7/Dsz4actwVj5dQg=="},
{OFB, PKCS7, "hello world", "f52dc896ceff0eccf869cb70563e5d42", "9S3Ils7/Dsz4actwVj5dQg=="},
{OFB, AnsiX923, "hello world", "f52dc896ceff0eccf869cb75533b5842", "9S3Ils7/Dsz4act1UztYQg=="},
{OFB, ISO97971, "hello world", "f52dc896ceff0eccf869cbf5533b5847", "9S3Ils7/Dsz4acv1UztYRw=="},

{CTR, PKCS7, "", "", ""},
{CTR, No, "hello world, go!", "f52dc896ceff0ecc366b2281038f6f7f", "9S3Ils7/Dsw2ayKBA49vfw=="},
{CTR, Zero, "hello world", "f52dc896ceff0ecc366b22ad23e8005e", "9S3Ils7/Dsw2ayKtI+gAXg=="},
{CTR, PKCS5, "hello world", "f52dc896ceff0ecc366b22a826ed055b", "9S3Ils7/Dsw2ayKoJu0FWw=="},
{CTR, PKCS7, "hello world", "f52dc896ceff0ecc366b22a826ed055b", "9S3Ils7/Dsw2ayKoJu0FWw=="},
{CTR, AnsiX923, "hello world", "f52dc896ceff0ecc366b22ad23e8005b", "9S3Ils7/Dsw2ayKtI+gAWw=="},
{CTR, ISO97971, "hello world", "f52dc896ceff0ecc366b222d23e8005e", "9S3Ils7/Dsw2ayItI+gAXg=="},

{ECB, PKCS7, "", "", ""},
{ECB, No, "hello world, go!", "b8097975c76319c623be7c7aa6e0f3fc", "uAl5dcdjGcYjvnx6puDz/A=="},
{ECB, Zero, "hello world", "b8097975c76319c61971a986e579cdf9", "uAl5dcdjGcYZcamG5XnN+Q=="},
{ECB, PKCS5, "hello world", "b8097975c76319c6172687e0d90fd4d1", "uAl5dcdjGcYXJofg2Q/U0Q=="},
{ECB, PKCS7, "hello world", "b8097975c76319c6172687e0d90fd4d1", "uAl5dcdjGcYXJofg2Q/U0Q=="},
{ECB, AnsiX923, "hello world", "b8097975c76319c6d98a83ce5ec18698", "uAl5dcdjGcbZioPOXsGGmA=="},
{ECB, ISO97971, "hello world", "b8097975c76319c66b1c75b91028ca62", "uAl5dcdjGcZrHHW5ECjKYg=="},
}

func Test3Des_Encrypt_String(t *testing.T) {
for index, test := range tripleDesTests {
raw := Decode.FromString(test.toHex).ByHex().ToString()
e := Encrypt.FromString(test.input).By3Des(getCipher(test.mode, test.padding, tripleDesKey, tripleDesIV))

t.Run(fmt.Sprintf(string(test.mode)+"_raw_test_%d", index), func(t *testing.T) {
t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_raw_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, raw, e.ToRawString())
})
t.Run(fmt.Sprintf(string(test.mode)+"_hex_test_%d", index), func(t *testing.T) {
t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_hex_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, test.toHex, e.ToHexString())
})
t.Run(fmt.Sprintf(string(test.mode)+"_base64_test_%d", index), func(t *testing.T) {
t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_base64_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, test.toBase64, e.ToBase64String())
})
Expand All @@ -75,7 +84,7 @@ func Test3Des_Decrypt_String(t *testing.T) {
raw := Decode.FromString(test.toHex).ByHex().ToString()
e := Decrypt.FromRawString(raw).By3Des(getCipher(test.mode, test.padding, tripleDesKey, tripleDesIV))

t.Run(fmt.Sprintf(string(test.mode)+"_raw_test_%d", index), func(t *testing.T) {
t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_raw_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, test.input, e.ToString())
assert.Equal(t, test.input, fmt.Sprintf("%s", e))
Expand All @@ -85,7 +94,7 @@ func Test3Des_Decrypt_String(t *testing.T) {
for index, test := range tripleDesTests {
e := Decrypt.FromHexString(test.toHex).By3Des(getCipher(test.mode, test.padding, tripleDesKey, tripleDesIV))

t.Run(fmt.Sprintf(string(test.mode)+"_hex_test_%d", index), func(t *testing.T) {
t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_hex_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, test.input, e.ToString())
assert.Equal(t, test.input, fmt.Sprintf("%s", e))
Expand All @@ -95,7 +104,7 @@ func Test3Des_Decrypt_String(t *testing.T) {
for index, test := range tripleDesTests {
e := Decrypt.FromBase64String(test.toBase64).By3Des(getCipher(test.mode, test.padding, tripleDesKey, tripleDesIV))

t.Run(fmt.Sprintf(string(test.mode)+"_base64_test_%d", index), func(t *testing.T) {
t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_base64_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, test.input, e.ToString())
assert.Equal(t, test.input, fmt.Sprintf("%s", e))
Expand All @@ -108,15 +117,15 @@ func Test3Des_Encrypt_Bytes(t *testing.T) {
raw := Decode.FromBytes([]byte(test.toHex)).ByHex().ToBytes()
e := Encrypt.FromBytes([]byte(test.input)).By3Des(getCipher(test.mode, test.padding, []byte(tripleDesKey), []byte(tripleDesIV)))

t.Run(fmt.Sprintf(string(test.mode)+"_raw_test_%d", index), func(t *testing.T) {
t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_raw_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, raw, e.ToRawBytes())
})
t.Run(fmt.Sprintf(string(test.mode)+"_hex_test_%d", index), func(t *testing.T) {
t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_hex_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, []byte(test.toHex), e.ToHexBytes())
})
t.Run(fmt.Sprintf(string(test.mode)+"_base64_test_%d", index), func(t *testing.T) {
t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_base64_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, []byte(test.toBase64), e.ToBase64Bytes())
})
Expand All @@ -128,7 +137,7 @@ func Test3Des_Decrypt_Bytes(t *testing.T) {
raw := Decode.FromBytes([]byte(test.toHex)).ByHex().ToBytes()
e := Decrypt.FromRawBytes(raw).By3Des(getCipher(test.mode, test.padding, []byte(tripleDesKey), []byte(tripleDesIV)))

t.Run(fmt.Sprintf(string(test.mode)+"_raw_test_%d", index), func(t *testing.T) {
t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_raw_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, []byte(test.input), e.ToBytes())
})
Expand All @@ -137,7 +146,7 @@ func Test3Des_Decrypt_Bytes(t *testing.T) {
for index, test := range tripleDesTests {
e := Decrypt.FromHexBytes([]byte(test.toHex)).By3Des(getCipher(test.mode, test.padding, []byte(tripleDesKey), []byte(tripleDesIV)))

t.Run(fmt.Sprintf(string(test.mode)+"_hex_test_%d", index), func(t *testing.T) {
t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_hex_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, []byte(test.input), e.ToBytes())
})
Expand All @@ -146,7 +155,7 @@ func Test3Des_Decrypt_Bytes(t *testing.T) {
for index, test := range tripleDesTests {
e := Decrypt.FromBase64Bytes([]byte(test.toBase64)).By3Des(getCipher(test.mode, test.padding, []byte(tripleDesKey), []byte(tripleDesIV)))

t.Run(fmt.Sprintf(string(test.mode)+"_base64_test_%d", index), func(t *testing.T) {
t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_base64_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, []byte(test.input), e.ToBytes())
})
Expand Down
Loading

0 comments on commit d6e4fb5

Please sign in to comment.