grsa

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2023 License: MIT Imports: 11 Imported by: 0

README

GRSA

Getting started

The key features of Gin are:

  • PKCS#1
  • PKCS#8
  • Support Bits:512、1024、2048、3072、4096
  • Support Long Text
Use GRSA

With Go module support, simply add the following import

import "github.com/hugiot/crypto/grsa"

to your code, and then go [build|run|test] will automatically fetch the necessary dependencies.

Otherwise, run the following Go command to install the grsa package:

$ go get -u github.com/hugiot/crypto/grsa
Generate Key
package main

import (
	"github.com/hugiot/crypto/grsa"
	"log"
)

func main() {
	//privateKey, publicKey, err := grsa.GenerateKey(1024, grsa.PKCS8)
	//privateKeyString, publicKeyString, err := grsa.GenerateKeyToString(1024, grsa.PKCS8)
	if err := grsa.GenerateKeyToFile(1024, grsa.PKCS8, "."); err != nil {
		log.Fatal(err)
	}
}
Get Key
package main

import "github.com/hugiot/crypto/grsa"

func main() {
	//rsaPrivateKey, _ := grsa.GetPrivateKeyFromStr("")
	//rsaPublicKey, _ := grsa.GetPublicKeyFromFile("")
	rsaPrivateKey, _ := grsa.GetPrivateKeyFromFile("./private.key")
	rsaPublicKey, _ := grsa.GetPublicKeyFromFile("./Public.key")
}
Encryption
package main

import "github.com/hugiot/crypto/grsa"

func main() {
	// use private key
	privateKey, _ := grsa.GetPrivateKeyFromFile(grsa.PrivateKeyFileName)
	ciphertext, _ := grsa.EncryptByPrivateKey(privateKey, []byte("this is content"))
	// use public key
	publicKey, _ := grsa.GetPublicKeyFromFile(grsa.PublicKeyFileName)
	ciphertext, _ := grsa.EncryptByPublicKey(publicKey, []byte("this is content"))
}
Decryption
package main

import "github.com/hugiot/crypto/grsa"

func main() {
	// use private key
	privateKey, _ := grsa.GetPrivateKeyFromFile(grsa.PrivateKeyFileName)
	plaintext, _ := grsa.DecryptByPrivateKey(privateKey, []byte("xxx"))
	// use public key
	publicKey, _ := grsa.GetPublicKeyFromFile(grsa.PublicKeyFileName)
	plaintext, _ := grsa.DecryptByPublicKey(publicKey, []byte("xxx"))
}

Documentation

Index

Constants

View Source
const (
	PrivateKeyFileName string = "private.key"
	PublicKeyFileName  string = "public.key"
)

Variables

View Source
var KeyFormatErr = errors.New("the key format must be PKCS#1 or PKCS#2")
View Source
var PrivateKeyErr = errors.New("private key error")
View Source
var PublicKeyErr = errors.New("public key error")

Functions

func DecryptByPrivateKey

func DecryptByPrivateKey(pk *rsa.PrivateKey, data []byte) ([]byte, error)

DecryptByPrivateKey alias rsa.DecryptPKCS1v15

func DecryptByPublicKey

func DecryptByPublicKey(pk *rsa.PublicKey, data []byte) ([]byte, error)

DecryptByPublicKey decrypt by public key

func EncryptByPrivateKey

func EncryptByPrivateKey(pk *rsa.PrivateKey, data []byte) ([]byte, error)

EncryptByPrivateKey alias rsa.SignPKCS1v15

func EncryptByPublicKey

func EncryptByPublicKey(pk *rsa.PublicKey, data []byte) ([]byte, error)

EncryptByPublicKey alias rsa.EncryptPKCS1v15

func GenerateKey

func GenerateKey(bits int, format KeyFormat) (privateKey []byte, publicKey []byte, err error)

GenerateKey generate public and private keys bits: 512、1024、2048、3072、4096 format: 0(PKCS#1)、1(PKCS#8)

func GenerateKeyToFile

func GenerateKeyToFile(bits int, format KeyFormat, path string) (err error)

GenerateKeyToFile generate public and private keys to file Generate as the file name is PrivateKeyFileName and PublicKeyFileName

func GenerateStringKey added in v0.0.5

func GenerateStringKey(bits int, format KeyFormat) (privateKey string, publicKey string, err error)

GenerateStringKey generate public and private keys of string type

func PrivateKeyToString added in v0.0.5

func PrivateKeyToString(bs []byte) (string, error)

PrivateKeyToString private key byte slice to string

func PublicKeyToString added in v0.0.5

func PublicKeyToString(bs []byte) (string, error)

PublicKeyToString public key byte slice to string

func ResolvePrivateKey added in v0.0.5

func ResolvePrivateKey(bs []byte) (privateKey *rsa.PrivateKey, err error)

ResolvePrivateKey resolve private key from byte slice

func ResolvePrivateKeyFromFile added in v0.0.5

func ResolvePrivateKeyFromFile(fileName string) (privateKey *rsa.PrivateKey, err error)

ResolvePrivateKeyFromFile resolve private key from file

func ResolvePrivateKeyFromStr added in v0.0.5

func ResolvePrivateKeyFromStr(s string) (privateKey *rsa.PrivateKey, err error)

ResolvePrivateKeyFromStr resolve private key from string

func ResolvePublicKey added in v0.0.5

func ResolvePublicKey(bs []byte) (publicKey *rsa.PublicKey, err error)

ResolvePublicKey resolve public key from byte slice

func ResolvePublicKeyFromFile added in v0.0.5

func ResolvePublicKeyFromFile(fileName string) (publicKey *rsa.PublicKey, err error)

ResolvePublicKeyFromFile resolve public key from file

func ResolvePublicKeyFromStr added in v0.0.5

func ResolvePublicKeyFromStr(s string) (publicKey *rsa.PublicKey, err error)

ResolvePublicKeyFromStr resolve public key from string

Types

type KeyFormat

type KeyFormat uint8 // key format
const (
	PKCS1 KeyFormat = iota // PKCS#1
	PKCS8                  // PKCS#8
)

Jump to

Keyboard shortcuts

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