Documentation
¶
Overview ¶
Package encrypt provides AES-256-GCM authenticated encryption and decryption.
The encrypted output is encoded as hex then base64, with the nonce prepended to the ciphertext. This package uses crypto/rand for nonce generation, ensuring each encryption produces unique output even for identical plaintext.
Key requirements: AES-256 requires a 32-byte key. AES-128 (16-byte) and AES-192 (24-byte) keys are also accepted by the underlying cipher.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecryptAES256GCM ¶
DecryptAES256GCM decrypts ciphertext that was encrypted with EncryptAES256GCM using the same 32-byte key.
func EncryptAES256GCM ¶
EncryptAES256GCM encrypts plaintext using AES-256-GCM with the provided 32-byte key. The result is base64-encoded and includes the nonce prepended to the ciphertext.
Example ¶
package main
import (
"fmt"
"github.com/alpardfm/go-toolkit/encrypt"
)
func main() {
// AES-256 requires a 32-byte key
key := []byte("01234567890123456789012345678901")
plaintext := []byte("hello, world!")
// Encrypt the plaintext
ciphertext, err := encrypt.EncryptAES256GCM(plaintext, key)
if err != nil {
fmt.Println("error:", err)
return
}
// Decrypt it back
decrypted, err := encrypt.DecryptAES256GCM(ciphertext, key)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(string(decrypted))
}
Output: hello, world!
Types ¶
This section is empty.