gocrypt

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 16 Imported by: 0

README ΒΆ

GoCrypt πŸ”

CI Go Version License Go Report Card Release

Jasypt-like encryption library for Go - Encrypt your application configuration with the familiar ENC(...) pattern used in Spring Boot applications.

Made with ❀️ from Claude AI for Golang developers who need Jasypt


🎯 Why GoCrypt?

If you're coming from Java/Spring Boot world and miss Jasypt's simplicity for encrypting configuration values, GoCrypt is for you! It provides:

  • βœ… Familiar ENC(...) pattern - Just like Jasypt in Spring Boot
  • βœ… Multiple encryption algorithms - From legacy Jasypt compatibility to modern AES-256-GCM
  • βœ… Zero external dependencies - Only uses Go standard library
  • βœ… Easy migration - Decrypt existing Jasypt values from Java applications
  • βœ… CLI tool included - Encrypt/decrypt from command line
  • βœ… Framework integrations - Ready-to-use examples for Go-Gin, and more

πŸ“¦ Installation

go get github.com/farizfadian/gocrypt

πŸš€ Quick Start

Basic Encryption/Decryption
package main

import (
    "fmt"
    "github.com/farizfadian/gocrypt"
)

func main() {
    // Create encryptor with password
    enc, _ := gocrypt.NewEncryptor("mySecretPassword")

    // Encrypt
    encrypted, _ := enc.EncryptWithPrefix("db_password_123")
    fmt.Println(encrypted) // Output: ENC(base64encodedvalue...)

    // Decrypt
    decrypted, _ := enc.DecryptPrefixed(encrypted)
    fmt.Println(decrypted) // Output: db_password_123
}
Loading Encrypted Configuration
// .env file:
// DATABASE_HOST=localhost
// DATABASE_PASSWORD=ENC(AbCdEf123456...)

loader, _ := gocrypt.NewConfigLoader(os.Getenv("GOCRYPT_PASSWORD"))
config, _ := loader.LoadEnvFile(".env")

fmt.Println(config["DATABASE_PASSWORD"]) // Output: actual_password

πŸ” Encryption Algorithms

GoCrypt provides three encryption algorithms for different use cases:

Encryptor Algorithm Security Use Case
NewEncryptor AES-256-GCM ⭐⭐⭐⭐⭐ Recommended for new projects
NewJasyptStrongEncryptor PBEWithHmacSHA256AndAES_256 ⭐⭐⭐⭐ Jasypt strong compatibility
NewJasyptEncryptor PBEWithMD5AndDES ⭐⭐ Legacy Jasypt compatibility
Choose the Right Algorithm
// ═══════════════════════════════════════════════════════════════════════════
// RECOMMENDED: For new Go projects
// ═══════════════════════════════════════════════════════════════════════════
enc, _ := gocrypt.NewEncryptor(password)

// ═══════════════════════════════════════════════════════════════════════════
// For compatibility with Java Jasypt (default algorithm)
// ═══════════════════════════════════════════════════════════════════════════
enc, _ := gocrypt.NewJasyptEncryptor(password)

// ═══════════════════════════════════════════════════════════════════════════
// For compatibility with Java Jasypt (strong encryption)
// ═══════════════════════════════════════════════════════════════════════════
enc, _ := gocrypt.NewJasyptStrongEncryptor(password)

πŸ“– Usage Guide

1. Configuration Files
.env File
# config.env
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_PASSWORD=ENC(AbCdEf123456...)
API_KEY=ENC(XyZ789...)
loader, _ := gocrypt.NewConfigLoader(os.Getenv("GOCRYPT_PASSWORD"))
config, _ := loader.LoadEnvFile("config.env")
fmt.Println(config["DATABASE_PASSWORD"]) // decrypted value
YAML File
# config.yaml
database:
  host: localhost
  password: ENC(AbCdEf123456...)
config, _ := loader.LoadYAML("config.yaml")
JSON File
var config struct {
    Database struct {
        Password string `json:"password"`
    } `json:"database"`
}
loader.LoadJSON("config.json", &config)
2. Set to Environment Variables
loader, _ := gocrypt.NewConfigLoader(password)
loader.SetToEnv(".env")  // Load and set all values

