xrsa

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

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

Go to latest
Published: Jun 26, 2018 License: GPL-3.0 Imports: 9 Imported by: 1

README

xrsa

OpenSSL RSA Encryption, Decryption, and Key Generation. Large Data Support in Golang

示例

package main

import (
	"fmt"
	"time"
	"github.com/ego008/xrsa"
)

func main() {
	// example

	keyLen := 2048
	priKT := xrsa.PKCS1

	// 生成密钥对
	pubKey, priKey, err := xrsa.GenRsaKeyPair(keyLen, priKT)
	if err != nil {
		fmt.Println("生成密钥对失败", err)
		return
	}
	fmt.Println("pubkey:\n", string(pubKey))
	fmt.Println("prikey:\n", string(priKey))

	mRsa, err := xrsa.NewXRsa(pubKey, priKey, keyLen, priKT) // 输入密钥对
	mRsa, err = xrsa.NewXRsa(nil, nil, keyLen, priKT)        // 或密钥对留空
	if err != nil {
		fmt.Println("NewRsa 失败", err)
		return
	}

	data := "Hello, World"
	fmt.Println("data:", data)

	t1 := time.Now()

	// 以"实例"方式使用,适用于固定密钥对

	// 公钥加密
	encrypted, err := mRsa.PublicEncrypt(data)
	if err != nil {
		fmt.Println("公钥加密失败", err)
		return
	}
	fmt.Println("encrypted:", encrypted)

	// 私钥解密
	decrypted, err := mRsa.PrivateDecrypt(encrypted)
	if err != nil {
		fmt.Println("私钥解密失败", err)
		return
	}
	fmt.Println("decrypted:", decrypted)

	if data == decrypted {
		fmt.Println("公钥加密、私钥解密成功!")
	} else {
		fmt.Println("公钥加密成功!私钥解密失败!!!")
	}

	// 签名、验签
	sign, err := mRsa.Sign(data)
	if err != nil {
		fmt.Println("签名失败", err)
		return
	}
	fmt.Println("sign:", sign)

	err = mRsa.Verify(data, sign)
	if err != nil {
		fmt.Println("验签失败", err)
	} else {
		fmt.Println("验签成功")
	}

	fmt.Println("time:", time.Now().Sub(t1))

	// 以"函数"方式使用,适用于动态密钥对

	// 函数示例
	fmt.Println("------------函数示例---------")
	pubKeyStr := string(pubKey)
	priKeyStr := string(priKey)

	t1 = time.Now()

	// 公钥加密
	encrypted, err = xrsa.PublicEncrypt(pubKeyStr, data)
	if err != nil {
		fmt.Println("公钥加密失败", err)
		return
	}
	fmt.Println("encrypted:", encrypted)

	// 私钥解密
	decrypted, err = xrsa.PrivateDecrypt(pubKeyStr, priKeyStr, encrypted, xrsa.PKCS1)
	if err != nil {
		fmt.Println("私钥解密失败", err)
		return
	}
	fmt.Println("decrypted:", decrypted)

	if data == decrypted {
		fmt.Println("公钥加密、私钥解密成功!")
	} else {
		fmt.Println("公钥加密成功!私钥解密失败!!!")
	}

	// 签名、验签
	sign, err = xrsa.Sign(priKeyStr, data, xrsa.PKCS1)
	if err != nil {
		fmt.Println("签名失败", err)
		return
	}
	fmt.Println("sign:", sign)

	err = xrsa.Verify(pubKeyStr, data, sign)
	if err != nil {
		fmt.Println("验签失败", err)
	} else {
		fmt.Println("验签成功")
	}

	fmt.Println("time:", time.Now().Sub(t1))
}

感谢

基于 https://github.com/liamylian/x-rsa/tree/master/golang/xrsa 改进

Documentation

Index

Constants

View Source
const (
	PKCS1 pkcsType = iota
	PKCS8
)

Variables

This section is empty.

Functions

func GenRsaKeyPair

func GenRsaKeyPair(keyLength int, privateKeyType pkcsType) (public, private []byte, err error)

生成密钥对

func MarshalPKCS8PrivateKey

func MarshalPKCS8PrivateKey(key *rsa.PrivateKey) []byte

PKCS8

func PrivateDecrypt

func PrivateDecrypt(publicKey, privateKey, encrypted string, privateKeyType pkcsType) (originalData string, err error)

私钥解密

func PublicEncrypt

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

公钥加密

func Sign

func Sign(privateKey, data string, privateKeyType pkcsType) (signResult string, err error)

私钥签名

func Verify

func Verify(publicKey, data string, sign string) error

签名验证

Types

type XRsa

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

func NewXRsa

func NewXRsa(publicKey []byte, privateKey []byte, keyLen int, privateKeyType pkcsType) (xRsa *XRsa, err error)

func (*XRsa) PrivateDecrypt

func (r *XRsa) PrivateDecrypt(encrypted string) (string, error)

私钥解密

func (*XRsa) PublicEncrypt

func (r *XRsa) PublicEncrypt(data string) (string, error)

公钥加密

func (*XRsa) Sign

func (r *XRsa) Sign(data string) (signResult string, err error)

私钥签名

func (*XRsa) Verify

func (r *XRsa) Verify(data string, sign string) error

签名验证

Jump to

Keyboard shortcuts

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