v0.10.0 Latest Latest

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

Go to latest
Published: Jul 2, 2019 License: Apache-2.0 Imports: 19 Imported by: 0



cipher package is a helper package for encrypting and decrypting messages



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


This section is empty.


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

GetHash finds a matching Hash for the string given.

func GetPrivateKey added in v0.3.2

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

GetPrivateKey uses a keyloader to load a private key.

func GetPublicKey added in v0.3.2

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

GetPublicKey uses a keyloader to load a public key.


type AlgorithmType added in v0.4.0

type AlgorithmType string

AlgorithmType is an enum used to specify which algorithm is being used.

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

func ParseAlgorithmType added in v0.8.0

func ParseAlgorithmType(algo string) AlgorithmType

ParseAlgorithmType takes a string and returns an enum if one matches, otherwise returns the None AlgorithmType enum.

type BasicHashLoader added in v0.3.0

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

BasicHashLoader implements HashLoader.

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

BoxLoader loads the box encryption/decryption.

func (*BoxLoader) LoadDecrypt added in v0.3.2

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

LoadDecrypt loads a decrypter for the box algorithm.

func (*BoxLoader) LoadEncrypt added in v0.3.2

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

LoadEncrypt loads an encrypter for the box algorithm.

type BytesLoader added in v0.3.3

type BytesLoader struct {
	Data []byte

BytesLoader implements the KeyLoader.

func (*BytesLoader) GetBytes added in v0.3.3

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

GetBytes returns the bytes stored by the BytesLoader

type Ciphers added in v0.4.0

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

Ciphers provide all of the possibly algorithms that can be used to encrypt or decrypt.

func PopulateCiphers added in v0.4.0

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

PopulateCiphers takes options and a logger and creates ciphers from them.

func (*Ciphers) Get added in v0.4.0

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

Get returns a decrypter given an algorithm and KID.

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)

LoadDecrypt uses the config to load a decrypter.

func (*Config) LoadEncrypt added in v0.4.0

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

LoadEncrypt uses the config to load an encrypter.

type Decrypt added in v0.3.0

type Decrypt interface {

	// 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

DEfaultCipherDecrypter returns a NOOP decrypter.

func NewBoxDecrypter added in v0.3.2

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

NewBoxDecrypter returns a new box decrypter.

func NewRSADecrypter added in v0.4.0

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

NewRSADecrypter returns an RSA decrypter.

type DecryptLoader added in v0.3.2

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

DecryptLoader loads a decrypter.

type Encrypt added in v0.3.2

type Encrypt interface {

	// 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

DefaultCipherEncrypter returns a NOOP encrypter.

func NewBoxEncrypter added in v0.3.2

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

NewBoxEncrypter returns a new box encrypter.

func NewRSAEncrypter added in v0.4.0

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

NewRSAEncrypter returns an RSA encrypter.

type EncryptLoader added in v0.3.2

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

EncryptLoader loads an encrypter.

type FileLoader added in v0.3.0

type FileLoader struct {
	Path string

FileLoader loads a key from a file.

func (*FileLoader) GetBytes added in v0.3.0

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

GetBytes returns the bytes found at the filepath.

type HashLoader added in v0.3.0

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

HashLoader can get a hash.

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)

KeyLoader gets the bytes for a key.

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

KeyType is an enum for how the key can be used.

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

LocalCerts specify where locally to find the certs for a hash.

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)

DecryptMessage simply returns the message given.

func (*NOOP) EncryptMessage

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

EncryptMessage simply returns the message given.

func (*NOOP) GetAlgorithm added in v0.4.0

func (*NOOP) GetAlgorithm() AlgorithmType

GetAlgorithm returns None.

func (*NOOP) GetKID added in v0.4.0

func (*NOOP) GetKID() string

GetKID returns none.

type Options added in v0.3.2

type Options []Config

Options is the list of configurations used to load ciphers.

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)

GetEncrypter takes options and creates an encrypter out of it.

type RSALoader added in v0.4.0

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

RSALoader loads the encrypter/decrypter for the RSA algorithm.

func (*RSALoader) LoadDecrypt added in v0.4.0

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

LoadDecrypt loads the RSA decrypter.

func (*RSALoader) LoadEncrypt added in v0.4.0

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

LoadEncrypt loads the RSA encrypter.


Path Synopsis

Jump to

Keyboard shortcuts

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