Documentation
ΒΆ
Overview ΒΆ
Package gocrypt provides Jasypt-like encryption/decryption for Go configurations. It supports the ENC(...) pattern commonly used in Spring Boot applications.
GoCrypt offers three encryption algorithms:
- Encryptor: AES-256-GCM (recommended for new projects)
- JasyptEncryptor: PBEWithMD5AndDES (compatible with Java Jasypt default)
- JasyptStrongEncryptor: PBEWithHmacSHA256AndAES_256 (Jasypt strong encryption)
Basic usage:
enc, err := gocrypt.NewEncryptor("myPassword")
if err != nil {
log.Fatal(err)
}
// Encrypt
encrypted, _ := enc.EncryptWithPrefix("secret_value")
// Result: ENC(base64encodedvalue...)
// Decrypt
decrypted, _ := enc.DecryptPrefixed(encrypted)
// Result: secret_value
For Java Jasypt compatibility:
enc, _ := gocrypt.NewJasyptEncryptor("myPassword")
decrypted, _ := enc.DecryptPrefixed("ENC(valueFromJava)")
Made with β€οΈ from Claude AI for Golang developers who need Jasypt.
Package gocrypt provides Jasypt-compatible encryption/decryption. This file implements the original Jasypt algorithms for backward compatibility.
Index ΒΆ
- Constants
- Variables
- func IsEncrypted(value string) bool
- type ConfigLoader
- type Encryptor
- func (e *Encryptor) Decrypt(encoded string) (string, error)
- func (e *Encryptor) DecryptAllInString(input string) (string, error)
- func (e *Encryptor) DecryptMap(config map[string]string) (map[string]string, error)
- func (e *Encryptor) DecryptPrefixed(value string) (string, error)
- func (e *Encryptor) Encrypt(plaintext string) (string, error)
- func (e *Encryptor) EncryptWithPrefix(plaintext string) (string, error)
- type JasyptEncryptor
- func (e *JasyptEncryptor) Decrypt(encoded string) (string, error)
- func (e *JasyptEncryptor) DecryptAllInString(input string) (string, error)
- func (e *JasyptEncryptor) DecryptMap(config map[string]string) (map[string]string, error)
- func (e *JasyptEncryptor) DecryptPrefixed(value string) (string, error)
- func (e *JasyptEncryptor) Encrypt(plaintext string) (string, error)
- func (e *JasyptEncryptor) EncryptWithPrefix(plaintext string) (string, error)
- type JasyptOption
- type JasyptStrongEncryptor
- func (e *JasyptStrongEncryptor) Decrypt(encoded string) (string, error)
- func (e *JasyptStrongEncryptor) DecryptAllInString(input string) (string, error)
- func (e *JasyptStrongEncryptor) DecryptMap(config map[string]string) (map[string]string, error)
- func (e *JasyptStrongEncryptor) DecryptPrefixed(value string) (string, error)
- func (e *JasyptStrongEncryptor) Encrypt(plaintext string) (string, error)
- func (e *JasyptStrongEncryptor) EncryptWithPrefix(plaintext string) (string, error)
- type JasyptStrongOption
- type Option
Constants ΒΆ
const ( // DefaultIterations for PBKDF2 key derivation DefaultIterations = 10000 // DefaultSaltSize in bytes DefaultSaltSize = 16 // DefaultKeySize in bytes (256 bits for AES-256) DefaultKeySize = 32 // EncPrefix is the prefix for encrypted values EncPrefix = "ENC(" // EncSuffix is the suffix for encrypted values EncSuffix = ")" )
const ( // JasyptDefaultIterations is the default iteration count for Jasypt JasyptDefaultIterations = 1000 // JasyptSaltSize is the salt size used by Jasypt (8 bytes for DES) JasyptSaltSize = 8 )
Variables ΒΆ
var ( ErrEmptyPassword = errors.New("password cannot be empty") ErrEmptyValue = errors.New("value cannot be empty") ErrInvalidEncFormat = errors.New("invalid encrypted format, expected ENC(...)") ErrDecryptionFailed = errors.New("decryption failed") )
var (
ErrInvalidJasyptData = errors.New("invalid jasypt encrypted data")
)
Functions ΒΆ
func IsEncrypted ΒΆ
IsEncrypted checks if a value is in ENC(...) format.
Example:
if gocrypt.IsEncrypted(value) {
decrypted, _ := enc.DecryptPrefixed(value)
}
Types ΒΆ
type ConfigLoader ΒΆ
type ConfigLoader struct {
// contains filtered or unexported fields
}
ConfigLoader handles loading and decrypting configuration files. It automatically decrypts any value with ENC(...) prefix.
func NewConfigLoader ΒΆ
func NewConfigLoader(password string, opts ...Option) (*ConfigLoader, error)
NewConfigLoader creates a new ConfigLoader with the given password.
Example:
loader, err := gocrypt.NewConfigLoader(os.Getenv("GOCRYPT_PASSWORD"))
config, err := loader.LoadEnvFile("config.env")
func (*ConfigLoader) LoadEnvFile ΒΆ
func (c *ConfigLoader) LoadEnvFile(filepath string) (map[string]string, error)
LoadEnvFile loads and decrypts a .env file. Returns a map of key-value pairs with all ENC(...) values decrypted.
Example:
config, err := loader.LoadEnvFile(".env")
dbPassword := config["DATABASE_PASSWORD"]
func (*ConfigLoader) LoadJSON ΒΆ
func (c *ConfigLoader) LoadJSON(filepath string, out interface{}) error
LoadJSON loads and decrypts a JSON configuration file into the given struct. All string fields containing ENC(...) values are automatically decrypted.
func (*ConfigLoader) LoadYAML ΒΆ
func (c *ConfigLoader) LoadYAML(filepath string) (map[string]string, error)
LoadYAML loads and decrypts a simple YAML configuration file. Returns a map of key-value pairs.
Note: This is a simplified YAML parser that handles basic key-value pairs. For complex YAML structures, use a full YAML library like gopkg.in/yaml.v3
func (*ConfigLoader) SetToEnv ΒΆ
func (c *ConfigLoader) SetToEnv(filepath string) error
SetToEnv loads an env file and sets the values as environment variables. All ENC(...) values are decrypted before being set.
Example:
loader.SetToEnv(".env")
dbPassword := os.Getenv("DATABASE_PASSWORD")
type Encryptor ΒΆ
type Encryptor struct {
// contains filtered or unexported fields
}
Encryptor handles encryption and decryption operations using AES-256-GCM. This is the recommended encryptor for new projects as it provides authenticated encryption with associated data (AEAD).
func NewEncryptor ΒΆ
NewEncryptor creates a new Encryptor with the given password and options. This encryptor uses AES-256-GCM which provides authenticated encryption.
Example:
enc, err := gocrypt.NewEncryptor("myPassword",
gocrypt.WithIterations(50000),
gocrypt.WithSaltSize(32),
)
func (*Encryptor) DecryptAllInString ΒΆ
DecryptAllInString decrypts all ENC(...) values in a string. Useful for processing configuration files or templates.
Example:
input := "password=ENC(xxx) api_key=ENC(yyy)" output, _ := enc.DecryptAllInString(input) // Result: "password=secret1 api_key=secret2"
func (*Encryptor) DecryptMap ΒΆ
DecryptMap decrypts all ENC(...) values in a map. Non-encrypted values are copied as-is.
Example:
config := map[string]string{
"host": "localhost",
"password": "ENC(xxx)",
}
decrypted, _ := enc.DecryptMap(config)
func (*Encryptor) DecryptPrefixed ΒΆ
DecryptPrefixed decrypts a value with ENC(...) prefix.
Example:
decrypted, err := enc.DecryptPrefixed("ENC(base64value...)")
func (*Encryptor) Encrypt ΒΆ
Encrypt encrypts the plaintext and returns base64-encoded ciphertext. Each encryption produces a different output due to random salt and nonce.
func (*Encryptor) EncryptWithPrefix ΒΆ
EncryptWithPrefix encrypts and wraps with ENC(...) prefix. This format is compatible with Jasypt's property encryption pattern.
Example:
encrypted, _ := enc.EncryptWithPrefix("mySecret")
// Result: ENC(base64value...)
type JasyptEncryptor ΒΆ
type JasyptEncryptor struct {
// contains filtered or unexported fields
}
JasyptEncryptor provides compatibility with original Jasypt encryption. It uses PBEWithMD5AndDES algorithm which is the default in Jasypt.
WARNING: This algorithm is considered weak by modern standards. Use only for backward compatibility with existing Jasypt-encrypted values.
Example:
enc, _ := gocrypt.NewJasyptEncryptor("myPassword")
decrypted, _ := enc.DecryptPrefixed("ENC(valueFromJava)")
func NewJasyptEncryptor ΒΆ
func NewJasyptEncryptor(password string, opts ...JasyptOption) (*JasyptEncryptor, error)
NewJasyptEncryptor creates a new Jasypt-compatible encryptor. This uses PBEWithMD5AndDES algorithm for compatibility with Java Jasypt.
Example:
enc, err := gocrypt.NewJasyptEncryptor("myPassword")
enc, err := gocrypt.NewJasyptEncryptor("myPassword", gocrypt.WithJasyptIterations(2000))
func (*JasyptEncryptor) Decrypt ΒΆ
func (e *JasyptEncryptor) Decrypt(encoded string) (string, error)
Decrypt decrypts Jasypt-encrypted data
func (*JasyptEncryptor) DecryptAllInString ΒΆ
func (e *JasyptEncryptor) DecryptAllInString(input string) (string, error)
DecryptAllInString decrypts all ENC(...) values in a string
func (*JasyptEncryptor) DecryptMap ΒΆ
DecryptMap decrypts all ENC(...) values in a map
func (*JasyptEncryptor) DecryptPrefixed ΒΆ
func (e *JasyptEncryptor) DecryptPrefixed(value string) (string, error)
DecryptPrefixed decrypts a value with ENC(...) prefix
func (*JasyptEncryptor) Encrypt ΒΆ
func (e *JasyptEncryptor) Encrypt(plaintext string) (string, error)
Encrypt encrypts plaintext using PBEWithMD5AndDES (Jasypt compatible)
func (*JasyptEncryptor) EncryptWithPrefix ΒΆ
func (e *JasyptEncryptor) EncryptWithPrefix(plaintext string) (string, error)
EncryptWithPrefix encrypts and wraps with ENC(...) prefix
type JasyptOption ΒΆ
type JasyptOption func(*JasyptEncryptor)
JasyptOption is a functional option for configuring JasyptEncryptor
func WithJasyptIterations ΒΆ
func WithJasyptIterations(iterations int) JasyptOption
WithJasyptIterations sets the iteration count for key derivation. Default is 1000 (same as Jasypt default).
type JasyptStrongEncryptor ΒΆ
type JasyptStrongEncryptor struct {
// contains filtered or unexported fields
}
JasyptStrongEncryptor provides compatibility with Jasypt's stronger algorithm. It uses PBEWithHmacSHA256AndAES_256 which is more secure than the default.
Example:
enc, _ := gocrypt.NewJasyptStrongEncryptor("myPassword")
enc, _ := gocrypt.NewJasyptStrongEncryptor("myPassword",
gocrypt.WithStrongIterations(5000),
)
func NewJasyptStrongEncryptor ΒΆ
func NewJasyptStrongEncryptor(password string, opts ...JasyptStrongOption) (*JasyptStrongEncryptor, error)
NewJasyptStrongEncryptor creates a new encryptor using PBEWithHmacSHA256AndAES_256
func (*JasyptStrongEncryptor) Decrypt ΒΆ
func (e *JasyptStrongEncryptor) Decrypt(encoded string) (string, error)
Decrypt decrypts data encrypted with PBEWithHmacSHA256AndAES_256
func (*JasyptStrongEncryptor) DecryptAllInString ΒΆ
func (e *JasyptStrongEncryptor) DecryptAllInString(input string) (string, error)
DecryptAllInString decrypts all ENC(...) values in a string
func (*JasyptStrongEncryptor) DecryptMap ΒΆ
DecryptMap decrypts all ENC(...) values in a map
func (*JasyptStrongEncryptor) DecryptPrefixed ΒΆ
func (e *JasyptStrongEncryptor) DecryptPrefixed(value string) (string, error)
DecryptPrefixed decrypts a value with ENC(...) prefix
func (*JasyptStrongEncryptor) Encrypt ΒΆ
func (e *JasyptStrongEncryptor) Encrypt(plaintext string) (string, error)
Encrypt encrypts using AES-256-CBC with PBKDF2-HMAC-SHA256
func (*JasyptStrongEncryptor) EncryptWithPrefix ΒΆ
func (e *JasyptStrongEncryptor) EncryptWithPrefix(plaintext string) (string, error)
EncryptWithPrefix encrypts and wraps with ENC(...) prefix
type JasyptStrongOption ΒΆ
type JasyptStrongOption func(*JasyptStrongEncryptor)
JasyptStrongOption is a functional option for JasyptStrongEncryptor
func WithStrongIterations ΒΆ
func WithStrongIterations(iterations int) JasyptStrongOption
WithStrongIterations sets the iteration count
func WithStrongSaltSize ΒΆ
func WithStrongSaltSize(size int) JasyptStrongOption
WithStrongSaltSize sets the salt size
type Option ΒΆ
type Option func(*Encryptor)
Option is a functional option for configuring the Encryptor
func WithIterations ΒΆ
WithIterations sets the PBKDF2 iteration count. Higher values increase security but also increase computation time. Default is 10000.
func WithKeySize ΒΆ
WithKeySize sets the key size in bytes. Use 16 for AES-128 or 32 for AES-256 (default).
func WithSaltSize ΒΆ
WithSaltSize sets the salt size in bytes. Default is 16 bytes.
Directories
ΒΆ
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gocrypt-cli
command
Command gocrypt-cli provides a command-line tool for encrypting and decrypting values.
|
Command gocrypt-cli provides a command-line tool for encrypting and decrypting values. |
|
examples
|
|
|
basic
command
Package main demonstrates basic GoCrypt usage.
|
Package main demonstrates basic GoCrypt usage. |
|
gin-integration
command
Package main demonstrates GoCrypt integration with Go-Gin framework.
|
Package main demonstrates GoCrypt integration with Go-Gin framework. |