crypto

package module
v0.0.0-...-2aced4b Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2021 License: BSD-3-Clause Imports: 15 Imported by: 2

README

Crypto.go

a handy library to create public, private keys, encrypt, decrypt data, and create custom certificate. under the hood is using

  • Chacha20 for encrypt/decrypt
  • ED25519 and Curve25519 for public private keys, which also used in Certificate

tests contains all example required for using this library

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyMismatched           = errors.New("keys are mismatched")
	ErrNotCA                   = errors.New("given certificate is not a CA")
	ErrNotIssuer               = errors.New("issuer value is not the same")
	ErrFailedVerifyCertificate = errors.New("certificate is not verified by ca")
)
View Source
var (
	// ErrChiperTextTooShort if content length is too short for decoding
	ErrChiperTextTooShort = errors.New("ciphertext too short")
	// ErrWrongKey is returns when decrypting content is failing
	ErrWrongKey = errors.New("wrong key")
)
View Source
var (
	ErrPemBannerNotFound  = errors.New("pem banner not found")
	ErrPemIncorrectFormat = errors.New("pem incurrent format")
)
View Source
var ErrWrongFormat = errors.New("wrong format")

Functions

func BinaryDecode

func BinaryDecode(b []byte, ptr interface{}) error

func BinaryEncode

func BinaryEncode(value interface{}) ([]byte, error)

func CreateCertificate

func CreateCertificate(parentPrivateKey *PrivateKey, parentCertificate *Certificate, opts ...CertificateDetailsFn) (*Certificate, *PrivateKey, error)

func DecodePEM

func DecodePEM(r io.Reader, decoder BinaryDecoder, banner string, passphrase []byte) error

func EncodePEM

func EncodePEM(w io.Writer, encoder BinaryEncoder, banner string, passphrase []byte) error

func IsKeysMatched

func IsKeysMatched(public *PublicKey, private *PrivateKey) bool

IsKeysMatched checks whether both public and private key are belong to each other

func NewKeyPair

func NewKeyPair() (*PublicKey, *PrivateKey, error)

Types

type BinaryDecoder

type BinaryDecoder interface {
	Decode(b []byte) error
}

type BinaryEncoder

type BinaryEncoder interface {
	Encode() ([]byte, error)
}

type Certificate

type Certificate struct {
	Details   *CertificateDetails
	Signature []byte
}

func (*Certificate) Decode

func (c *Certificate) Decode(b []byte) error

func (*Certificate) DecodeExtra

func (c *Certificate) DecodeExtra(decoder BinaryDecoder) error

func (*Certificate) Encode

func (c *Certificate) Encode() ([]byte, error)

func (*Certificate) Verify

func (c *Certificate) Verify(ca *Certificate) error

type CertificateDetails

type CertificateDetails struct {
	NotBefore time.Time
	NotAfter  time.Time
	PublicKey PublicKey
	Issuer    []byte
	Extra     []byte
	IsCA      bool
}

type CertificateDetailsFn

type CertificateDetailsFn func(details *CertificateDetails) error

func CertWithAuthority

func CertWithAuthority() CertificateDetailsFn

func CertWithExtra

func CertWithExtra(extra BinaryEncoder) CertificateDetailsFn

func CertWithNotAfter

func CertWithNotAfter(notAfter time.Time) CertificateDetailsFn

func CertWithNotBefore

func CertWithNotBefore(notBefore time.Time) CertificateDetailsFn

type ChaCha20

type ChaCha20 struct{}

ChaCha20 enecryption type

func NewChaCha20

func NewChaCha20() *ChaCha20

NewChaCha20 creates chacha20 encryption

func (ChaCha20) Decrypt

func (c ChaCha20) Decrypt(data []byte, key []byte) ([]byte, error)

Decrypt decrypts data using given key

func (ChaCha20) Encrypt

func (c ChaCha20) Encrypt(data []byte, key []byte) ([]byte, error)

Encrypt encrypts data using given key

type ChaCha20Stream

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

func NewChaCha20Stream

func NewChaCha20Stream(r io.Reader, key []byte) (*ChaCha20Stream, error)

func (*ChaCha20Stream) Close

func (cs *ChaCha20Stream) Close() error

func (*ChaCha20Stream) Read

func (cs *ChaCha20Stream) Read(p []byte) (int, error)

type PrivateKey

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

func (*PrivateKey) Decode

func (p *PrivateKey) Decode(b []byte) error

func (*PrivateKey) Encode

func (p *PrivateKey) Encode() ([]byte, error)

func (*PrivateKey) SharedKey

func (p *PrivateKey) SharedKey(public *PublicKey) ([]byte, error)

func (*PrivateKey) Sign

func (p *PrivateKey) Sign(data []byte) ([]byte, error)

type PublicKey

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

func (*PublicKey) Decode

func (p *PublicKey) Decode(b []byte) error

func (*PublicKey) Encode

func (p *PublicKey) Encode() ([]byte, error)

func (*PublicKey) GobDecode

func (p *PublicKey) GobDecode(b []byte) error

func (*PublicKey) GobEncode

func (p *PublicKey) GobEncode() ([]byte, error)

func (*PublicKey) Verify

func (p *PublicKey) Verify(data []byte, signature []byte) bool

Jump to

Keyboard shortcuts

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