pkigen

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

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

Go to latest
Published: Oct 1, 2022 License: MIT Imports: 10 Imported by: 0

README

Documentation

PKIGEN | Geoff Ford

PKIGEN provides unique public/private RSA key pairs, of the bit size required by the caller.

Optionally a public key can be provided by the caller, and the generated RSA key pair will then be returned, encrypted using that public key.

Installing and building the library

This project requires Go 1.18

To use this package in your own code, install it using go get:

go get github.com/gford1000-go/pkigen

Then, you can include it in your project:

import "github.com/gford1000-go/pkigen"

Alternatively, you can clone it yourself:

git clone https://github.com/gford1000-go/pkigen.git

Testing and benchmarking

To run all tests, cd into the directory and use:

go test -v

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var GCM = "GCM"
View Source
var OAEP_SHA256 = "OAEP_SHA256"

Functions

func UnmarshalPrivateKey

func UnmarshalPrivateKey(b *Base64EncodedRSAKey) (*rsa.PrivateKey, error)

UnmarshalPrivateKey returns the PrivateKey from the Base64EncodedRSAKey

func UnmarshalPublicKey

func UnmarshalPublicKey(b *Base64EncodedRSAKey) (*rsa.PublicKey, error)

UnmarshalPrivateKey returns the PublicKey from the Base64EncodedRSAKey

Types

type Base64EncodedRSAKey

type Base64EncodedRSAKey struct {
	PrivateKey string `json:"private_key,omitempty"`
	PublicKey  string `json:"public_key,omitempty"`
}

Base64EncodedRSAKey stores details of RSA keys, as base64 url-encoded strings

func CreateEncodedRSAKey

func CreateEncodedRSAKey(size int) (*Base64EncodedRSAKey, error)

CreateEncodedRSAKey returns a fully populated Base64EncodedRSAKey, where new keys are generated on each call using the specified number of bits

func Marshal

func Marshal(priv *rsa.PrivateKey) (*Base64EncodedRSAKey, error)

Marshal returns a Base64EncodedRSAKey from the specified key

func MarshalPublicKey

func MarshalPublicKey(priv *rsa.PrivateKey) (*Base64EncodedRSAKey, error)

MarshalPublicKey returns a Base64EncodedRSAKey that only contains the public key, taken from the supplied private key

Example
// ALWAYS generate a new key - do not reuse this key
k, _ := UnmarshalPrivateKey(
	&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CgYBphxLV0mHnurwWe0dAVcKEhWSHWK1qF8O1V78ldXn6CJgv-ZPAhTM22UxBnjL3QxldDV6OOpKJrTnpOlQZWCwJYaBtOzy7nP3b8smyu62ceu_HCJ_crCKY__YmkzXIXzSgjP6jV8EbdW9AxK4Z9q_bTGF8oJ6jhxqddkgryWP51QKBgQCVqb09Q7wxDGuuHhtQ2Wmq42xy3GBYvaBnk3mkbNge18aAUVEEpYaUIIOmPW0rwmc17MFvM_dqx3iofQVRf2-aIYiihCwahzhMV3ISNQsr_MTH4YvO6fIuRtUANyLJ7_CZPSRZwSweQbY4cZGo-JTFoFb75YB6V91mScCaU5afawKBgDgZTwOoA3fDujfkAdiqiDS5s-MSKD9slHByX5lvxt_ERWMJwUHUHoDqy0uJsvHx0qlqorkFHcSgiXNSYWDtbvcWH-tV3gPhWSEI0gBqgcy4uKNRBALJOhC5Ew-X4NkL_FOYeRgMYn9AE2aVybbdM2JTpola8HtLP3TlY-RFUAqJ",
	},
)

// Return struct containing public key only, base64 encoded
j, _ := MarshalPublicKey(k)

// Serialise
b, _ := json.Marshal(j)

fmt.Println(string(b))
Output:

{"public_key":"MIIBCgKCAQEA2_vOF24UnIu-EnlL-wnZiKOmsxJIr0ZSGMe6atPTP9GT1OEBZLMbZ5NBiSlD4nIhpvSrthxjrRjiIERUupk2if-oaPnBPbuwuKks8X6aidP_vWUaIQiKYYXzlEvoXWxnPOlvs_e9cqjDAowy6zodHvGcGTOH8nTcv0MN_PEdGhbv5E85HBjl0I2pja2gEFjFmid0IORff1po_bKepzrktDJdS6pNgQimOM4HcpBuQU8E4MSgKnXH0ONlj-qbsfiTA34QINaSFMqm-UrAyh_jUkr8zpu5pFZpqSb9XGxLtb8f2TFhyJEPR1i42VyLiV42q1f0cemQqqNAAaTjMKM_mQIDAQAB"}

type EncryptedRSAKey

type EncryptedRSAKey struct {
	EnvelopePublicKey     string `json:"envelope_pk"`
	EnvelopeAlgo          string `json:"envelope_algo"`
	DetailEncyptionKey    string `json:"detail_encryption_key"`
	DetailEncryptionNonce string `json:"detail_encryption_nonce"`
	DetailAlgo            string `json:"detail_algo"`
	KeyDetail             string `json:"key_detail"`
}

EncryptedRSAKey provides sufficient details to be able to decrypt the Base64EncodedRSAKey details in KeyDetail, provided the private key corresponding to EnvelopePublicKey is known

func CreateEncryptedRSAKey

func CreateEncryptedRSAKey(pubKey *rsa.PublicKey, size int) (*EncryptedRSAKey, error)

CreateEncryptedRSAKey creates a Base64EncodedRSAKey with keys using the specified bit size, where the Base64EncodedRSAKey is encrypted using the supplied public key. The Base64EncodedRSAKey is actually symmetrically encrypted using a one time random key and nonce, and it is the random key that is then envelope encrypted with the supplied public key. This ensures that the process will work with different bit sizes.

func EncryptRSAKey

func EncryptRSAKey(pubKey *rsa.PublicKey, b *Base64EncodedRSAKey) (*EncryptedRSAKey, error)

EncryptRSAKey encrypts the provided Base64EncodedRSAKey using the supplied public key. The Base64EncodedRSAKey is actually symmetrically encrypted using a one time random key and nonce, and it is the random key that is then envelope encrypted with the supplied public key. This ensures that the process will work with different bit sizes.

func (*EncryptedRSAKey) Unmarshal

func (e *EncryptedRSAKey) Unmarshal(privKey *rsa.PrivateKey) (*Base64EncodedRSAKey, error)

Unmarshal will return the Base64EncodedRSAKey, provided that the supplied private key matches the public key used for encryption, and the contents of EncryptedRSAKey have not been modified

func (*EncryptedRSAKey) UnmarshalEnvelopePublicKey

func (e *EncryptedRSAKey) UnmarshalEnvelopePublicKey() (*rsa.PublicKey, error)

UnmarshalEnvelopePublicKey returns the public key that was used to encrypt the data in this instance, so that the corresponding private key can be identified

Jump to

Keyboard shortcuts

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