Skip to content

Commit

Permalink
rename function name PKCS5UnPadding to PKCS7UnPadding (gogf#3124)
Browse files Browse the repository at this point in the history
  • Loading branch information
hailaz authored Nov 6, 2023
1 parent 007fe0e commit eb99f5e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
37 changes: 31 additions & 6 deletions crypto/gaes/gaes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"bytes"
"crypto/aes"
"crypto/cipher"
"fmt"

"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
Expand Down Expand Up @@ -41,7 +42,7 @@ func EncryptCBC(plainText []byte, key []byte, iv ...[]byte) ([]byte, error) {
return nil, err
}
blockSize := block.BlockSize()
plainText = PKCS5Padding(plainText, blockSize)
plainText = PKCS7Padding(plainText, blockSize)
ivValue := ([]byte)(nil)
if len(iv) > 0 {
ivValue = iv[0]
Expand Down Expand Up @@ -80,23 +81,47 @@ func DecryptCBC(cipherText []byte, key []byte, iv ...[]byte) ([]byte, error) {
blockModel := cipher.NewCBCDecrypter(block, ivValue)
plainText := make([]byte, len(cipherText))
blockModel.CryptBlocks(plainText, cipherText)
plainText, e := PKCS5UnPadding(plainText, blockSize)
plainText, e := PKCS7UnPadding(plainText, blockSize)
if e != nil {
return nil, e
}
return plainText, nil
}

func PKCS5Padding(src []byte, blockSize int) []byte {
// PKCS5Padding applies PKCS#5 padding to the source byte slice to match the given block size.
//
// If the block size is not provided, it defaults to 8.
func PKCS5Padding(src []byte, blockSize ...int) []byte {
blockSizeTemp := 8
if len(blockSize) > 0 {
blockSizeTemp = blockSize[0]
}
return PKCS7Padding(src, blockSizeTemp)
}

// PKCS5UnPadding removes PKCS#5 padding from the source byte slice based on the given block size.
//
// If the block size is not provided, it defaults to 8.
func PKCS5UnPadding(src []byte, blockSize ...int) ([]byte, error) {
blockSizeTemp := 8
if len(blockSize) > 0 {
blockSizeTemp = blockSize[0]
}
return PKCS7UnPadding(src, blockSizeTemp)
}

// PKCS7Padding applies PKCS#7 padding to the source byte slice to match the given block size.
func PKCS7Padding(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}

func PKCS5UnPadding(src []byte, blockSize int) ([]byte, error) {
// PKCS7UnPadding removes PKCS#7 padding from the source byte slice based on the given block size.
func PKCS7UnPadding(src []byte, blockSize int) ([]byte, error) {
length := len(src)
if blockSize <= 0 {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "invalid blocklen")
return nil, gerror.NewCode(gcode.CodeInvalidParameter, fmt.Sprintf("invalid blockSize: %d", blockSize))
}

if length%blockSize != 0 || length == 0 {
Expand All @@ -105,7 +130,7 @@ func PKCS5UnPadding(src []byte, blockSize int) ([]byte, error) {

unpadding := int(src[length-1])
if unpadding > blockSize || unpadding == 0 {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "invalid padding")
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "invalid unpadding")
}

padding := src[length-unpadding:]
Expand Down
20 changes: 19 additions & 1 deletion crypto/gaes/gaes_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestDecrypt(t *testing.T) {
t.Assert(decrypt, content)

decrypt, err = gaes.Decrypt([]byte(content_32_iv), keys, iv)
t.Assert(err, "invalid padding")
t.Assert(err, "invalid unpadding")
})
}

Expand Down Expand Up @@ -128,6 +128,24 @@ func TestPKCS5UnPaddingErr(t *testing.T) {
_, err = gaes.PKCS5UnPadding(key_32_err, 32)
t.AssertNE(err, nil)
})

gtest.C(t, func(t *gtest.T) {
// PKCS7UnPadding blockSize zero
_, err := gaes.PKCS7UnPadding(content, 0)
t.AssertNE(err, nil)

// PKCS7UnPadding src len zero
_, err = gaes.PKCS7UnPadding([]byte(""), 16)
t.AssertNE(err, nil)

// PKCS7UnPadding src len > blockSize
_, err = gaes.PKCS7UnPadding(key_17, 16)
t.AssertNE(err, nil)

// PKCS7UnPadding src len > blockSize
_, err = gaes.PKCS7UnPadding(key_32_err, 32)
t.AssertNE(err, nil)
})
}

func TestEncryptCFB(t *testing.T) {
Expand Down

0 comments on commit eb99f5e

Please sign in to comment.