cipher

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2019 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

cipher package is a helper package for encrypting and decrypting messages

Index

Constants

View Source
const (
	// CipherKey is the Viper subkey under which logging should be stored.
	// NewOptions *does not* assume this key.
	CipherKey = "cipher"
)

Variables

This section is empty.

Functions

func GeneratePrivateKey

func GeneratePrivateKey(size int) *rsa.PrivateKey

GeneratePrivateKey will create a private key with the size given size must be greater than 64 or else it will default to 64.

Careful with the size, if its too large it won't encrypt the message or take forever

func GetHash added in v0.3.0

func GetHash(hashType string) crypto.Hash

func GetPrivateKey added in v0.3.2

func GetPrivateKey(loader KeyLoader) (*rsa.PrivateKey, error)

func GetPublicKey added in v0.3.2

func GetPublicKey(loader KeyLoader) (*rsa.PublicKey, error)

Types

type AlgorithmType added in v0.4.0

type AlgorithmType string
const (
	None          AlgorithmType = "none"
	Box           AlgorithmType = "box"
	RSASymmetric  AlgorithmType = "rsa-sym"
	RSAAsymmetric AlgorithmType = "rsa-asy"
)

func ParseAlogrithmType added in v0.4.0

func ParseAlogrithmType(algo string) AlgorithmType

type BasicHashLoader added in v0.3.0

type BasicHashLoader struct {
	HashName string `mapstructure:"hash"`
}

func (*BasicHashLoader) GetHash added in v0.3.0

func (b *BasicHashLoader) GetHash() (crypto.Hash, error)

GetHash return the given hash from hashFunctions if not found it will return an error.

0 is an invalid hash

type BoxLoader added in v0.3.2

type BoxLoader struct {
	KID        string
	PrivateKey KeyLoader
	PublicKey  KeyLoader
}

func (*BoxLoader) LoadDecrypt added in v0.3.2

func (boxLoader *BoxLoader) LoadDecrypt() (Decrypt, error)

func (*BoxLoader) LoadEncrypt added in v0.3.2

func (boxLoader *BoxLoader) LoadEncrypt() (Encrypt, error)

type BytesLoader added in v0.3.3

type BytesLoader struct {
	Data []byte
}

func (*BytesLoader) GetBytes added in v0.3.3

func (b *BytesLoader) GetBytes() ([]byte, error)

type Ciphers added in v0.4.0

type Ciphers struct {
	Options map[AlgorithmType]map[string]Decrypt
}

func PopulateCiphers added in v0.4.0

func PopulateCiphers(o Options, logger log.Logger) Ciphers

func (*Ciphers) Get added in v0.4.0

func (c *Ciphers) Get(alg AlgorithmType, KID string) (Decrypt, bool)

type Config added in v0.4.0

type Config struct {
	// Logger is the go-kit Logger to use for server startup and error logging.  If not
	// supplied, logging.DefaultLogger() is used instead.
	Logger log.Logger `json:"-"`

	// Type is the algorithm type. Like none, box, rsa etc.
	Type AlgorithmType `json:"type"`

	// KID is the key id of the cipher
	KID string `json:"kid,omitempty"`

	// Params to be provided to the algorithm type.
	// For example providing a hash algorithm to rsa.
	Params map[string]string `json:"params,omitempty"`

	// Keys is a map of keys to path. aka senderPrivateKey : private.pem
	Keys map[KeyType]string `json:"keys,omitempty"`
}

Config used load the Encrypt or Decrypt

func (*Config) LoadDecrypt added in v0.4.0

func (config *Config) LoadDecrypt() (Decrypt, error)

func (*Config) LoadEncrypt added in v0.4.0

func (config *Config) LoadEncrypt() (Encrypt, error)

type Decrypt added in v0.3.0

type Decrypt interface {
	Identification

	// DecryptMessage attempts to decode the message into a string.
	// and error will be returned if failed to decode the message.
	DecryptMessage(cipher []byte, nonce []byte) (message []byte, err error)
}

Decrypt represents the ability to decrypt messages

func DefaultCipherDecrypter added in v0.3.2

func DefaultCipherDecrypter() Decrypt

func NewBoxDecrypter added in v0.3.2

