Skip to content

Commit

Permalink
optimize crypto code
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSen-qn committed Aug 30, 2023
1 parent f1927f5 commit 95cd61d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion cmd_test/aa_pre_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ package cmd

import (
"fmt"
"testing"

"github.com/qiniu/qshell/v2/cmd_test/test"
"github.com/qiniu/qshell/v2/iqshell/common/data"
"testing"
)

func TestCmd(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions cmd_test/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
package cmd

import (
"github.com/qiniu/qshell/v2/cmd"
"github.com/qiniu/qshell/v2/cmd_test/test"
"os"
"testing"

"github.com/qiniu/qshell/v2/cmd"
"github.com/qiniu/qshell/v2/cmd_test/test"
)

var accessKey = test.AccessKey
Expand Down
8 changes: 8 additions & 0 deletions iqshell/common/utils/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/cipher"
"crypto/md5"
"encoding/hex"

"github.com/qiniu/qshell/v2/iqshell/common/data"
)

Expand Down Expand Up @@ -42,6 +43,9 @@ func AesDecrypt(crypted, key []byte) ([]byte, *data.CodeError) {

blockMode.CryptBlocks(origData, crypted)
origData = PKCS5UnPadding(origData)
if len(origData) == 0 {
return nil, data.NewEmptyError().AppendDesc("data format error")
}
return origData, nil
}

Expand All @@ -56,5 +60,9 @@ func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
if unpadding >= length {
return nil
}

return origData[:(length - unpadding)]
}

0 comments on commit 95cd61d

Please sign in to comment.