// Now use os.Getenv()
dbPassword := os.Getenv("DATABASE_PASSWORD")
3. Decrypt Map
config := map[string]string{
    "host":     "localhost",
    "password": "ENC(encrypted_value)",
}

decrypted, _ := enc.DecryptMap(config)
fmt.Println(decrypted["password"]) // plaintext
4. Decrypt All in String
input := `
DATABASE_HOST=localhost
DATABASE_PASSWORD=ENC(encrypted_value)
API_KEY=ENC(another_encrypted_value)
`

decrypted, _ := enc.DecryptAllInString(input)
5. Check if Value is Encrypted
if gocrypt.IsEncrypted(value) {
    decrypted, _ := enc.DecryptPrefixed(value)
}

πŸ”§ Framework Integration

Go-Gin
package main

import (
    "os"
    "github.com/gin-gonic/gin"
    "github.com/farizfadian/gocrypt"
)

func main() {
    // Load encrypted config
    password := os.Getenv("GOCRYPT_PASSWORD")
    enc, _ := gocrypt.NewEncryptor(password)
    
    // Decrypt database password
    dbPassword := os.Getenv("DATABASE_PASSWORD")
    if gocrypt.IsEncrypted(dbPassword) {
        dbPassword, _ = enc.DecryptPrefixed(dbPassword)
    }
    
    // Use decrypted password for database connection
    // db, _ := gorm.Open(postgres.Open(dsn), &gorm.Config{})
    
    r := gin.Default()
    r.GET("/", func(c *gin.Context) {
        c.JSON(200, gin.H{"status": "ok"})
    })
    r.Run()
}

See examples/gin-integration for a complete CRUD example.

With Viper
import (
    "github.com/spf13/viper"
    "github.com/farizfadian/gocrypt"
)

func loadConfig() {
    viper.SetConfigFile("config.yaml")
    viper.ReadInConfig()

    enc, _ := gocrypt.NewEncryptor(os.Getenv("GOCRYPT_PASSWORD"))

    // Decrypt specific values
    password := viper.GetString("database.password")
    if gocrypt.IsEncrypted(password) {
        decrypted, _ := enc.DecryptPrefixed(password)
        viper.Set("database.password", decrypted)
    }
}

πŸ’» CLI Tool

Build
cd cmd/gocrypt-cli
go build -o gocrypt-cli

# Or install globally
go install github.com/farizfadian/gocrypt/cmd/gocrypt-cli@latest
Usage
# Encrypt a value
gocrypt-cli encrypt -p mySecret -v "database_password"
# Output: ENC(base64value...)

# Decrypt a value
gocrypt-cli decrypt -p mySecret -v "ENC(base64value...)"
# Output: database_password

# Encrypt all values in a file
gocrypt-cli encrypt-file -p mySecret -i .env.plain -o .env.encrypted

# Decrypt all values in a file
gocrypt-cli decrypt-file -p mySecret -i .env.encrypted -o .env.plain

# Use Jasypt-compatible algorithm
gocrypt-cli encrypt -p mySecret -v "secret" --jasypt

# Use environment variable for password
export GOCRYPT_PASSWORD=mySecret
gocrypt-cli encrypt -v "secret_value"

β˜• Java Jasypt Compatibility

⚠️ Important: Compatibility Matrix
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚            ENCRYPT WITH β†’ DECRYPT WITH                      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Java Jasypt (default)  β†’ NewJasyptEncryptor       βœ… YES   β”‚
β”‚ Java Jasypt (strong)   β†’ NewJasyptStrongEncryptor βœ… YES   β”‚
β”‚ Java Jasypt (default)  β†’ NewEncryptor             ❌ NO    β”‚
β”‚ NewEncryptor           β†’ Java Jasypt              ❌ NO    β”‚
β”‚ NewEncryptor           β†’ NewEncryptor             βœ… YES   β”‚
β”‚ NewJasyptEncryptor     β†’ Java Jasypt              βœ… YES   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Scenario 1: Migrate from Java (Decrypt existing ENC values)
// Your Java application.properties has:
// db.password=ENC(xxxFromJavaxxx)

