github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/coder/rsa.go (about)

     1  package coder
     2  
     3  import (
     4  	"crypto/rand"
     5  	"crypto/rsa"
     6  	"crypto/x509"
     7  	"encoding/hex"
     8  	"encoding/pem"
     9  	"fmt"
    10  	"os"
    11  )
    12  
    13  const (
    14  	RSA_KEY_SIZE_256  = 256
    15  	RSA_KEY_SIZE_512  = 512
    16  	RSA_KEY_SIZE_1024 = 1024
    17  	RSA_KEY_SIZE_2048 = 2048
    18  )
    19  
    20  func RSAGenerateKeyPair(size int, privateKeyPath string, publicKeyPath string) error {
    21  	privKey, err := rsa.GenerateKey(rand.Reader, size)
    22  	if err != nil {
    23  		return err
    24  	}
    25  	xpriv := x509.MarshalPKCS1PrivateKey(privKey)
    26  	privFile, err := os.Create(privateKeyPath)
    27  	if err != nil {
    28  		return err
    29  	}
    30  	defer func(privFile *os.File) { _ = privFile.Close() }(privFile)
    31  	privBlock := pem.Block{
    32  		Type:  "RSA Private Key",
    33  		Bytes: xpriv,
    34  	}
    35  	err = pem.Encode(privFile, &privBlock)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	pubKey := privKey.PublicKey
    41  	xpub, err := x509.MarshalPKIXPublicKey(&pubKey)
    42  	if err != nil {
    43  		return err
    44  	}
    45  	pubFile, err := os.Create(publicKeyPath)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	defer func(pubFile *os.File) { _ = pubFile.Close() }(pubFile)
    50  	pubBlock := pem.Block{
    51  		Type:  "RSA Public Key",
    52  		Bytes: xpub,
    53  	}
    54  	err = pem.Encode(pubFile, &pubBlock)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	return nil
    59  }
    60  
    61  func RSAEncrypt(content string, publicKeyPath string) (string, error) {
    62  	file, err := os.Open(publicKeyPath)
    63  	if err != nil {
    64  		return "", err
    65  	}
    66  	defer func(file *os.File) { _ = file.Close() }(file)
    67  	info, _ := file.Stat()
    68  	buf := make([]byte, info.Size())
    69  	_, err = file.Read(buf)
    70  	if err != nil {
    71  		return "", err
    72  	}
    73  	block, _ := pem.Decode(buf)
    74  	pubKeyIntf, err := x509.ParsePKIXPublicKey(block.Bytes)
    75  	if err != nil {
    76  		return "", err
    77  	}
    78  	pubKey := pubKeyIntf.(*rsa.PublicKey)
    79  	text, err := rsa.EncryptPKCS1v15(rand.Reader, pubKey, []byte(content))
    80  	if err != nil {
    81  		return "", err
    82  	}
    83  	return fmt.Sprintf("%x", text), nil
    84  }
    85  
    86  func RSADecrypt(content string, privateKeyPath string) (string, error) {
    87  	file, err := os.Open(privateKeyPath)
    88  	if err != nil {
    89  		return "", err
    90  	}
    91  	defer func(file *os.File) { _ = file.Close() }(file)
    92  	info, _ := file.Stat()
    93  	buf := make([]byte, info.Size())
    94  	_, err = file.Read(buf)
    95  	if err != nil {
    96  		return "", err
    97  	}
    98  	block, _ := pem.Decode(buf)
    99  	privKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
   100  	if err != nil {
   101  		return "", err
   102  	}
   103  	b, err := hex.DecodeString(content)
   104  	if err != nil {
   105  		return "", err
   106  	}
   107  	text, err := rsa.DecryptPKCS1v15(rand.Reader, privKey, b)
   108  	if err != nil {
   109  		return "", err
   110  	}
   111  	return string(text), nil
   112  }