voynicrypto

package module
v0.1.1 Latest Latest
Warning

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

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

README

voynicrypto

Voynicrypto provides helper functions to encrypt or decrypt.

Build Status codecov.io Code Climate Issue Count Go Report Card Apache V2 License GitHub release GoDoc

Summary

Voynicrypto provides helper functions to encrypt or decrypt.

Install

This repo is a package. There is no installation.

Contributing

Refer to CONTRIBUTING.md.

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

func GetHash(hashType string) crypto.Hash

GetHash finds a matching Hash for the string given.

func GetPrivateKey

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

GetPrivateKey uses a keyloader to load a private key.

func GetPublicKey

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

GetPublicKey uses a keyloader to load a public key.

Types

type AlgorithmType

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

func ParseAlgorithmType(algo string) AlgorithmType

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

type BasicHashLoader

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

BasicHashLoader implements HashLoader.

func (*BasicHashLoader) GetHash

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

type BoxLoader struct {
	KID        string
	PrivateKey KeyLoader
	PublicKey  KeyLoader
}

BoxLoader loads the box encryption/decryption.

func (*BoxLoader) LoadDecrypt

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

LoadDecrypt loads a decrypter for the box algorithm.

func (*BoxLoader) LoadEncrypt

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

LoadEncrypt loads an encrypter for the box algorithm.

type BytesLoader

type BytesLoader struct {
	Data []byte
}

BytesLoader implements the KeyLoader.

func (*BytesLoader) GetBytes

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

GetBytes returns the bytes stored by the BytesLoader

type Ciphers

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

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

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

func (*Ciphers) Get

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

Get returns a decrypter given an algorithm and KID.

type Config

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

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

LoadDecrypt uses the config to load a decrypter.

func (*Config) LoadEncrypt

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

LoadEncrypt uses the config to load an encrypter.

type Decrypt

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

func DefaultCipherDecrypter() Decrypt

DEfaultCipherDecrypter returns a NOOP decrypter.

func NewBoxDecrypter

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

NewBoxDecrypter returns a new box decrypter.

func NewRSADecrypter

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

NewRSADecrypter returns an RSA decrypter.

type DecryptLoader

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

DecryptLoader loads a decrypter.

type Encrypt

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

func DefaultCipherEncrypter() Encrypt

DefaultCipherEncrypter returns a NOOP encrypter.

func NewBoxEncrypter

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

NewBoxEncrypter returns a new box encrypter.

func NewRSAEncrypter

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

NewRSAEncrypter returns an RSA encrypter.

type EncryptLoader

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

EncryptLoader loads an encrypter.

type FileLoader

type FileLoader struct {
	Path string
}

FileLoader loads a key from a file.

func (*FileLoader) GetBytes

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

GetBytes returns the bytes found at the filepath.

type HashLoader

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

HashLoader can get a hash.

type Identification

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

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

KeyLoader gets the bytes for a key.

func CreateFileLoader

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

type KeyType

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

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

func (*NOOP) GetAlgorithm() AlgorithmType

GetAlgorithm returns None.

func (*NOOP) GetKID

func (*NOOP) GetKID() string

GetKID returns none.

type Options

type Options []Config

Options is the list of configurations used to load ciphers.

func FromViper

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

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

GetEncrypter takes options and creates an encrypter out of it.

type RSALoader

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

RSALoader loads the encrypter/decrypter for the RSA algorithm.

func (*RSALoader) LoadDecrypt

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

LoadDecrypt loads the RSA decrypter.

func (*RSALoader) LoadEncrypt

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

LoadEncrypt loads the RSA encrypter.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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