From eb99f5ebfeea8a15516d2f50c313ac137fc07efb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=B7=E4=BA=AE?= <739476267@qq.com> Date: Mon, 6 Nov 2023 19:20:07 +0800 Subject: [PATCH] rename function name `PKCS5UnPadding` to `PKCS7UnPadding` (#3124) --- crypto/gaes/gaes.go | 37 +++++++++++++++++++++++++++------ crypto/gaes/gaes_z_unit_test.go | 20 +++++++++++++++++- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/crypto/gaes/gaes.go b/crypto/gaes/gaes.go index 4e7e5e4f6dc..acaea665d1a 100644 --- a/crypto/gaes/gaes.go +++ b/crypto/gaes/gaes.go @@ -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" @@ -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] @@ -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 { @@ -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:] diff --git a/crypto/gaes/gaes_z_unit_test.go b/crypto/gaes/gaes_z_unit_test.go index 052b3973143..ca5a9a706e2 100644 --- a/crypto/gaes/gaes_z_unit_test.go +++ b/crypto/gaes/gaes_z_unit_test.go @@ -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") }) } @@ -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) {