// In Go, use JasyptEncryptor (NOT NewEncryptor!)
enc, _ := gocrypt.NewJasyptEncryptor(samePasswordAsJava)
decrypted, _ := enc.DecryptPrefixed("ENC(xxxFromJavaxxx)")  // βœ… Works!
Scenario 2: Go & Java Share Same Config
// Use JasyptEncryptor so both Go and Java can read
enc, _ := gocrypt.NewJasyptEncryptor(sharedPassword)
encrypted, _ := enc.EncryptWithPrefix("sharedSecret")

// This ENC(...) value can be decrypted by:
// - Go: using NewJasyptEncryptor
// - Java: using Jasypt library
Scenario 3: New Go Project (No Java)
// Use NewEncryptor for better security
enc, _ := gocrypt.NewEncryptor(password)
encrypted, _ := enc.EncryptWithPrefix("mySecret")

// ⚠️ This CANNOT be decrypted by Java Jasypt!
// Only use if you don't need Java compatibility
For Jasypt Strong Encryption

If Java uses PBEWITHHMACSHA256ANDAES_256:

enc, _ := gocrypt.NewJasyptStrongEncryptor(password,
    gocrypt.WithStrongIterations(1000), // match Java config
)
decrypted, _ := enc.DecryptPrefixed(encryptedValue)
Migration from Jasypt to GoCrypt
// 1. Decrypt using Jasypt-compatible encryptor
jasyptEnc, _ := gocrypt.NewJasyptEncryptor(oldPassword)
plaintext, _ := jasyptEnc.DecryptPrefixed(oldEncryptedValue)

// 2. Re-encrypt using GoCrypt (AES-256-GCM)
newEnc, _ := gocrypt.NewEncryptor(newPassword)
newEncrypted, _ := newEnc.EncryptWithPrefix(plaintext)

βš™οΈ Advanced Configuration

Custom Options
// GoCrypt (AES-256-GCM)
enc, _ := gocrypt.NewEncryptor(password,
    gocrypt.WithIterations(50000),  // default: 10000
    gocrypt.WithSaltSize(32),       // default: 16
    gocrypt.WithKeySize(32),        // 32 = AES-256
)

// Jasypt Compatible
enc, _ := gocrypt.NewJasyptEncryptor(password,
    gocrypt.WithJasyptIterations(2000), // default: 1000
)

// Jasypt Strong
enc, _ := gocrypt.NewJasyptStrongEncryptor(password,
    gocrypt.WithStrongIterations(5000),
    gocrypt.WithStrongSaltSize(32),
)

πŸ›‘οΈ Security

Algorithm Comparison
Algorithm Cipher Key Derivation Auth Security
GoCrypt Default AES-256-GCM PBKDF2-SHA256 AEAD βœ… Strong
Jasypt Strong AES-256-CBC PBKDF2-SHA256 HMAC βœ… Good
Jasypt Default DES-CBC PBKDF1-MD5 None ⚠️ Legacy
Best Practices
  1. Never hardcode passwords - Use environment variables
  2. Rotate passwords regularly
  3. Use strong passwords (minimum 16 characters)
  4. Store encrypted config in version control, not plaintext
  5. Use NewEncryptor for new projects - More secure than Jasypt

πŸ“š API Reference

Encryptor (AES-256-GCM)
func NewEncryptor(password string, opts ...Option) (*Encryptor, error)

func (e *Encryptor) Encrypt(plaintext string) (string, error)
func (e *Encryptor) EncryptWithPrefix(plaintext string) (string, error)
func (e *Encryptor) Decrypt(encoded string) (string, error)
func (e *Encryptor) DecryptPrefixed(value string) (string, error)
func (e *Encryptor) DecryptAllInString(input string) (string, error)
func (e *Encryptor) DecryptMap(config map[string]string) (map[string]string, error)

