Skip to content

Commit 33cd35d

Browse files
committed
docs: module, client, clientconfig Go documentation
1 parent ea822df commit 33cd35d

File tree

11 files changed

+136
-10
lines changed

11 files changed

+136
-10
lines changed

Diff for: doc.go

+69
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,73 @@
11
// Copyright Chainify Group LTD. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
// Package sdk is the Unofficial Go SDK implementation of the AWS Encryption SDK.
5+
//
6+
// # Getting started
7+
//
8+
// To install the AWS Encryption SDK for Go, use the following command:
9+
//
10+
// go get github.com/chainifynet/aws-encryption-sdk-go@latest
11+
//
12+
// # Usage
13+
//
14+
// The following example demonstrates how to use SDK to encrypt and decrypt data using a static key.
15+
//
16+
// package main
17+
//
18+
// import (
19+
// "context"
20+
// "fmt"
21+
//
22+
// "github.com/chainifynet/aws-encryption-sdk-go/pkg/client"
23+
// "github.com/chainifynet/aws-encryption-sdk-go/pkg/materials"
24+
// "github.com/chainifynet/aws-encryption-sdk-go/pkg/providers/rawprovider"
25+
// )
26+
//
27+
// func main() {
28+
// // static key to use for encryption and decryption
29+
// staticKey1 := []byte("superSecureKeySecureKey32bytes32")
30+
//
31+
// // data to encrypt
32+
// secretData := []byte("secret data to encrypt")
33+
//
34+
// // setup Encryption SDK client with default configuration
35+
// sdkClient := client.NewClient()
36+
//
37+
// // setup Raw Key provider
38+
// rawKeyProvider, err := rawprovider.NewWithOpts(
39+
// "raw",
40+
// rawprovider.WithStaticKey("static1", staticKey1),
41+
// )
42+
// if err != nil {
43+
// panic(err) // handle error
44+
// }
45+
//
46+
// // setup crypto materials manager
47+
// cmm, err := materials.NewDefault(rawKeyProvider)
48+
// if err != nil {
49+
// panic(err) // handle error
50+
// }
51+
//
52+
// // encrypt data without encryption context passing nil as the third argument
53+
// encrypted, header, err := sdkClient.Encrypt(context.TODO(), secretData, nil, cmm)
54+
// if err != nil {
55+
// panic(err) // handle error
56+
// }
57+
//
58+
// fmt.Printf("encrypted encryption context: %v\n", header.AADData().EncryptionContext())
59+
//
60+
// // decrypt "encrypted" data
61+
// decrypted, _, err := sdkClient.Decrypt(context.TODO(), encrypted, cmm)
62+
// if err != nil {
63+
// panic(err) // handle error
64+
// }
65+
//
66+
// fmt.Printf("decrypted data: %s\n", decrypted)
67+
//
68+
// // verify that "decrypted" plaintext is identical to the original secret data
69+
// if string(decrypted) != string(secretData) {
70+
// panic("decrypted data does not match with the original data")
71+
// }
72+
// }
473
package sdk

Diff for: pkg/client/doc.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright Chainify Group LTD. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package client provides the entrypoint for using AWS Encryption SDK for Go.
5+
package client

Diff for: pkg/client/options.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
)
1313

1414
const (
15-
DefaultFrameLength = int(4096) // default frame size for encryption
15+
// DefaultFrameLength default frame length for encryption.
16+
DefaultFrameLength = int(4096)
1617
)
1718

