Documentation
¶
Index ¶
- Constants
- type BaseClient
- type Client
- func (c *Client) Decrypt(ctx context.Context, ciphertext []byte, ...) ([]byte, format.MessageHeader, error)
- func (c *Client) Encrypt(ctx context.Context, source []byte, ec suite.EncryptionContext, ...) ([]byte, format.MessageHeader, error)
- func (c *Client) EncryptWithParams(ctx context.Context, source []byte, ec suite.EncryptionContext, ...) ([]byte, format.MessageHeader, error)
- type EncryptOptionFunc
- type EncryptOptions
Examples ¶
Constants ¶
const (
DefaultFrameLength = int(4096) // default frame size for encryption
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseClient ¶
type BaseClient interface {
Encrypt(ctx context.Context, source []byte, ec suite.EncryptionContext, materialsManager model.CryptoMaterialsManager, optFns ...EncryptOptionFunc) ([]byte, format.MessageHeader, error)
EncryptWithParams(ctx context.Context, source []byte, ec suite.EncryptionContext, materialsManager model.CryptoMaterialsManager, algorithm *suite.AlgorithmSuite, frameLength int) ([]byte, format.MessageHeader, error)
Decrypt(ctx context.Context, ciphertext []byte, materialsManager model.CryptoMaterialsManager) ([]byte, format.MessageHeader, error)
// contains filtered or unexported methods
}
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewClient ¶
func NewClient() *Client
NewClient returns a new client with default clientconfig.ClientConfig config
Example ¶
package main
import (
"fmt"
"github.com/chainifynet/aws-encryption-sdk-go/pkg/client"
)
func main() {
var c = client.NewClient()
fmt.Printf("%#v", *c)
}
Output: client.Client{config:clientconfig.ClientConfig{commitmentPolicy:3, maxEncryptedDataKeys:10}}
func NewClientWithConfig ¶
func NewClientWithConfig(cfg *clientconfig.ClientConfig) *Client
NewClientWithConfig returns a new client with cfg clientconfig.ClientConfig
Example ¶
package main
import (
"fmt"
"github.com/chainifynet/aws-encryption-sdk-go/pkg/client"
"github.com/chainifynet/aws-encryption-sdk-go/pkg/clientconfig"
"github.com/chainifynet/aws-encryption-sdk-go/pkg/suite"
)
func main() {
cfg, err := clientconfig.NewConfigWithOpts(
clientconfig.WithCommitmentPolicy(suite.CommitmentPolicyRequireEncryptRequireDecrypt),
clientconfig.WithMaxEncryptedDataKeys(2),
)
if err != nil {
panic(err)
}
var c = client.NewClientWithConfig(cfg)
fmt.Printf("%#v", *c)
}
Output: client.Client{config:clientconfig.ClientConfig{commitmentPolicy:3, maxEncryptedDataKeys:2}}
func (*Client) Decrypt ¶
func (c *Client) Decrypt(ctx context.Context, ciphertext []byte, materialsManager model.CryptoMaterialsManager) ([]byte, format.MessageHeader, error)
Decrypt decrypts the given ciphertext using the provided materials manager. It returns the decrypted plaintext and the message header.
Parameters:
- ctx: context.Context.
- ciphertext []byte: The data to decrypt.
- materialsManager model.CryptoMaterialsManager: The manager that provides the cryptographic materials.
Returns:
- []byte: The decrypted data.
- format.MessageHeader: The header of the encrypted message.
- error: An error if decryption fails.
func (*Client) Encrypt ¶
func (c *Client) Encrypt(ctx context.Context, source []byte, ec suite.EncryptionContext, materialsManager model.CryptoMaterialsManager, optFns ...EncryptOptionFunc) ([]byte, format.MessageHeader, error)
Encrypt encrypts the given source data using the provided materials manager and encryption context. By default, it uses the algorithm suite.AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384 and a frame length of 4096.
This behavior can be modified by passing in optional functional arguments using EncryptOptionFunc. It returns the ciphertext data along with the message header.
Parameters:
- ctx context.Context: The context for the operation.
- source []byte: The data to encrypt.
- ec suite.EncryptionContext: The encryption context, a set of key-value pairs that are cryptographically bound to the encrypted data.
- materialsManager model.CryptoMaterialsManager: The manager that provides the cryptographic materials.
- optFns EncryptOptionFunc: A variadic set of optional functions for configuring encryption options such as custom algorithm or frame length.
Returns:
- []byte: The encrypted data.
- format.MessageHeader: The header of the encrypted message.
- error: An error if encryption fails.
Example usage:
ciphertext, header, err := client.Encrypt(context.TODO(), plaintext, encryptionContext, materialsManager,
WithAlgorithm(customAlgorithm),
WithFrameLength(1024))
if err != nil {
// handle error
}
Notes:
- The Encrypt function allows customization of the encryption process through its optional parameters.
- The WithAlgorithm and WithFrameLength functions can be used to specify an encryption algorithm and frame length, respectively. If these functions are not used, default values are applied.
func (*Client) EncryptWithParams ¶
func (c *Client) EncryptWithParams(ctx context.Context, source []byte, ec suite.EncryptionContext, materialsManager model.CryptoMaterialsManager, algorithm *suite.AlgorithmSuite, frameLength int) ([]byte, format.MessageHeader, error)
EncryptWithParams is similar to Encrypt but allows specifying additional options such as the algorithm suite and frame length as arguments instead of functional EncryptOptionFunc options.
Parameters:
- ctx context.Context: The context for the operation.
- source []byte: The data to encrypt.
- ec suite.EncryptionContext: The encryption context.
- materialsManager model.CryptoMaterialsManager: The manager that provides the cryptographic materials.
- algorithm suite.AlgorithmSuite: The algorithm suite to use for encryption.
- frameLength int: The frame length for encryption.
Returns:
- []byte: The encrypted data.
- format.MessageHeader: The header of the encrypted message.
- error: An error if encryption fails.
type EncryptOptionFunc ¶
type EncryptOptionFunc func(o *EncryptOptions) error
EncryptOptionFunc is a function type that applies a configuration option to an EncryptOptions struct. It is used to customize the encryption process by setting various options in EncryptOptions.
Each function of this type takes a pointer to an EncryptOptions struct and modifies it accordingly. It returns an error if the provided option is invalid or cannot be applied.
Use WithAlgorithm and WithFrameLength to create EncryptOptionFunc functions.
func WithAlgorithm ¶
func WithAlgorithm(alg *suite.AlgorithmSuite) EncryptOptionFunc
WithAlgorithm returns an EncryptOptionFunc that sets the encryption algorithm in EncryptOptions. This function allows the caller to specify a custom algorithm for encryption.
Parameters:
- alg suite.AlgorithmSuite: suite.AlgorithmSuite which defines the encryption algorithm to be used.
Returns:
- EncryptOptionFunc: A function that sets the Algorithm field in EncryptOptions.
Errors:
- If alg is nil, it returns an error indicating that the algorithm must not be nil.
func WithFrameLength ¶
func WithFrameLength(frameLength int) EncryptOptionFunc
WithFrameLength returns an EncryptOptionFunc that sets the frame length in EncryptOptions. This function allows the caller to specify a custom frame length for encryption, within allowed limits.
Parameters:
- frameLength int: The frame length to be set for encryption.
Returns:
- EncryptOptionFunc: A function that sets the FrameLength field in EncryptOptions.
Errors:
- If frameLength is less than suite.MinFrameSize (128) or greater than suite.MaxFrameSize (2147483647), it returns an error indicating that the frame length is out of range.
- If frameLength is not a multiple of the suite.BlockSize (128) of the crypto algorithm, it returns an error indicating that.
type EncryptOptions ¶
type EncryptOptions struct {
Algorithm *suite.AlgorithmSuite
FrameLength int
}
EncryptOptions defines the configuration options for the encryption process. It contains settings such as the algorithm to use for encryption and the frame length.
Fields:
- Algorithm suite.AlgorithmSuite: AlgorithmSuite that defines the encryption algorithm to be used. If nil, a default suite.AES_256_GCM_HKDF_SHA512_COMMIT_KEY_ECDSA_P384 algorithm is used.
- FrameLength int: Specifies the frame length for encryption. If not set, a default value of DefaultFrameLength is used.