fix golint issues in core/codec (#473)

master
Kevin Wan 4 years ago committed by GitHub
parent 9602494454
commit 8f1c88e07d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,6 +10,7 @@ import (
"github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/logx"
) )
// ErrPaddingSize indicates bad padding size.
var ErrPaddingSize = errors.New("padding size error") var ErrPaddingSize = errors.New("padding size error")
type ecb struct { type ecb struct {
@ -26,6 +27,7 @@ func newECB(b cipher.Block) *ecb {
type ecbEncrypter ecb type ecbEncrypter ecb
// NewECBEncrypter returns an ECB encrypter.
func NewECBEncrypter(b cipher.Block) cipher.BlockMode { func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
return (*ecbEncrypter)(newECB(b)) return (*ecbEncrypter)(newECB(b))
} }
@ -52,6 +54,7 @@ func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
type ecbDecrypter ecb type ecbDecrypter ecb
// NewECBDecrypter returns an ECB decrypter.
func NewECBDecrypter(b cipher.Block) cipher.BlockMode { func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
return (*ecbDecrypter)(newECB(b)) return (*ecbDecrypter)(newECB(b))
} }
@ -78,6 +81,7 @@ func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
} }
} }
// EcbDecrypt decrypts src with the given key.
func EcbDecrypt(key, src []byte) ([]byte, error) { func EcbDecrypt(key, src []byte) ([]byte, error) {
block, err := aes.NewCipher(key) block, err := aes.NewCipher(key)
if err != nil { if err != nil {
@ -92,6 +96,8 @@ func EcbDecrypt(key, src []byte) ([]byte, error) {
return pkcs5Unpadding(decrypted, decrypter.BlockSize()) return pkcs5Unpadding(decrypted, decrypter.BlockSize())
} }
// EcbDecryptBase64 decrypts base64 encoded src with the given base64 encoded key.
// The returned string is also base64 encoded.
func EcbDecryptBase64(key, src string) (string, error) { func EcbDecryptBase64(key, src string) (string, error) {
keyBytes, err := getKeyBytes(key) keyBytes, err := getKeyBytes(key)
if err != nil { if err != nil {
@ -111,6 +117,7 @@ func EcbDecryptBase64(key, src string) (string, error) {
return base64.StdEncoding.EncodeToString(decryptedBytes), nil return base64.StdEncoding.EncodeToString(decryptedBytes), nil
} }
// EcbEncrypt encrypts src with the given key.
func EcbEncrypt(key, src []byte) ([]byte, error) { func EcbEncrypt(key, src []byte) ([]byte, error) {
block, err := aes.NewCipher(key) block, err := aes.NewCipher(key)
if err != nil { if err != nil {
@ -126,6 +133,8 @@ func EcbEncrypt(key, src []byte) ([]byte, error) {
return crypted, nil return crypted, nil
} }
// EcbEncryptBase64 encrypts base64 encoded src with the given base64 encoded key.
// The returned string is also base64 encoded.
func EcbEncryptBase64(key, src string) (string, error) { func EcbEncryptBase64(key, src string) (string, error) {
keyBytes, err := getKeyBytes(key) keyBytes, err := getKeyBytes(key)
if err != nil { if err != nil {

@ -11,8 +11,11 @@ import (
// 2048-bit MODP Group // 2048-bit MODP Group
var ( var (
// ErrInvalidPriKey indicates the invalid private key.
ErrInvalidPriKey = errors.New("invalid private key") ErrInvalidPriKey = errors.New("invalid private key")
// ErrInvalidPubKey indicates the invalid public key.
ErrInvalidPubKey = errors.New("invalid public key") ErrInvalidPubKey = errors.New("invalid public key")
// ErrPubKeyOutOfBound indicates the public key is out of bound.
ErrPubKeyOutOfBound = errors.New("public key out of bound") ErrPubKeyOutOfBound = errors.New("public key out of bound")
p, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF", 16) p, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF", 16)
@ -20,11 +23,13 @@ var (
zero = big.NewInt(0) zero = big.NewInt(0)
) )
// DhKey defines the Diffie Hellman key.
type DhKey struct { type DhKey struct {
PriKey *big.Int PriKey *big.Int
PubKey *big.Int PubKey *big.Int
} }
// ComputeKey returns a key from public key and private key.
func ComputeKey(pubKey, priKey *big.Int) (*big.Int, error) { func ComputeKey(pubKey, priKey *big.Int) (*big.Int, error) {
if pubKey == nil { if pubKey == nil {
return nil, ErrInvalidPubKey return nil, ErrInvalidPubKey
@ -41,6 +46,7 @@ func ComputeKey(pubKey, priKey *big.Int) (*big.Int, error) {
return new(big.Int).Exp(pubKey, priKey, p), nil return new(big.Int).Exp(pubKey, priKey, p), nil
} }
// GenerateKey returns a Diffie Hellman key.
func GenerateKey() (*DhKey, error) { func GenerateKey() (*DhKey, error) {
var err error var err error
var x *big.Int var x *big.Int
@ -63,10 +69,12 @@ func GenerateKey() (*DhKey, error) {
return key, nil return key, nil
} }
// NewPublicKey returns a public key from the given bytes.
func NewPublicKey(bs []byte) *big.Int { func NewPublicKey(bs []byte) *big.Int {
return new(big.Int).SetBytes(bs) return new(big.Int).SetBytes(bs)
} }
// Bytes returns public key bytes.
func (k *DhKey) Bytes() []byte { func (k *DhKey) Bytes() []byte {
if k.PubKey == nil { if k.PubKey == nil {
return nil return nil

@ -8,6 +8,7 @@ import (
const unzipLimit = 100 * 1024 * 1024 // 100MB const unzipLimit = 100 * 1024 * 1024 // 100MB
// Gzip compresses bs.
func Gzip(bs []byte) []byte { func Gzip(bs []byte) []byte {
var b bytes.Buffer var b bytes.Buffer
@ -18,6 +19,7 @@ func Gzip(bs []byte) []byte {
return b.Bytes() return b.Bytes()
} }
// Gunzip uncompresses bs.
func Gunzip(bs []byte) ([]byte, error) { func Gunzip(bs []byte) ([]byte, error) {
r, err := gzip.NewReader(bytes.NewBuffer(bs)) r, err := gzip.NewReader(bytes.NewBuffer(bs))
if err != nil { if err != nil {

@ -7,12 +7,14 @@ import (
"io" "io"
) )
// Hmac returns HMAC bytes for body with the given key.
func Hmac(key []byte, body string) []byte { func Hmac(key []byte, body string) []byte {
h := hmac.New(sha256.New, key) h := hmac.New(sha256.New, key)
io.WriteString(h, body) io.WriteString(h, body)
return h.Sum(nil) return h.Sum(nil)
} }
// HmacBase64 returns the base64 encoded string of HMAC for body with the given key.
func HmacBase64(key []byte, body string) string { func HmacBase64(key []byte, body string) string {
return base64.StdEncoding.EncodeToString(Hmac(key, body)) return base64.StdEncoding.EncodeToString(Hmac(key, body))
} }

@ -11,17 +11,22 @@ import (
) )
var ( var (
// ErrPrivateKey indicates the invalid private key.
ErrPrivateKey = errors.New("private key error") ErrPrivateKey = errors.New("private key error")
// ErrPublicKey indicates the invalid public key.
ErrPublicKey = errors.New("failed to parse PEM block containing the public key") ErrPublicKey = errors.New("failed to parse PEM block containing the public key")
// ErrNotRsaKey indicates the invalid RSA key.
ErrNotRsaKey = errors.New("key type is not RSA") ErrNotRsaKey = errors.New("key type is not RSA")
) )
type ( type (
// RsaDecrypter represents a RSA decrypter.
RsaDecrypter interface { RsaDecrypter interface {
Decrypt(input []byte) ([]byte, error) Decrypt(input []byte) ([]byte, error)
DecryptBase64(input string) ([]byte, error) DecryptBase64(input string) ([]byte, error)
} }
// RsaEncrypter represents a RSA encrypter.
RsaEncrypter interface { RsaEncrypter interface {
Encrypt(input []byte) ([]byte, error) Encrypt(input []byte) ([]byte, error)
} }
@ -41,6 +46,7 @@ type (
} }
) )
// NewRsaDecrypter returns a RsaDecrypter with the given file.
func NewRsaDecrypter(file string) (RsaDecrypter, error) { func NewRsaDecrypter(file string) (RsaDecrypter, error) {
content, err := ioutil.ReadFile(file) content, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
@ -84,6 +90,7 @@ func (r *rsaDecrypter) DecryptBase64(input string) ([]byte, error) {
return r.Decrypt(base64Decoded) return r.Decrypt(base64Decoded)
} }
// NewRsaEncrypter returns a RsaEncrypter with the given key.
func NewRsaEncrypter(key []byte) (RsaEncrypter, error) { func NewRsaEncrypter(key []byte) (RsaEncrypter, error) {
block, _ := pem.Decode(key) block, _ := pem.Decode(key)
if block == nil { if block == nil {

Loading…
Cancel
Save