encryption

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 6, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package encryption provides Jasypt-compatible encryption for Go applications.

Gocrypt is a pure Go implementation of the popular Java Jasypt library, providing simplified encryption for configuration values and sensitive data. It's designed to be API-compatible with Jasypt, making it easy to migrate from Java to Go or use both in the same ecosystem.

Features:

  • Complete compatibility with Jasypt encrypted values
  • Support for multiple PBE algorithms
  • ENC() wrapper for configuration values
  • Simple and intuitive API
  • Zero dependencies (except standard library)
  • Thread-safe implementation

Basic Usage:

config := NewPasswordEncryptorConfig()
config.Password = "my-secret-key"
config.Algorithm = "PBEWithHMACSHA256AndAES_256"
encryptor, err := NewConfigStringEncryptor(config)
if err != nil {
    log.Fatal(err)
}

// Encrypt a value
encrypted, _ := encryptor.Encrypt("my-password")
// Returns: "ENC(encrypted-string)"

// Decrypt a value
decrypted, _ := encryptor.Decrypt("ENC(encrypted-string)")

Algorithms:

  • PBEWithMD5AndDES
  • PBEWithSHA1AndDESede
  • PBEWithHMACSHA256AndAES_256

For more examples, see the examples directory.

Index

Constants

View Source
const (
	// DefaultAlgorithm 默认加密算法
	DefaultAlgorithm = AlgorithmPBEWithHMACSHA256AndAES256

	// DefaultPasswordIterations 默认密码迭代次数
	DefaultPasswordIterations = 1000

	// DefaultSaltSize 默认Salt大小
	DefaultSaltSize = 16

	// DefaultIVSize 0表示使用算法默认
	DefaultIVSize = 0

	// DefaultOutputType 默认输出类型
	DefaultOutputType = OutputTypeBase64

	// DefaultIVGenerator 默认IV生成器
	DefaultIVGenerator = IVGeneratorRandom

	// DefaultSaltGenerator 默认Salt生成器
	DefaultSaltGenerator = SaltGeneratorRandom

	// DefaultPrefix 默认前缀
	DefaultPrefix = "ENC("

	// DefaultSuffix 默认后缀
	DefaultSuffix = ")"
)

Default constants

Variables

View Source
var (
	// NewPasswordEncryptorConfig 创建新的密码加密器配置
	NewPasswordEncryptorConfig = newPasswordEncryptorConfig

	// NewConfigStringEncryptor 创建配置字符串加密器
	NewConfigStringEncryptor = newConfigStringEncryptor

	// NewStandardPBEStringEncryptor 创建标准PBE字符串加密器
	NewStandardPBEStringEncryptor = newStandardPBEStringEncryptor

	// IsEncrypted 检查字符串是否已被加密
	IsEncrypted = isEncrypted

	// WrapWithENC 用ENC()包装值
	WrapWithENC = wrapWithENC

	// ExtractEncryptedValue 从ENC()包装中提取加密值
	ExtractEncryptedValue = extractEncryptedValue
)

导出公共函数

Functions

This section is empty.

Types

type Algorithm

type Algorithm string

Algorithm 加密算法常量

const (
	// AlgorithmPBEWithMD5AndDES PBE with MD5 and DES算法
	AlgorithmPBEWithMD5AndDES Algorithm = "PBEWithMD5AndDES"

	// AlgorithmPBEWithSHA1AndDESede PBE with SHA1 and 3DES算法
	AlgorithmPBEWithSHA1AndDESede Algorithm = "PBEWithSHA1AndDESede"

	// AlgorithmPBEWithHMACSHA256AndAES256 PBE with HMAC SHA256 and AES-256算法
	AlgorithmPBEWithHMACSHA256AndAES256 Algorithm = "PBEWithHMACSHA256AndAES_256"
)

func (Algorithm) String

func (a Algorithm) String() string

String 返回算法字符串表示

type ConfigStringEncryptor

type ConfigStringEncryptor = configStringEncryptor

ConfigStringEncryptor 配置字符串加密器

type IVGeneratorType

type IVGeneratorType string

IVGeneratorType IV生成器类型常量

const (
	// IVGeneratorRandom 随机IV生成器
	IVGeneratorRandom IVGeneratorType = "random"
)

func (IVGeneratorType) String

func (igt IVGeneratorType) String() string

String 返回IV生成器类型字符串表示

type OutputType

type OutputType string

OutputType 输出类型常量

const (
	// OutputTypeBase64 Base64输出格式
	OutputTypeBase64 OutputType = "base64"

	// OutputTypeHex Hex输出格式
	OutputTypeHex OutputType = "hex"
)

func (OutputType) String

func (ot OutputType) String() string

String 返回输出类型字符串表示

type PasswordEncryptorConfig

type PasswordEncryptorConfig = passwordEncryptorConfig

PasswordEncryptorConfig 配置加密器

type SaltGeneratorType

type SaltGeneratorType string

SaltGeneratorType Salt生成器类型常量

const (
	// SaltGeneratorRandom 随机Salt生成器
	SaltGeneratorRandom SaltGeneratorType = "random"
)

func (SaltGeneratorType) String

func (sgt SaltGeneratorType) String() string

String 返回Salt生成器类型字符串表示

type StringEncryptor

type StringEncryptor interface {
	Encrypt(message string) (string, error)
	Decrypt(encryptedMessage string) (string, error)
}

StringEncryptor 字符串加密器接口

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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