-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.go
More file actions
54 lines (54 loc) · 1.79 KB
/
Copy pathdoc.go
File metadata and controls
54 lines (54 loc) · 1.79 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
// Package e5t provides small AES-256-GCM encryption helpers built only on the
// Go standard library.
//
// The package encrypts and decrypts byte slices with AES in Galois/Counter Mode
// (GCM). GCM is an authenticated encryption mode: successful decryption proves
// that the ciphertext, authentication tag, and nonce match the supplied key.
//
// # Encryption Format
//
// [Encrypt] returns raw bytes in this format:
//
// nonce || ciphertext || authentication-tag
//
// [EncryptAsString] returns the same bytes encoded as hexadecimal text.
// [Decrypt] expects the raw byte format, and [DecryptFromText] expects the
// hex-encoded format.
//
// # Keys
//
// AES-256 requires a 32-byte key. [GenerateHashKey] is a convenience helper that
// returns 32 bytes by hashing a key string plus an optional salt with SHA-256:
//
// key := e5t.GenerateHashKey("application-secret", "config-v1")
//
// For higher-risk secrets or user-entered passwords, pass a 32-byte key produced
// by a dedicated key management or password-based key derivation strategy.
//
// # Basic Usage
//
// key := e5t.GenerateHashKey("my-secret-password", "unique-salt")
//
// encrypted, err := e5t.EncryptAsString([]byte("sensitive data"), key)
// if err != nil {
// log.Fatal(err)
// }
//
// decrypted, err := e5t.DecryptFromText(encrypted, key)
// if err != nil {
// log.Fatal(err)
// }
//
// fmt.Println(string(decrypted))
//
// # Error Handling
//
// The package returns [ErrInvalidKeySize] when a key is not exactly 32 bytes and
// [ErrCiphertextTooShort] when encrypted input is shorter than the nonce prefix.
// Use errors.Is to branch on these sentinel errors.
//
// # Dependencies
//
// e5t has zero third-party dependencies. It uses crypto/aes, crypto/cipher,
// crypto/rand, crypto/sha256, encoding/hex, and other standard library packages.
package e5t