Documentation
¶
Overview ¶
Example (Config) ¶
package main
import (
"fmt"
"github.com/vmvarela/ghoten/internal/encryption/keyprovider"
"github.com/vmvarela/ghoten/internal/encryption/method/aesgcm"
)
func main() {
// Obtain a modifiable, buildable config.
config := aesgcm.Config{}
// Set up an encryption key:
config.Keys = keyprovider.Output{
EncryptionKey: []byte("AiphoogheuwohShal8Aefohy7ooLeeyu"),
DecryptionKey: []byte("AiphoogheuwohShal8Aefohy7ooLeeyu"),
}
// Now you can build a method:
method, err := config.Build()
if err != nil {
panic(err)
}
// Encrypt something:
encrypted, err := method.Encrypt([]byte("Hello world!"))
if err != nil {
panic(err)
}
// Decrypt it:
decrypted, err := method.Decrypt(encrypted)
if err != nil {
panic(err)
}
fmt.Printf("%s", decrypted)
}
Output: Hello world!
Example (Config_hcl) ¶
// First, get the descriptor to make sure we always have the default values.
descriptor := aesgcm.New()
// Unmarshal HCL code into the config struct. The input must be a list of bytes, so in a real world scenario
// you may want to put in a hex-decoding function:
rawHCLInput := `keys = {
encryption_key = [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],
decryption_key = [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]
}`
file, diags := hclsyntax.ParseConfig(
[]byte(rawHCLInput),
"example.hcl",
hcl.Pos{Byte: 0, Line: 1, Column: 1},
)
if diags.HasErrors() {
panic(diags)
}
methodCtx := method.EvalContext{ValueForExpression: func(expr hcl.Expression) (cty.Value, hcl.Diagnostics) {
return expr.Value(nil)
}}
config, diags := descriptor.DecodeConfig(methodCtx, file.Body)
if diags.HasErrors() {
panic(diags)
}
// Now you can build a method:
method, err := config.Build()
if err != nil {
panic(err)
}
// Encrypt something:
encrypted, err := method.Encrypt([]byte("Hello world!"))
if err != nil {
panic(err)
}
// Decrypt it:
decrypted, err := method.Decrypt(encrypted)
if err != nil {
panic(err)
}
fmt.Printf("%s", decrypted)
Output: Hello world!
Example (HandlePanic) ¶
_, err := handlePanic(func() ([]byte, error) {
panic("Hello world!")
})
fmt.Printf("%v", err)
Output: Hello world!
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New() method.Descriptor
New creates a new descriptor for the AES-GCM encryption method, which requires a 32-byte key.
Types ¶
type Config ¶
type Config struct {
// Key is the encryption key for the AES-GCM encryption. It has to be 16, 24, or 32 bytes long for AES-128, 192, or
// 256, respectively.
Keys keyprovider.Output
// AAD is the Additional Authenticated Data that is authenticated, but not encrypted. In the Go implementation, this
// data serves as a canary value against replay attacks. The AAD value on decryption must match this setting,
// otherwise the decryption will fail. (Note: this is Go-specific and differs from the NIST SP 800-38D description
// of the AAD.)
AAD []byte
}
Config is the configuration for the AES-GCM method.
Click to show internal directories.
Click to hide internal directories.