github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/commons/cryptos/rsa/rsa.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package rsa
    19  
    20  import (
    21  	"crypto/rand"
    22  	"crypto/rsa"
    23  	"crypto/x509"
    24  	"encoding/pem"
    25  	"github.com/aacfactory/errors"
    26  )
    27  
    28  func New(pubPEM []byte, priPEM []byte) (v *RSA, err error) {
    29  	pubBlock, _ := pem.Decode(pubPEM)
    30  	if pubBlock == nil {
    31  		err = errors.Warning("rsa: public pem is invalid format")
    32  		return
    33  	}
    34  	publicKeyInterface, parsePubErr := x509.ParsePKIXPublicKey(pubBlock.Bytes)
    35  	if parsePubErr != nil {
    36  		err = errors.Warning("rsa: parse public pem failed").WithCause(parsePubErr)
    37  		return
    38  	}
    39  	publicKey, ok := publicKeyInterface.(*rsa.PublicKey)
    40  	if !ok {
    41  		err = errors.Warning("rsa: the kind of key is not a rsa.PublicKey")
    42  		return
    43  	}
    44  
    45  	priBlock, _ := pem.Decode(priPEM)
    46  	if priBlock == nil {
    47  		err = errors.Warning("rsa: private pem is invalid format")
    48  		return
    49  	}
    50  
    51  	// x509 parse
    52  	privateKey, parsePrivateErr := x509.ParsePKCS1PrivateKey(priBlock.Bytes)
    53  	if parsePrivateErr != nil {
    54  		err = errors.Warning("rsa: the kind of key is not a rsa.Private").WithCause(parsePrivateErr)
    55  		return
    56  	}
    57  
    58  	v = &RSA{
    59  		public:  publicKey,
    60  		private: privateKey,
    61  	}
    62  
    63  	return
    64  }
    65  
    66  type RSA struct {
    67  	public  *rsa.PublicKey
    68  	private *rsa.PrivateKey
    69  }
    70  
    71  func (r *RSA) Key() (public *rsa.PublicKey, private *rsa.PrivateKey) {
    72  	public, private = r.public, r.private
    73  	return
    74  }
    75  
    76  func (r *RSA) Encrypt(plain []byte) (encrypted []byte, err error) {
    77  	encrypted, err = rsa.EncryptPKCS1v15(rand.Reader, r.public, plain)
    78  	if err != nil {
    79  		err = errors.Warning("rsa: encrypt failed").WithCause(err)
    80  		return
    81  	}
    82  	return
    83  }
    84  
    85  func (r *RSA) Decrypt(encrypted []byte) (plain []byte, err error) {
    86  	plain, err = rsa.DecryptPKCS1v15(rand.Reader, r.private, encrypted)
    87  	if err != nil {
    88  		err = errors.Warning("rsa: decrypt failed").WithCause(err)
    89  		return
    90  	}
    91  	return
    92  }