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