1819
// EncryptOptions defines the configuration options for the encryption process.
@@ -25,9 +26,12 @@ const (
2526
// - Handler: Specifies a function that creates [model.EncryptionHandler] encryption handler.
2627
// If not set, a default [encrypter.New] function is used.
2728
type EncryptOptions struct {
28-
Algorithm *suite.AlgorithmSuite
29+
// Algorithm that defines the encryption algorithm to be used.
30+
Algorithm *suite.AlgorithmSuite
31+
// FrameLength specifies the frame length for encryption.
2932
FrameLength int
30-
Handler func(config crypto.EncrypterConfig, cmm model.CryptoMaterialsManager) model.EncryptionHandler
33+
// Handler specifies a function that creates model.EncryptionHandler encryption handler.
34+
Handler func(config crypto.EncrypterConfig, cmm model.CryptoMaterialsManager) model.EncryptionHandler
3135
}
3236

3337
// DecryptOptions defines the configuration options for the decryption process.
@@ -36,6 +40,7 @@ type EncryptOptions struct {
3640
// - Handler: Specifies a function that creates [model.DecryptionHandler] decryption handler.
3741
// If not set, a default [decrypter.New] function is used.
3842
type DecryptOptions struct {
43+
// Handler specifies a function that creates model.DecryptionHandler decryption handler.
3944
Handler func(config crypto.DecrypterConfig, cmm model.CryptoMaterialsManager) model.DecryptionHandler
4045
}
4146

Diff for: pkg/clientconfig/client_config.go

+28-3
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,56 @@ import (
1010
"github.com/chainifynet/aws-encryption-sdk-go/pkg/suite"
1111
)
1212

13+
// ConfigOptions is a holder of the configuration options for the client.
1314
type ConfigOptions struct {
14-
CommitmentPolicy suite.CommitmentPolicy
15+
// CommitmentPolicy is the commitment policy for the client.
16+
CommitmentPolicy suite.CommitmentPolicy
17+
18+
// MaxEncryptedDataKeys is the maximum number of encrypted data keys that can be used in a single message.
1519
MaxEncryptedDataKeys int
1620
}
1721

22+
// ConfigOptionFunc is a function that sets a configuration option.
1823
type ConfigOptionFunc func(o *ConfigOptions) error
1924

25+
// ClientConfig is the configuration for the client.
2026
type ClientConfig struct {
2127
commitmentPolicy suite.CommitmentPolicy
2228
maxEncryptedDataKeys int
2329
}
2430

31+
// CommitmentPolicy returns the commitment policy for the client.
2532
func (c ClientConfig) CommitmentPolicy() suite.CommitmentPolicy {
2633
return c.commitmentPolicy
2734
}
2835

36+
// MaxEncryptedDataKeys returns the maximum number of encrypted data keys that can be used in a single message.
2937
func (c ClientConfig) MaxEncryptedDataKeys() int {
3038
return c.maxEncryptedDataKeys
3139
}
3240

41+
// NewConfig returns a new client configuration with the default options.
3342
func NewConfig() (*ClientConfig, error) {
3443
return NewConfigWithOpts()
3544
}
3645

46+
// NewConfigWithOpts returns a new client configuration with the provided options.
47+
// The options are passed as [ConfigOptionFunc] functions, which modify the [ConfigOptions] struct.
48+
//
49+
// The default values for the configuration options are as follows:
50+
// - CommitmentPolicy: [DefaultCommitment]
51+
// - MaxEncryptedDataKeys: [DefaultMaxEDK]
52+
//
53+
// Example usage:
54+
//
55+
// cfg, err := NewConfigWithOpts(
56+
// WithCommitmentPolicy(suite.CommitmentPolicyRequireEncryptAllowDecrypt),
57+
// WithMaxEncryptedDataKeys(5),
58+
// )
3759
func NewConfigWithOpts(optFns ...ConfigOptionFunc) (*ClientConfig, error) {
3860
opts := ConfigOptions{
39-
CommitmentPolicy: defaultCommitment,
40-
MaxEncryptedDataKeys: defaultMaxEDK,
61+
CommitmentPolicy: DefaultCommitment,
62+
MaxEncryptedDataKeys: DefaultMaxEDK,
4163
}
4264
for _, optFn := range optFns {
4365
if err := optFn(&opts); err != nil {
@@ -50,6 +72,7 @@ func NewConfigWithOpts(optFns ...ConfigOptionFunc) (*ClientConfig, error) {
5072
}, nil
5173
}
5274

75+
// WithCommitmentPolicy returns a [ConfigOptionFunc] that sets the commitment policy for the client.
5376
func WithCommitmentPolicy(policy suite.CommitmentPolicy) ConfigOptionFunc {
5477
return func(o *ConfigOptions) error {
5578
if err := suite.ValidateCommitmentPolicy(policy); err != nil {
@@ -60,6 +83,8 @@ func WithCommitmentPolicy(policy suite.CommitmentPolicy) ConfigOptionFunc {
6083
}
6184
}
6285

86+
// WithMaxEncryptedDataKeys returns a [ConfigOptionFunc] that sets the maximum
87+
// number of encrypted data keys that can be used in a single message.
6388
func WithMaxEncryptedDataKeys(maxEncryptedDataKeys int) ConfigOptionFunc {
6489
return func(o *ConfigOptions) error {
6590
if maxEncryptedDataKeys > math.MaxUint8 {

Diff for: pkg/clientconfig/client_config_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ func TestNewConfig(t *testing.T) {
1515
defaultCfg, err := NewConfig()
1616
assert.NoError(t, err)
1717

18-
assert.Equal(t, defaultCommitment, defaultCfg.CommitmentPolicy())
19-
assert.Equal(t, defaultMaxEDK, defaultCfg.MaxEncryptedDataKeys())
18+
assert.Equal(t, DefaultCommitment, defaultCfg.CommitmentPolicy())
19+
assert.Equal(t, DefaultMaxEDK, defaultCfg.MaxEncryptedDataKeys())
2020
}
2121

2222
func TestNewConfigWithOpts(t *testing.T) {

Diff for: pkg/clientconfig/client_defaults.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ package clientconfig
66
import "github.com/chainifynet/aws-encryption-sdk-go/pkg/suite"
77

88
const (
9-
defaultCommitment = suite.CommitmentPolicyRequireEncryptRequireDecrypt
10-
defaultMaxEDK = 10
9+
// DefaultCommitment is the default commitment policy for the client.
10+
DefaultCommitment = suite.CommitmentPolicyRequireEncryptRequireDecrypt
11+
12+
// DefaultMaxEDK is the default maximum number of encrypted data keys that can be
13+
// used to encrypt or decrypt a single message.
14+
DefaultMaxEDK = 10
1115
)

Diff for: pkg/clientconfig/doc.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright Chainify Group LTD. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package clientconfig provides a way to configure SDK client.
5+
package clientconfig

Diff for: pkg/crypto/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import (
88
"github.com/chainifynet/aws-encryption-sdk-go/pkg/suite"
99
)
1010

11+
// EncrypterConfig is the configuration for the encrypter.
1112
type EncrypterConfig struct {
1213
ClientCfg clientconfig.ClientConfig
1314
Algorithm *suite.AlgorithmSuite
1415
FrameLength int
1516
}
1617

18+
// DecrypterConfig is the configuration for the decrypter.
1719
type DecrypterConfig struct {
1820
ClientCfg clientconfig.ClientConfig
1921
}

Diff for: pkg/crypto/doc.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright Chainify Group LTD. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package crypto provides common errors and encryption configuration.
5+
package crypto

Diff for: pkg/doc.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright Chainify Group LTD. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package pkg provides the core SDK packages.
5+
package pkg

Diff for: pkg/version.go

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33

44
package pkg
55

6+
// Version is the current version of the SDK.
67
const Version = "0.3.2"

0 commit comments

Comments
 (0)