Skip to content

Commit

Permalink
Minor style change
Browse files Browse the repository at this point in the history
Error string should not be capitalized or end with punctuation

Signed-off-by: davidliu <[email protected]>
  • Loading branch information
davidkhala committed Sep 17, 2020
1 parent 4a2cb80 commit eeebcfc
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions utils/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// EC private keys are converted to PKCS#8 format.
func PrivateKeyToPEM(privateKey *sm2.PrivateKey, pwd []byte) ([]byte, error) {
if privateKey == nil {
return nil, errors.New("Invalid sm2 private key. It must be different from nil.")
return nil, errors.New("invalid sm2 private key. It must be different from nil")
}
raw, err := x509.MarshalECPrivateKey(privateKey)

Expand Down Expand Up @@ -43,24 +43,24 @@ func PrivateKeyToPEM(privateKey *sm2.PrivateKey, pwd []byte) ([]byte, error) {
return pem.EncodeToMemory(block), nil
}

// PEMtoPrivateKey unmarshals a pem to private key
// PEMtoPrivateKey unmarshal a pem to private key
func PEMtoPrivateKey(raw []byte, pwd []byte) (*sm2.PrivateKey, error) {
if len(raw) == 0 {
return nil, errors.New("Invalid PEM. It must be different from nil.")
return nil, errors.New("invalid PEM. It must be different from nil")
}
block, _ := pem.Decode(raw)
if block == nil {
return nil, fmt.Errorf("Failed decoding PEM. Block must be different from nil. [% x]", raw)
return nil, fmt.Errorf("failed decoding PEM. Block must be different from nil. [% x]", raw)
}

if x509.IsEncryptedPEMBlock(block) {
if len(pwd) == 0 {
return nil, errors.New("Encrypted Key. Need a password")
return nil, errors.New("encrypted Key. Need a password")
}

decrypted, err := x509.DecryptPEMBlock(block, pwd)
if err != nil {
return nil, fmt.Errorf("Failed PEM decryption [%s]", err)
return nil, fmt.Errorf("failed PEM decryption [%s]", err)
}

key, err := x509.ParseECPrivateKey(decrypted)
Expand Down Expand Up @@ -94,12 +94,12 @@ func PublicKeyToPEM(publicKey *sm2.PublicKey, pwd []byte) ([]byte, error) {
}

if publicKey == nil {
return nil, errors.New("Invalid public key. It must be different from nil.")
return nil, errors.New("invalid public key. It must be different from nil")
}

PubASN1, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return nil, err
return nil, err
}
return pem.EncodeToMemory(
&pem.Block{
Expand All @@ -113,48 +113,48 @@ func PublicKeyToPEM(publicKey *sm2.PublicKey, pwd []byte) ([]byte, error) {
// PublicKeyToEncryptedPEM converts a public key to encrypted pem
func PublicKeyToEncryptedPEM(publicKey *sm2.PublicKey, pwd []byte) ([]byte, error) {
if publicKey == nil {
return nil, errors.New("Invalid public key. It must be different from nil.")
return nil, errors.New("invalid public key. It must be different from nil")
}
if len(pwd) == 0 {
return nil, errors.New("Invalid password. It must be different from nil.")
return nil, errors.New("invalid password. It must be different from nil")
}

raw, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return nil, err
}
return nil, err
}
block, err := x509.EncryptPEMBlock(
rand.Reader,
"PUBLIC KEY",
raw,
pwd,
x509.PEMCipherAES256)
if err != nil {
return nil, err
}
return nil, err
}

return pem.EncodeToMemory(block), nil
}

// PEMtoPublicKey unmarshals a pem to public key
// PEMtoPublicKey unmarshal a pem to public key
func PEMtoPublicKey(raw []byte, pwd []byte) (*sm2.PublicKey, error) {
if len(raw) == 0 {
return nil, errors.New("Invalid PEM. It must be different from nil.")
return nil, errors.New("invalid PEM. It must be different from nil")
}
block, _ := pem.Decode(raw)
if block == nil {
return nil, fmt.Errorf("Failed decoding. Block must be different from nil. [% x]", raw)
return nil, fmt.Errorf("failed decoding. Block must be different from nil. [% x]", raw)
}

// TODO: derive from header the type of the key
if x509.IsEncryptedPEMBlock(block) {
if len(pwd) == 0 {
return nil, errors.New("Encrypted Key. Password must be different from nil")
return nil, errors.New("encrypted Key. Password must be different from nil")
}

decrypted, err := x509.DecryptPEMBlock(block, pwd)
if err != nil {
return nil, fmt.Errorf("Failed PEM decryption. [%s]", err)
return nil, fmt.Errorf("failed PEM decryption. [%s]", err)
}

key, err := x509.ParsePKIXPublicKey(decrypted)
Expand Down

0 comments on commit eeebcfc

Please sign in to comment.