From fc09e49f3846d16ffdd34a654de159cbde34d7ef Mon Sep 17 00:00:00 2001 From: deatil <2217957370@qq.com> Date: Sat, 4 Jan 2025 12:04:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkcs8/pbes1/pkcs8.go | 8 ++++---- pkcs8/pbes2/kdf_pbkdf2.go | 2 +- pkcs8/pbes2/kdf_smpbkdf2.go | 2 +- pkcs8/pbes2/pkcs8.go | 15 +++++++-------- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/pkcs8/pbes1/pkcs8.go b/pkcs8/pbes1/pkcs8.go index 9128644..b56ff13 100644 --- a/pkcs8/pbes1/pkcs8.go +++ b/pkcs8/pbes1/pkcs8.go @@ -59,7 +59,7 @@ func EncryptPKCS8PrivateKey( b, err := asn1.Marshal(pki) if err != nil { - return nil, errors.New(err.Error() + " error marshaling encrypted key") + return nil, errors.New("error marshaling encrypted key") } return &pem.Block{ @@ -72,7 +72,7 @@ func EncryptPKCS8PrivateKey( func DecryptPKCS8PrivateKey(data, password []byte) ([]byte, error) { var pki encryptedPrivateKeyInfo if _, err := asn1.Unmarshal(data, &pki); err != nil { - return nil, errors.New(err.Error() + " failed to unmarshal private key") + return nil, errors.New("failed to unmarshal private key") } cipher, cipherParams, err := parseEncryptionScheme(pki.EncryptionAlgorithm) @@ -130,7 +130,7 @@ func EncryptPKCS8Privatekey( b, err := asn1.Marshal(pki) if err != nil { - return nil, errors.New(err.Error() + " error marshaling encrypted key") + return nil, errors.New("error marshaling encrypted key") } return &pem.Block{ @@ -143,7 +143,7 @@ func EncryptPKCS8Privatekey( func DecryptPKCS8Privatekey(data, password []byte) ([]byte, error) { var pki encryptedPrivateKeyInfo if _, err := asn1.Unmarshal(data, &pki); err != nil { - return nil, errors.New(err.Error() + " failed to unmarshal private key") + return nil, errors.New("failed to unmarshal private key") } cipher, cipherParams, err := parseEncryptionScheme(pki.EncryptionAlgorithm) diff --git a/pkcs8/pbes2/kdf_pbkdf2.go b/pkcs8/pbes2/kdf_pbkdf2.go index 60271e6..4943871 100644 --- a/pkcs8/pbes2/kdf_pbkdf2.go +++ b/pkcs8/pbes2/kdf_pbkdf2.go @@ -118,7 +118,7 @@ func oidByHash(h Hash) (asn1.ObjectIdentifier, error) { return oidHMACWithGOST34112012512, nil } - return nil, errors.New("go-cryptobin/pkcs8: unsupported hash function") + return nil, errors.New("unsupported hash function") } // pbkdf2 params diff --git a/pkcs8/pbes2/kdf_smpbkdf2.go b/pkcs8/pbes2/kdf_smpbkdf2.go index 389630d..d2f05b0 100644 --- a/pkcs8/pbes2/kdf_smpbkdf2.go +++ b/pkcs8/pbes2/kdf_smpbkdf2.go @@ -39,7 +39,7 @@ func oidSMByHash(h Hash) (asn1.ObjectIdentifier, error) { return oidHMACWithSM3, nil } - return nil, errors.New("go-cryptobin/pkcs8: unsupported hash function") + return nil, errors.New("unsupported hash function") } // smpbkdf2 params diff --git a/pkcs8/pbes2/pkcs8.go b/pkcs8/pbes2/pkcs8.go index 7190c01..6a2f555 100644 --- a/pkcs8/pbes2/pkcs8.go +++ b/pkcs8/pbes2/pkcs8.go @@ -86,10 +86,9 @@ func EncryptPKCS8PrivateKey( encrypted, encryptionAlgorithm, err := PBES2Encrypt(rand, data, password, useOpts) if err != nil { - return nil, errors.New("go-cryptobin/pkcs8: " + err.Error()) + return nil, err } - // 生成 ans1 数据 pki := encryptedPrivateKeyInfo{ EncryptionAlgorithm: encryptionAlgorithm, EncryptedData: encrypted, @@ -97,7 +96,7 @@ func EncryptPKCS8PrivateKey( b, err := asn1.Marshal(pki) if err != nil { - return nil, errors.New("go-cryptobin/pkcs8: error marshaling encrypted key: " + err.Error()) + return nil, errors.New("error marshaling encrypted key") } return &pem.Block{ @@ -110,7 +109,7 @@ func EncryptPKCS8PrivateKey( func DecryptPKCS8PrivateKey(data, password []byte) ([]byte, error) { var pki encryptedPrivateKeyInfo if _, err := asn1.Unmarshal(data, &pki); err != nil { - return nil, errors.New("go-cryptobin/pkcs8: failed to unmarshal private key: " + err.Error()) + return nil, errors.New("failed to unmarshal private key") } algo := pki.EncryptionAlgorithm @@ -118,7 +117,7 @@ func DecryptPKCS8PrivateKey(data, password []byte) ([]byte, error) { decryptedKey, err := PBES2Decrypt(encryptedKey, algo, password) if err != nil { - return nil, errors.New("go-cryptobin/pkcs8: " + err.Error()) + return nil, err } return decryptedKey, nil @@ -135,7 +134,7 @@ func DecryptPEMBlock(block *pem.Block, password []byte) ([]byte, error) { return DecryptPKCS8PrivateKey(block.Bytes, password) } - return nil, errors.New("go-cryptobin/pkcs8: unsupported encrypted PEM") + return nil, errors.New("unsupported encrypted PEM") } // PBES2 Encrypt data @@ -153,8 +152,8 @@ func PBES2Encrypt(rand io.Reader, data []byte, password []byte, opts *Opts) (enc } salt := make([]byte, kdfOpts.GetSaltSize()) - if _, saltErr := io.ReadFull(rand, salt); saltErr != nil { - err = errors.New("failed to generate salt: " + err.Error()) + if _, err = io.ReadFull(rand, salt); err != nil { + err = errors.New("failed to generate salt") return }