You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
naza/pkg/crypto/pkcs.go

45 lines
1.0 KiB
Go

4 years ago
// Copyright 2021, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package crypto
import (
"bytes"
"errors"
)
var ErrPKCS = errors.New("naza.crypto: fxxk")
// @param blockSize 取值范围[0, 255]
// 如果是AES见标准库中aes.BlockSize等于16
func EncryptPKCS7(in []byte, blockSize int) []byte {
paddingLength := blockSize - len(in)%blockSize
paddingBuf := bytes.Repeat([]byte{byte(paddingLength)}, paddingLength)
return append(in, paddingBuf...)
}
func DecryptPKCS7(in []byte) ([]byte, error) {
totalLength := len(in)
if totalLength < 1 {
return nil, ErrPKCS
}
paddingLength := int(in[totalLength-1])
if totalLength < paddingLength {
return nil, ErrPKCS
}
return in[:totalLength-int(paddingLength)], nil
}
func EncryptPKCS5(in []byte) []byte {
return EncryptPKCS7(in, 8)
}
func DecryptPKCS5(in []byte) ([]byte, error) {
return DecryptPKCS7(in)
}