gitee.com/lh-her-team/common@v1.5.1/crypto/asym/rsa/pk.go (about)

     1  package rsa
     2  
     3  import (
     4  	"bytes"
     5  	crypto2 "crypto"
     6  	"crypto/rand"
     7  	"crypto/rsa"
     8  	"crypto/sha256"
     9  	"crypto/x509"
    10  	"encoding/pem"
    11  	"fmt"
    12  
    13  	"gitee.com/lh-her-team/common/crypto"
    14  	"gitee.com/lh-her-team/common/crypto/hash"
    15  )
    16  
    17  type PublicKey struct {
    18  	K *rsa.PublicKey
    19  }
    20  
    21  func (pk *PublicKey) Bytes() ([]byte, error) {
    22  	if pk.K == nil {
    23  		return nil, fmt.Errorf("public key is nil")
    24  	}
    25  	return x509.MarshalPKCS1PublicKey(pk.K), nil
    26  }
    27  
    28  func (pk *PublicKey) Verify(data []byte, sig []byte) (bool, error) {
    29  	hashed := sha256.Sum256(data)
    30  	err := rsa.VerifyPKCS1v15(pk.K, crypto2.SHA256, hashed[:], sig)
    31  	if err != nil {
    32  		return false, err
    33  	}
    34  	return true, nil
    35  }
    36  
    37  func (pk *PublicKey) VerifyWithOpts(data []byte, sig []byte, opts *crypto.SignOpts) (bool, error) {
    38  	if opts == nil || opts.Hash == crypto.HASH_TYPE_SM3 {
    39  		return pk.Verify(data, sig)
    40  	}
    41  	hashed, err := hash.Get(opts.Hash, data)
    42  	if err != nil {
    43  		return false, err
    44  	}
    45  	switch opts.EncodingType {
    46  	case RSA_PSS:
    47  		err = rsa.VerifyPSS(pk.K, crypto2.SHA256, hashed, sig, nil)
    48  	default:
    49  		err = rsa.VerifyPKCS1v15(pk.K, crypto2.Hash(opts.Hash), hashed[:], sig)
    50  	}
    51  	if err != nil {
    52  		return false, err
    53  	}
    54  	return true, nil
    55  }
    56  
    57  func (pk *PublicKey) Type() crypto.KeyType {
    58  	return -1
    59  }
    60  
    61  func (pk *PublicKey) String() (string, error) {
    62  	pkDER, err := pk.Bytes()
    63  	if err != nil {
    64  		return "", err
    65  	}
    66  	block := &pem.Block{
    67  		Type:  "PUBLIC KEY",
    68  		Bytes: pkDER,
    69  	}
    70  	buf := new(bytes.Buffer)
    71  	if err = pem.Encode(buf, block); err != nil {
    72  		return "", err
    73  	}
    74  	return buf.String(), nil
    75  }
    76  
    77  func (pk *PublicKey) ToStandardKey() crypto2.PublicKey {
    78  	return pk.K
    79  }
    80  
    81  func (pk *PublicKey) Encrypt(data []byte) ([]byte, error) {
    82  	return pk.EncryptWithOpts(data, defaultRSAOpts)
    83  }
    84  
    85  func (pk *PublicKey) EncryptWithOpts(data []byte, opts *crypto.EncOpts) ([]byte, error) {
    86  	switch opts.EncodingType {
    87  	case RSA_OAEP:
    88  		hashAlgo, err := hash.GetHashAlgorithm(opts.Hash)
    89  		if err != nil {
    90  			return nil, fmt.Errorf("RSA encryption fails: %v", err)
    91  		}
    92  		return rsa.EncryptOAEP(hashAlgo, rand.Reader, pk.ToStandardKey().(*rsa.PublicKey), data, opts.Label)
    93  	case RSA_PKCS1:
    94  		return rsa.EncryptPKCS1v15(rand.Reader, pk.ToStandardKey().(*rsa.PublicKey), data)
    95  	default:
    96  		return nil, fmt.Errorf("RSA encryption fails: unknown encoding type [%s]", opts.EncodingType)
    97  	}
    98  }