gorsa

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

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

Go to latest
Published: Dec 2, 2019 License: GPL-3.0 Imports: 14 Imported by: 0

README

GoRSA 加解密库

获取扩展包:

go get github.com/Dyangm/gorsa

具体使用:

package main

import (
	"fmt"
	"github.com/Dyangm/gorsa"
)

func main() {
	// 生成私钥
	privateKey, err := gorsa.GetPrivateKey()
	if err != nil {
		panic(err)
	}

	fmt.Println(privateKey)
	//由私钥生成公钥
	pubKey, err := gorsa.GetPublicKey(privateKey)
	if err != nil {
		panic(err)
	}
	fmt.Println(pubKey)

	// 公钥加密私钥解密
	if err := applyPubEPriD(privateKey, pubKey); err != nil {
		panic(err)
	}
	// 公钥解密私钥加密
	if err := applyPriEPubD(privateKey, pubKey); err != nil {
		panic(err)
	}
}

// 公钥加密私钥解密
func applyPubEPriD(privateKey, pubKey string) error {
	pubenctypt, err := gorsa.PublicEncrypt(`hello world`, pubKey)
	if err != nil {
		return err
	}

	pridecrypt, err := gorsa.PriKeyDecrypt(pubenctypt, privateKey)
	if err != nil {
		return err
	}
	if string(pridecrypt) != `hello world` {
		return fmt.Errorf(`解密失败`)
	}
	return nil
}

// 公钥解密私钥加密
func applyPriEPubD(privateKey, pubKey string) error {
	prienctypt, err := gorsa.PriKeyEncrypt(`hello world`, privateKey)
	if err != nil {
		return err
	}

	pubdecrypt, err := gorsa.PublicDecrypt(prienctypt, pubKey)
	if err != nil {
		return err
	}
	if string(pubdecrypt) != `hello world` {
		return fmt.Errorf(`解密失败`)
	}
	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 GetPrivateKey

func GetPrivateKey() (string, error)

获取私钥

func GetPublicKey

func GetPublicKey(privateKey string) (string, error)

获取公钥

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 SignMd5WithRsa

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

使用RSAWithMd5算法签名

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() (err error)

获取私钥

func (*RSASecurity) GetPublicKey

func (rsas *RSASecurity) GetPublicKey() (err error)

获取公钥

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) SignMd5WithRsa

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

*

  • 使用RSAWithMD5算法签名

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