gorsa

package module
v0.0.0-...-be94e5b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2022 License: GPL-3.0 Imports: 14 Imported by: 0

README

GoRSA Encryption library

based on https://github.com/farmerx/gorsa Optimized the following points for packaging:

  • Optimization of public and private keys requires registration and initialization in advance.
  • The encryption machine does not perform base64 processing, and avoids secondary encapsulation of base64 during cross-program transfer or storage
  • The incoming return uses the string type uniformly to avoid conversion trouble
  • Supports RSAWithSHA1 and RSAWithSHA256 signature verification algorithms

Get expansion pack:

go get github.com/atishoo/gorsa

Specific use:

package main

import (
	"log"
	"errors"
	"github.com/wenzhenxi/gorsa"
)

var Pubkey = `-----BEGIN Public key-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk+89V7vpOj1rG6bTAKYM
56qmFLwNCBVDJ3MltVVtxVUUByqc5b6u909MmmrLBqS//PWC6zc3wZzU1+ayh8xb
UAEZuA3EjlPHIaFIVIz04RaW10+1xnby/RQE23tDqsv9a2jv/axjE/27b62nzvCW
eItu1kNQ3MGdcuqKjke+LKhQ7nWPRCOd/ffVqSuRvG0YfUEkOz/6UpsPr6vrI331
hWRB4DlYy8qFUmDsyvvExe4NjZWblXCqkEXRRAhi2SQRCl3teGuIHtDUxCskRIDi
aMD+Qt2Yp+Vvbz6hUiqIWSIH1BoHJer/JOq2/O6X3cmuppU4AdVNgy8Bq236iXvr
MQIDAQAB
-----END Public key-----
`

var Pirvatekey = `-----BEGIN Private key-----
MIIEpAIBAAKCAQEAk+89V7vpOj1rG6bTAKYM56qmFLwNCBVDJ3MltVVtxVUUByqc
5b6u909MmmrLBqS//PWC6zc3wZzU1+ayh8xbUAEZuA3EjlPHIaFIVIz04RaW10+1
xnby/RQE23tDqsv9a2jv/axjE/27b62nzvCWeItu1kNQ3MGdcuqKjke+LKhQ7nWP
RCOd/ffVqSuRvG0YfUEkOz/6UpsPr6vrI331hWRB4DlYy8qFUmDsyvvExe4NjZWb
lXCqkEXR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KVf8whertivAqSfHytnf/pMW8A
vUPy5G3iF5/nHj76CNRUbHsfQtv+wqnzoyPpHZgVQeQBhcoXJSm+qV3cdGjLU6OM
HgqeaQKBgQCnmL5SX7GSAeB0rSNugPp2GezAQj0H4OCc8kNrHK8RUvXIU9B2zKA2
z/QUKFb1gIGcKxYr+LqQ25/+TGvINjuf6P3fVkHL0U8jOG0IqpPJXO3Vl9B8ewWL
cFQVB/nQfmaMa4ChK0QEUe+Mqi++MwgYbRHx1lIOXEfUJO+PXrMekw==
-----END Private key-----
`


func main() {
	// Public key encryption private key decryption
	if err := applyPubEPriD(); err != nil {
		log.Println(err)
	}
	// Public key decryption private key encryption
	if err := applyPriEPubD(); err != nil {
		log.Println(err)
	}
}

// Public key encryption private key decryption
func applyPubEPriD() error {
	pubenctypt, err := gorsa.PublicEncrypt(`hello world`,Pubkey)
	if err != nil {
		return err
	}

	pridecrypt, err := gorsa.PriKeyDecrypt(pubenctypt,Pirvatekey)
	if err != nil {
		return err
	}
	if string(pridecrypt) != `hello world` {
		return errors.New(`Decryption failed`)
	}
	return nil
}