func NewBoxDecrypter(recipientPrivateKey [32]byte, senderPublicKey [32]byte, kid string) Decrypt

func NewRSADecrypter added in v0.4.0

func NewRSADecrypter(hash crypto.Hash, recipientPrivateKey *rsa.PrivateKey, senderPublicKey *rsa.PublicKey, kid string) Decrypt

type DecryptLoader added in v0.3.2

type DecryptLoader interface {
	LoadDecrypt() (Decrypt, error)
}

type Encrypt added in v0.3.2

type Encrypt interface {
	Identification

	// EncryptMessage attempts to encode the message into an array of bytes.
	// and error will be returned if failed to encode the message.
	EncryptMessage(message []byte) (crypt []byte, nonce []byte, err error)
}

Encrypt represents the ability to encrypt messages

func DefaultCipherEncrypter added in v0.3.2

func DefaultCipherEncrypter() Encrypt

func NewBoxEncrypter added in v0.3.2

func NewBoxEncrypter(senderPrivateKey [32]byte, recipientPublicKey [32]byte, kid string) Encrypt

func NewRSAEncrypter added in v0.4.0

func NewRSAEncrypter(hash crypto.Hash, senderPrivateKey *rsa.PrivateKey, recipientPublicKey *rsa.PublicKey, kid string) Encrypt

type EncryptLoader added in v0.3.2

type EncryptLoader interface {
	LoadEncrypt() (Encrypt, error)
}

type FileLoader added in v0.3.0

type FileLoader struct {
	Path string
}

func (*FileLoader) GetBytes added in v0.3.0

func (f *FileLoader) GetBytes() ([]byte, error)

type HashLoader added in v0.3.0

type HashLoader interface {
	GetHash() (crypto.Hash, error)
}

type Identification added in v0.4.0

type Identification interface {
	// GetAlgorithm will return the algorithm Encrypt and Decrypt uses
	GetAlgorithm() AlgorithmType

	// GetKID returns the id of the specific keys used
	GetKID() string
}

type KeyLoader added in v0.3.0

type KeyLoader interface {
	GetBytes() ([]byte, error)
}

func CreateFileLoader added in v0.4.0

func CreateFileLoader(keys map[KeyType]string, keyType KeyType) KeyLoader

type KeyType added in v0.4.0

type KeyType string
const (
	PublicKey           KeyType = "publicKey"
	PrivateKey          KeyType = "privateKey"
	SenderPrivateKey    KeyType = "senderPrivateKey"
	SenderPublicKey     KeyType = "senderPublicKey"
	RecipientPrivateKey KeyType = "recipientPrivateKey"
	RecipientPublicKey  KeyType = "recipientPublicKey"
)

type LocalCerts added in v0.3.0

type LocalCerts struct {
	Path     string
	HashName string
}

type NOOP

type NOOP struct{}

NOOP will just return the message

func (*NOOP) DecryptMessage

func (*NOOP) DecryptMessage(cipher []byte, nonce []byte) (message []byte, err error)

func (*NOOP) EncryptMessage

func (*NOOP) EncryptMessage(message []byte) (crypt []byte, nonce []byte, err error)

func (*NOOP) GetAlgorithm added in v0.4.0

func (*NOOP) GetAlgorithm() AlgorithmType

func (*NOOP) GetKID added in v0.4.0

func (*NOOP) GetKID() string

type Options added in v0.3.2

type Options []Config

func FromViper added in v0.3.2

func FromViper(v *viper.Viper) (o Options, err error)

FromViper produces an Options from a (possibly nil) Viper instance. cipher key is expected

func (Options) GetEncrypter added in v0.4.0

func (o Options) GetEncrypter(logger log.Logger) (Encrypt, error)

type RSALoader added in v0.4.0

type RSALoader struct {
	KID        string
	Hash       HashLoader
	PrivateKey KeyLoader
	PublicKey  KeyLoader
}

func (*RSALoader) LoadDecrypt added in v0.4.0

func (loader *RSALoader) LoadDecrypt() (Decrypt, error)

func (*RSALoader) LoadEncrypt added in v0.4.0

func (loader *RSALoader) LoadEncrypt() (Encrypt, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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