Skip to content

Commit

Permalink
Remove useless type conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Nov 22, 2024
1 parent 81d5ba8 commit 42d5ae3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion 3des_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,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)))
e := Decrypt.FromHexBytes([]byte(test.toHex)).By3Des(getCipher(test.mode, test.padding, tripleDesKey, tripleDesIV))

t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_hex_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
Expand Down
12 changes: 6 additions & 6 deletions blowfish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestBlowfish_Decrypt_String(t *testing.T) {
func TestBlowfish_Encrypt_Bytes(t *testing.T) {
for index, test := range blowfishTests {
raw := Decode.FromBytes([]byte(test.toHex)).ByHex().ToBytes()
e := Encrypt.FromBytes([]byte(test.input)).ByBlowfish(getCipher(test.mode, test.padding, []byte(blowfishKey), []byte(blowfishIV)))
e := Encrypt.FromBytes([]byte(test.input)).ByBlowfish(getCipher(test.mode, test.padding, blowfishKey, blowfishIV))

t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_raw_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
Expand All @@ -141,7 +141,7 @@ func TestBlowfish_Encrypt_Bytes(t *testing.T) {
func TestBlowfish_Decrypt_Bytes(t *testing.T) {
for index, test := range blowfishTests {
raw := Decode.FromBytes([]byte(test.toHex)).ByHex().ToBytes()
e := Decrypt.FromRawBytes(raw).ByBlowfish(getCipher(test.mode, test.padding, []byte(blowfishKey), []byte(blowfishIV)))
e := Decrypt.FromRawBytes(raw).ByBlowfish(getCipher(test.mode, test.padding, blowfishKey, blowfishIV))

t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_raw_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
Expand All @@ -150,7 +150,7 @@ func TestBlowfish_Decrypt_Bytes(t *testing.T) {
}

for index, test := range blowfishTests {
e := Decrypt.FromHexBytes([]byte(test.toHex)).ByBlowfish(getCipher(test.mode, test.padding, []byte(blowfishKey), []byte(blowfishIV)))
e := Decrypt.FromHexBytes([]byte(test.toHex)).ByBlowfish(getCipher(test.mode, test.padding, blowfishKey, blowfishIV))

t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_hex_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
Expand All @@ -159,7 +159,7 @@ func TestBlowfish_Decrypt_Bytes(t *testing.T) {
}

for index, test := range blowfishTests {
e := Decrypt.FromBase64Bytes([]byte(test.toBase64)).ByBlowfish(getCipher(test.mode, test.padding, []byte(blowfishKey), []byte(blowfishIV)))
e := Decrypt.FromBase64Bytes([]byte(test.toBase64)).ByBlowfish(getCipher(test.mode, test.padding, blowfishKey, blowfishIV))

t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_base64_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
Expand Down Expand Up @@ -203,11 +203,11 @@ func TestBlowfish_Decoding_Error(t *testing.T) {

d1 := Decrypt.FromHexString("xxxx").ByBlowfish(getCipher(CTR, Zero, blowfishKey, blowfishIV))
assert.Equal(t, err.ModeError("hex"), d1.Error)
d2 := Decrypt.FromHexBytes([]byte("xxxx")).ByBlowfish(getCipher(CTR, Zero, []byte(blowfishKey), []byte(blowfishIV)))
d2 := Decrypt.FromHexBytes([]byte("xxxx")).ByBlowfish(getCipher(CTR, Zero, blowfishKey, blowfishIV))
assert.Equal(t, err.ModeError("hex"), d2.Error)

d3 := Decrypt.FromBase64String("xxxxxx").ByBlowfish(getCipher(CFB, PKCS7, blowfishKey, blowfishIV))
assert.Equal(t, err.ModeError("base64"), d3.Error)
d4 := Decrypt.FromBase64Bytes([]byte("xxxxxx")).ByBlowfish(getCipher(CFB, PKCS7, []byte(blowfishKey), []byte(blowfishIV)))
d4 := Decrypt.FromBase64Bytes([]byte("xxxxxx")).ByBlowfish(getCipher(CFB, PKCS7, blowfishKey, blowfishIV))
assert.Equal(t, err.ModeError("base64"), d4.Error)
}
4 changes: 2 additions & 2 deletions cipher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestCipher_Encrypt(t *testing.T) {
cipher.SetMode(CBC)
cipher.SetPadding(PKCS7)

block, err1 := aes.NewCipher([]byte(aesKey))
block, err1 := aes.NewCipher(aesKey)
assert.Nil(t, err1)
dst, err2 := cipher.Encrypt([]byte(""), block)
assert.Nil(t, err2)
Expand All @@ -24,7 +24,7 @@ func TestCipher_Decrypt(t *testing.T) {
cipher.SetMode(CBC)
cipher.SetPadding(PKCS7)

block, err1 := aes.NewCipher([]byte(aesKey))
block, err1 := aes.NewCipher(aesKey)
assert.Nil(t, err1)
dst, err2 := cipher.Decrypt([]byte(""), block)
assert.Nil(t, err2)
Expand Down
12 changes: 6 additions & 6 deletions des_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestDes_Decrypt_String(t *testing.T) {
func TestDes_Encrypt_Bytes(t *testing.T) {
for index, test := range desTests {
raw := Decode.FromBytes([]byte(test.toHex)).ByHex().ToBytes()
e := Encrypt.FromBytes([]byte(test.input)).ByDes(getCipher(test.mode, test.padding, []byte(desKey), []byte(desIV)))
e := Encrypt.FromBytes([]byte(test.input)).ByDes(getCipher(test.mode, test.padding, desKey, desIV))

t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_raw_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
Expand All @@ -141,7 +141,7 @@ func TestDes_Encrypt_Bytes(t *testing.T) {
func TestDes_Decrypt_Bytes(t *testing.T) {
for index, test := range desTests {
raw := Decode.FromBytes([]byte(test.toHex)).ByHex().ToBytes()
e := Decrypt.FromRawBytes(raw).ByDes(getCipher(test.mode, test.padding, []byte(desKey), []byte(desIV)))
e := Decrypt.FromRawBytes(raw).ByDes(getCipher(test.mode, test.padding, desKey, desIV))

t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_raw_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
Expand All @@ -150,7 +150,7 @@ func TestDes_Decrypt_Bytes(t *testing.T) {
}

for index, test := range desTests {
e := Decrypt.FromHexBytes([]byte(test.toHex)).ByDes(getCipher(test.mode, test.padding, []byte(desKey), []byte(desIV)))
e := Decrypt.FromHexBytes([]byte(test.toHex)).ByDes(getCipher(test.mode, test.padding, desKey, desIV))

t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_hex_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
Expand All @@ -159,7 +159,7 @@ func TestDes_Decrypt_Bytes(t *testing.T) {
}

for index, test := range desTests {
e := Decrypt.FromBase64Bytes([]byte(test.toBase64)).ByDes(getCipher(test.mode, test.padding, []byte(desKey), []byte(desIV)))
e := Decrypt.FromBase64Bytes([]byte(test.toBase64)).ByDes(getCipher(test.mode, test.padding, desKey, desIV))

t.Run(fmt.Sprintf(string(test.mode)+"_"+string(test.padding)+"_base64_test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
Expand Down Expand Up @@ -203,9 +203,9 @@ func TestDes_Src_Error(t *testing.T) {
func TestDes_Decoding_Error(t *testing.T) {
err := NewDecodeError()

d1 := Decrypt.FromHexBytes([]byte("xxxx")).ByDes(getCipher(CTR, Zero, []byte(desKey), []byte(desIV)))
d1 := Decrypt.FromHexBytes([]byte("xxxx")).ByDes(getCipher(CTR, Zero, desKey, desIV))
assert.Equal(t, err.ModeError("hex"), d1.Error)

d2 := Decrypt.FromBase64Bytes([]byte("xxxxxx")).ByDes(getCipher(CFB, PKCS7, []byte(desKey), []byte(desIV)))
d2 := Decrypt.FromBase64Bytes([]byte("xxxxxx")).ByDes(getCipher(CFB, PKCS7, desKey, desIV))
assert.Equal(t, err.ModeError("base64"), d2.Error)
}
8 changes: 4 additions & 4 deletions tea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ func TestTea_Decrypt_String(t *testing.T) {

func TestTea_Encrypt_Bytes(t *testing.T) {
for index, test := range teaTests {
e := Encrypt.FromBytes([]byte(test.input)).ByTea([]byte(test.key), test.rounds)
e := Encrypt.FromBytes([]byte(test.input)).ByTea(test.key, test.rounds)
if test.rounds == 0 {
e = Encrypt.FromBytes([]byte(test.input)).ByTea([]byte(test.key))
e = Encrypt.FromBytes([]byte(test.input)).ByTea(test.key)
}
assert.Nil(t, e.Error)
assert.Equal(t, []byte(test.toHex), e.ToHexBytes(), "Hex test test index is "+strconv.Itoa(index))
Expand All @@ -80,9 +80,9 @@ func TestTea_Decrypt_Bytes(t *testing.T) {
assert.Nil(t, e1.Error)
assert.Equal(t, []byte(test.input), e1.ToBytes(), "Hex test index is "+strconv.Itoa(index))

e2 := Decrypt.FromBase64Bytes([]byte(test.toBase64)).ByTea([]byte(test.key), test.rounds)
e2 := Decrypt.FromBase64Bytes([]byte(test.toBase64)).ByTea(test.key, test.rounds)
if test.rounds == 0 {
e2 = Decrypt.FromBase64Bytes([]byte(test.toBase64)).ByTea([]byte(test.key))
e2 = Decrypt.FromBase64Bytes([]byte(test.toBase64)).ByTea(test.key)
}
assert.Nil(t, e2.Error)
assert.Equal(t, []byte(test.input), e2.ToBytes(), "base64 test index is "+strconv.Itoa(index))
Expand Down

0 comments on commit 42d5ae3

Please sign in to comment.