-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.go
More file actions
229 lines (228 loc) · 7.01 KB
/
Copy pathdoc.go
File metadata and controls
229 lines (228 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Package encx provides production-ready field-level encryption, hashing, and key management for Go applications.
//
// Context7 Metadata:
// - Library Type: Encryption & Security
// - Use Cases: Data protection, PII encryption, password hashing, searchable encryption
// - Complexity: Intermediate to Advanced
// - Performance: High (10x improvement with code generation)
// - Compliance: HIPAA, GDPR, SOX ready
// - Integration: PostgreSQL, MySQL, SQLite, AWS KMS, HashiCorp Vault
//
// ENCX enables you to encrypt and hash struct fields using simple struct tags, with automatic
// key management through a DEK/KEK architecture. It supports multiple KMS backends, key rotation,
// combined operations, and comprehensive testing utilities.
//
// # Key Features
//
// - Field-level encryption with AES-GCM
// - Secure hashing with Argon2id and basic SHA-256
// - Combined operations - encrypt AND hash the same field
// - Automatic key management with DEK/KEK architecture
// - Key rotation support with version tracking
// - Multiple KMS backends (AWS KMS, HashiCorp Vault, etc.)
// - Comprehensive testing utilities and mocks
// - Compile-time validation for struct tags
//
// # Quick Start
//
// Define your struct with encx tags:
//
// type User struct {
// Name string `encx:"encrypt"`
// Email string `encx:"hash_basic"`
// Password string `encx:"hash_secure"`
//
// // No companion fields needed! Code generation creates separate output struct
// }
//
// Generate type-safe functions (recommended approach):
//
// //go:generate encx-gen generate .
//
// Create crypto instance and process with generated functions:
//
// crypto, _ := encx.NewTestCrypto(nil)
// user := &User{
// Name: "John Doe",
// Email: "john@example.com",
// Password: "secret123",
// }
//
// // Use generated type-safe functions
// userEncx, err := ProcessUserEncx(ctx, crypto, user)
// orderEncx, err := ProcessOrderEncx(ctx, crypto, order)
// // Pattern: Process{YourStructName}Encx
//
// # Struct Tags
//
// Single operation tags:
// - encx:"encrypt" - Encrypts field value
// - encx:"hash_basic" - Creates SHA-256 hash for searchable indexing
// - encx:"hash_secure" - Creates Argon2id hash with pepper (for passwords)
//
// Combined operation tags:
// - encx:"encrypt,hash_basic" - Both encrypts AND hashes the field (searchable encryption)
// - encx:"hash_secure,encrypt" - Secure hash for auth + encryption for recovery
//
// # Code Generation
//
// Code generation creates a separate {StructName}Encx struct with all encrypted/hashed fields:
//
// // Your source struct
// type User struct {
// Email string `encx:"encrypt,hash_basic"`
// }
//
// // Generated UserEncx struct (automatic)
// type UserEncx struct {
// EmailEncrypted []byte
// EmailHash string
// DEKEncrypted []byte
// KeyVersion int
// Metadata string
// }
//
// # Advanced Example: Combined Tags
//
// Perfect for user lookup with privacy protection:
//
// type User struct {
// Email string `encx:"encrypt,hash_basic"`
//
// // No companion fields needed! Code generation creates:
// // - UserEncx.EmailEncrypted []byte (for secure storage)
// // - UserEncx.EmailHash string (for fast lookups)
// }
//
// // Usage
// user := &User{Email: "user@example.com"}
// userEncx, err := ProcessUserEncx(ctx, crypto, user)
//
// // Now you can:
// // 1. Store userEncx.EmailEncrypted securely in database
// // 2. Use userEncx.EmailHash for fast user lookups
// // 3. Decrypt with DecryptUserEncx when needed for display
//
// # Production Configuration
//
// // With AWS KMS
// crypto, err := encx.NewCrypto(ctx,
// encx.WithKMSService(awsKMS),
// encx.WithDatabase(db),
// encx.WithPepper(pepper),
// encx.WithKEKAlias("myapp-kek"),
// )
//
// // With HashiCorp Vault
// crypto, err := encx.NewCrypto(ctx,
// encx.WithKMSService(vaultKMS),
// encx.WithDatabase(db),
// encx.WithPepper(pepper),
// encx.WithKEKAlias("myapp-kek"),
// )
//
// # Validation
//
// Validate struct tags before generating code:
//
// encx-gen validate -v .
// encx-gen validate -v ./models ./api
//
// Validation runs automatically before generation:
//
// encx-gen generate -v .
//
// # Error Handling
//
// ENCX provides structured error handling with sentinel errors for precise error classification:
//
// user := &User{Name: "John", Email: "john@example.com"}
// userEncx, err := ProcessUserEncx(ctx, crypto, user)
// if err != nil {
// switch {
// case encx.IsRetryableError(err):
// // KMS or database temporarily unavailable - retry with backoff
// log.Warn("Retryable error: %v", err)
// return handleRetry(err)
//
// case encx.IsConfigurationError(err):
// // Invalid configuration - fix setup
// log.Error("Configuration error: %v", err)
// return handleConfigError(err)
//
// case encx.IsAuthError(err):
// // Authentication failed - check credentials
// log.Error("Authentication failed: %v", err)
// return handleAuthError(err)
//
// case encx.IsOperationError(err):
// // Encryption/decryption failed - check data/keys
// log.Error("Operation failed: %v", err)
// return handleOperationError(err)
//
// case encx.IsValidationError(err):
// // Data validation failed - check input
// log.Error("Validation error: %v", err)
// return handleValidationError(err)
//
// default:
// log.Error("Unknown error: %v", err)
// return err
// }
// }
//
// Checking specific errors:
//
// if errors.Is(err, encx.ErrKMSUnavailable) {
// // Implement retry logic
// return retryWithBackoff(operation)
// }
//
// if errors.Is(err, encx.ErrAuthenticationFailed) {
// // Refresh credentials and retry
// return refreshAuthAndRetry(operation)
// }
//
// # Testing
//
// Unit testing with generated functions:
//
// func TestUserEncryption(t *testing.T) {
// crypto, _ := encx.NewTestCrypto(t)
//
// user := &User{Email: "test@example.com"}
// userEncx, err := ProcessUserEncx(ctx, crypto, user)
//
// assert.NoError(t, err)
// assert.NotEmpty(t, userEncx.EmailEncrypted)
// }
//
// Integration testing with full cycle:
//
// func TestUserEncryptDecryptCycle(t *testing.T) {
// crypto, _ := encx.NewTestCrypto(t)
//
// original := &User{Email: "test@example.com"}
// userEncx, err := ProcessUserEncx(ctx, crypto, original)
// assert.NoError(t, err)
//
// decrypted, err := DecryptUserEncx(ctx, crypto, userEncx)
// assert.NoError(t, err)
// assert.Equal(t, original.Email, decrypted.Email)
// }
//
// # Documentation
//
// For comprehensive documentation, examples, and advanced usage:
// - README.md - Complete getting started guide
// - docs/EXAMPLES.md - Detailed examples for all use cases
// - docs/API.md - Complete API reference
// - docs/MIGRATION.md - Version upgrade guide
// - docs/TROUBLESHOOTING.md - Common issues and solutions
//
// # Important: Version Control
//
// Add to your .gitignore:
//
// .encx/
package encx