key

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RsaBlockType         = "RSA PRIVATE KEY"
	EcdsaBlockType       = "EC PRIVATE KEY"
	PKCCS8BlockType      = "PRIVATE KEY"
	DefaultKeyLength     = 2048
	RecommendedKeyLength = 4096
)

Variables

This section is empty.

Functions

func EncodeWithBlockType added in v0.2.0

func EncodeWithBlockType(b []byte, blockType string) []byte

EncodeWithBlockType returns the PEM encoding of b with the given block type. If b has invalid headers and cannot be encoded, Encode returns nil.

Types

type EcdsaKey

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

func NewEcdsaKey

func NewEcdsaKey(curve elliptic.Curve) *EcdsaKey

NewEcdsaKey return an Ecdsa key generator.

func (*EcdsaKey) Gen

func (g *EcdsaKey) Gen() (crypto.Signer, error)

Gen generates a public and private key pair. And returns a crypto.Singer.

func (*EcdsaKey) Marshal added in v0.1.15

func (g *EcdsaKey) Marshal(pkey crypto.Signer, opts *MarshalOptions) ([]byte, error)

Marshal converts an EC private key to SEC 1 or PKCS#8, ASN.1 DER form. And returns the private key encoded in PEM blocks.

func (*EcdsaKey) MarshalECPrivateKey added in v0.2.0

func (g *EcdsaKey) MarshalECPrivateKey(pkey *ecdsa.PrivateKey, password []byte) ([]byte, error)

MarshalECPrivateKey converts an EC private key to SEC 1, ASN.1 DER form. And returns the private key encoded in PEM blocks.

func (*EcdsaKey) MarshalPKCS8PrivateKey added in v0.2.0

func (g *EcdsaKey) MarshalPKCS8PrivateKey(pkey interface{}) ([]byte, error)

MarshalPKCS8PrivateKey converts a private key to PKCS #8, ASN.1 DER form. And returns the private key encoded in PEM blocks.

type Generator added in v0.1.1

type Generator interface {
	// Gen generates a public and private key pair.
	// And returns a crypto.Singer.
	Gen() (crypto.Signer, error)
	// Marshal converts a private key to ASN.1 DER form.
	// And returns the private key encoded in PEM blocks.
	// The opts is optional.
	Marshal(pkey crypto.Signer, opts *MarshalOptions) ([]byte, error)
}

type MarshalOptions added in v0.2.0

type MarshalOptions struct {
	// Password can be nil, otherwise use it to encrypt the private key.
	// If the IsPKCS8 is true, the Password will be ignored.
	// See https://github.com/golang/go/commit/57af9745bfad2c20ed6842878e373d6c5b79285a.
	Password []byte
	// IsPKCS8 whether to convert the private key to PKCS #8, ASN.1 DER form.
	IsPKCS8 bool
}

type RsaKey

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

func NewRsaKey

func NewRsaKey(bits int) *RsaKey

NewRsaKey return an RSA key generator. If the bit size less than 2048 bits, set to 2048 bits.

func (*RsaKey) Gen

func (g *RsaKey) Gen() (crypto.Signer, error)

Gen generates a public and private key pair. And returns a crypto.Singer.

func (*RsaKey) Marshal added in v0.1.15

func (g *RsaKey) Marshal(pkey crypto.Signer, opts *MarshalOptions) ([]byte, error)

Marshal converts an RSA private key to PKCS #1 or PKCS #8, ASN.1 DER form. And returns the private key encoded in PEM blocks.

func (*RsaKey) MarshalPKCS1PrivateKey added in v0.2.0

func (g *RsaKey) MarshalPKCS1PrivateKey(pkey *rsa.PrivateKey, password []byte) ([]byte, error)

MarshalPKCS1PrivateKey converts an RSA private key to PKCS #1, ASN.1 DER form. And returns the private key encoded in PEM blocks.

func (*RsaKey) MarshalPKCS8PrivateKey added in v0.2.0

func (g *RsaKey) MarshalPKCS8PrivateKey(pkey any) ([]byte, error)

MarshalPKCS8PrivateKey converts a private key to PKCS #8, ASN.1 DER form. And returns the private key encoded in PEM blocks.

Jump to

Keyboard shortcuts

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