// Public key decryption private key encryption
func applyPriEPubD() error {
	prienctypt, err := gorsa.PriKeyEncrypt(`hello world`,Pirvatekey)
	if err != nil {
		return err
	}

	pubdecrypt, err := gorsa.PublicDecrypt(prienctypt,Pubkey)
	if err != nil {
		return err
	}
	if string(pubdecrypt) != `hello world` {
		return errors.New(`Decryption failed`)
	}
	return nil
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDataToLarge     = errors.New("message too long for RSA public key size")
	ErrDataLen         = errors.New("data length error")
	ErrDataBroken      = errors.New("data broken, first byte is not zero")
	ErrKeyPairDismatch = errors.New("data is not encrypted by the private key")
	ErrDecryption      = errors.New("decryption error")
	ErrPublicKey       = errors.New("get public key error")
	ErrPrivateKey      = errors.New("get private key error")
)
View Source
var RSA = &RSASecurity{}

Functions

func PriKeyDecrypt

func PriKeyDecrypt(data, privateKey string) (string, error)

私钥解密

func PriKeyEncrypt

func PriKeyEncrypt(data, privateKey string) (string, error)

私钥加密

func PublicDecrypt

func PublicDecrypt(data, publicKey string) (string, error)

公钥解密

func PublicEncrypt

func PublicEncrypt(data, publicKey string) (string, error)

公钥加密

func SignSha1WithRsa

func SignSha1WithRsa(data string, privateKey string) (string, error)

使用RSAWithSHA1算法签名

func SignSha256WithRsa

func SignSha256WithRsa(data string, privateKey string) (string, error)

使用RSAWithSHA256算法签名

func VerifySignSha1WithRsa

func VerifySignSha1WithRsa(data string, signData string, publicKey string) error

使用RSAWithSHA1验证签名

func VerifySignSha256WithRsa

func VerifySignSha256WithRsa(data string, signData string, publicKey string) error

使用RSAWithSHA256验证签名

Types

type RSASecurity

type RSASecurity struct {
	// contains filtered or unexported fields
}

func (*RSASecurity) GetPrivatekey

func (rsas *RSASecurity) GetPrivatekey() (*rsa.PrivateKey, error)

*rsa.PublicKey

func (*RSASecurity) GetPublickey

func (rsas *RSASecurity) GetPublickey() (*rsa.PublicKey, error)

*rsa.PrivateKey

func (*RSASecurity) PriKeyDECRYPT

func (rsas *RSASecurity) PriKeyDECRYPT(input []byte) ([]byte, error)

私钥解密

func (*RSASecurity) PriKeyENCTYPT

func (rsas *RSASecurity) PriKeyENCTYPT(input []byte) ([]byte, error)

私钥加密

func (*RSASecurity) PubKeyDECRYPT

func (rsas *RSASecurity) PubKeyDECRYPT(input []byte) ([]byte, error)

公钥解密

func (*RSASecurity) PubKeyENCTYPT

func (rsas *RSASecurity) PubKeyENCTYPT(input []byte) ([]byte, error)

公钥加密

func (*RSASecurity) SetPrivateKey

func (rsas *RSASecurity) SetPrivateKey(priStr string) (err error)

设置私钥

func (*RSASecurity) SetPublicKey

func (rsas *RSASecurity) SetPublicKey(pubStr string) (err error)

设置公钥

func (*RSASecurity) SignSha1WithRsa

func (rsas *RSASecurity) SignSha1WithRsa(data string) (string, error)

*

  • 使用RSAWithSHA1算法签名

func (*RSASecurity) SignSha256WithRsa

func (rsas *RSASecurity) SignSha256WithRsa(data string) (string, error)

*

  • 使用RSAWithSHA256算法签名

func (*RSASecurity) VerifySignSha1WithRsa

func (rsas *RSASecurity) VerifySignSha1WithRsa(data string, signData string) error

*

  • 使用RSAWithSHA1验证签名

func (*RSASecurity) VerifySignSha256WithRsa

func (rsas *RSASecurity) VerifySignSha256WithRsa(data string, signData string) error

*

  • 使用RSAWithSHA256验证签名

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL