micro/config/secrets/secrets.go

89 lines
2.1 KiB
Go
Raw Normal View History

// Package secrets is an interface for encrypting and decrypting secrets
package secrets
import "context"
2020-04-27 14:57:57 +01:00
// Secrets encrypts or decrypts arbitrary data. The data should be as small as possible
type Secrets interface {
// Initialise options
Init(...Option) error
2020-04-27 14:57:57 +01:00
// Return the options
Options() Options
2020-04-27 14:57:57 +01:00
// Decrypt a value
Decrypt([]byte, ...DecryptOption) ([]byte, error)
2020-04-27 14:57:57 +01:00
// Encrypt a value
Encrypt([]byte, ...EncryptOption) ([]byte, error)
2020-04-27 14:57:57 +01:00
// Secrets implementation
String() string
}
type Options struct {
2020-04-27 14:57:57 +01:00
// Key is a symmetric key for encoding
Key []byte
// Private key for decoding
PrivateKey []byte
2020-04-27 14:57:57 +01:00
// Public key for encoding
PublicKey []byte
// Context for other opts
Context context.Context
}
// Option sets options
type Option func(*Options)
2020-04-27 14:57:57 +01:00
// Key sets the symmetric secret key
func Key(k []byte) Option {
return func(o *Options) {
2020-04-27 14:57:57 +01:00
o.Key = make([]byte, len(k))
copy(o.Key, k)
}
}
// PublicKey sets the asymmetric Public Key of this codec
func PublicKey(key []byte) Option {
return func(o *Options) {
o.PublicKey = make([]byte, len(key))
copy(o.PublicKey, key)
}
}
// PrivateKey sets the asymmetric Private Key of this codec
func PrivateKey(key []byte) Option {
return func(o *Options) {
o.PrivateKey = make([]byte, len(key))
copy(o.PrivateKey, key)
}
}
2020-04-27 14:57:57 +01:00
// DecryptOptions can be passed to Secrets.Decrypt
type DecryptOptions struct {
SenderPublicKey []byte
}
// DecryptOption sets DecryptOptions
type DecryptOption func(*DecryptOptions)
2020-04-27 14:57:57 +01:00
// SenderPublicKey is the Public Key of the Secrets that encrypted this message
func SenderPublicKey(key []byte) DecryptOption {
return func(d *DecryptOptions) {
d.SenderPublicKey = make([]byte, len(key))
copy(d.SenderPublicKey, key)
}
}
2020-04-27 14:57:57 +01:00
// EncryptOptions can be passed to Secrets.Encrypt
type EncryptOptions struct {
RecipientPublicKey []byte
}
// EncryptOption Sets EncryptOptions
type EncryptOption func(*EncryptOptions)
2020-04-27 14:57:57 +01:00
// RecipientPublicKey is the Public Key of the Secrets that will decrypt this message
func RecipientPublicKey(key []byte) EncryptOption {
return func(e *EncryptOptions) {
e.RecipientPublicKey = make([]byte, len(key))
copy(e.RecipientPublicKey, key)
}
}