Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jan 4, 2025
1 parent 617d575 commit fc09e49
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
8 changes: 4 additions & 4 deletions pkcs8/pbes1/pkcs8.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand Down Expand Up @@ -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{
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkcs8/pbes2/kdf_pbkdf2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkcs8/pbes2/kdf_smpbkdf2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions pkcs8/pbes2/pkcs8.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,17 @@ 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,
}

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{
Expand All @@ -110,15 +109,15 @@ 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
encryptedKey := pki.EncryptedData

decryptedKey, err := PBES2Decrypt(encryptedKey, algo, password)
if err != nil {
return nil, errors.New("go-cryptobin/pkcs8: " + err.Error())
return nil, err
}

return decryptedKey, nil
Expand All @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit fc09e49

Please sign in to comment.