func IsEncrypted(value string) bool
JasyptEncryptor (PBEWithMD5AndDES)
func NewJasyptEncryptor(password string, opts ...JasyptOption) (*JasyptEncryptor, error)
// Same methods as Encryptor
JasyptStrongEncryptor (PBEWithHmacSHA256AndAES_256)
func NewJasyptStrongEncryptor(password string, opts ...JasyptStrongOption) (*JasyptStrongEncryptor, error)
// Same methods as Encryptor
ConfigLoader
func NewConfigLoader(password string, opts ...Option) (*ConfigLoader, error)
func (c *ConfigLoader) LoadEnvFile(filepath string) (map[string]string, error)
func (c *ConfigLoader) LoadYAML(filepath string) (map[string]string, error)
func (c *ConfigLoader) LoadJSON(filepath string, out interface{}) error
func (c *ConfigLoader) SetToEnv(filepath string) error

πŸ“ Project Structure

gocrypt/
β”œβ”€β”€ gocrypt.go              # Core AES-256-GCM encryption
β”œβ”€β”€ jasypt_compat.go        # Jasypt compatibility layer
β”œβ”€β”€ config_loader.go        # Config file loader
β”œβ”€β”€ gocrypt_test.go         # Unit tests
β”œβ”€β”€ jasypt_compat_test.go   # Jasypt compatibility tests
β”œβ”€β”€ go.mod
β”œβ”€β”€ LICENSE
β”œβ”€β”€ README.md
β”œβ”€β”€ CLAUDE.md               # Development context for Claude AI
β”œβ”€β”€ cmd/
β”‚   └── gocrypt-cli/
β”‚       └── main.go         # CLI tool
└── examples/
    β”œβ”€β”€ basic/
    β”‚   └── main.go         # Basic usage examples
    └── gin-integration/
        β”œβ”€β”€ main.go         # Go-Gin CRUD example
        └── .env.example

πŸ§ͺ Testing

# Run all tests
go test -v ./...

# Run with coverage
go test -cover ./...

# Run benchmarks
go test -bench=. ./...

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ™ Acknowledgments

  • Inspired by Jasypt - Java Simplified Encryption
  • Built for the Go community who migrated from Java/Spring Boot

An idea from Fariz and made with ❀️ by Claude AI for Golang developers who need Jasypt.

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 ΒΆ

View Source
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 = ")"
)
View Source
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 ΒΆ

View Source
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")
)
View Source
var (
	ErrInvalidJasyptData = errors.New("invalid jasypt encrypted data")
)

Functions ΒΆ

func IsEncrypted ΒΆ

func IsEncrypted(value string) bool

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 ΒΆ

func NewEncryptor(password string, opts ...Option) (*Encryptor, error)

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) Decrypt ΒΆ

func (e *Encryptor) Decrypt(encoded string) (string, error)

Decrypt decrypts base64-encoded ciphertext.

func (*Encryptor) DecryptAllInString ΒΆ

func (e *Encryptor) DecryptAllInString(input string) (string, error)

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 ΒΆ

func (e *Encryptor) DecryptMap(config map[string]string) (map[string]string, error)

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 ΒΆ

func (e *Encryptor) DecryptPrefixed(value string) (string, error)

DecryptPrefixed decrypts a value with ENC(...) prefix.

Example:

decrypted, err := enc.DecryptPrefixed("ENC(base64value...)")

func (*Encryptor) Encrypt ΒΆ

func (e *Encryptor) Encrypt(plaintext string) (string, error)

Encrypt encrypts the plaintext and returns base64-encoded ciphertext. Each encryption produces a different output due to random salt and nonce.

func (*Encryptor) EncryptWithPrefix ΒΆ

func (e *Encryptor) EncryptWithPrefix(plaintext string) (string, error)

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 ΒΆ

func (e *JasyptEncryptor) DecryptMap(config map[string]string) (map[string]string, error)

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 ΒΆ

func (e *JasyptStrongEncryptor) DecryptMap(config map[string]string) (map[string]string, error)

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 ΒΆ

func WithIterations(iterations int) Option

WithIterations sets the PBKDF2 iteration count. Higher values increase security but also increase computation time. Default is 10000.

func WithKeySize ΒΆ

func WithKeySize(size int) Option

WithKeySize sets the key size in bytes. Use 16 for AES-128 or 32 for AES-256 (default).

func WithSaltSize ΒΆ

func WithSaltSize(size int) Option

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL