cryptox

package
v1.11.7 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 10 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const DefaultMask = "[已脱敏]"

DefaultMask 是敏感值对外展示时使用的统一占位文本。 它只表达“该值已被隐藏”,不会泄露原始值的长度或内容。

View Source
const EncryptedPrefix = "ENC:"

EncryptedPrefix 是带版本标记的标准密文全局前缀。

Variables

View Source
var ErrInvalidCiphertext = errors.New("invalid ciphertext")

ErrInvalidCiphertext 表示输入既不是当前格式密文,也不是已注册的历史密文。

Functions

func MaskValue added in v1.11.3

func MaskValue(string) string

MaskValue 用于不需要解密、只需要生成展示结果的投影边界。

Types

type CiphertextDecryptor added in v1.11.3

type CiphertextDecryptor interface {
	DecryptCiphertext(encryptedText string) (string, error)
}

CiphertextDecryptor 只接受可确认的密文并返回解密后的明文。 该接口会兼容没有 ENC:版本: 前缀的历史密文,但遇到非法明文或损坏密文时必须严格阻断。

type Crypto added in v1.9.1

type Crypto interface {
	// Encrypt 加密明文字符串并返回密文。
	Encrypt(plainText string) (string, error)

	// Decrypt 解密密文字符串并返回明文。
	Decrypt(encryptedText string) (string, error)
}

Crypto 定义通用加解密接口。

func MustNewAESCrypto added in v1.11.0

func MustNewAESCrypto(key string) Crypto

MustNewAESCrypto 包装 NewAESCrypto,初始化失败时直接 panic。

func MustNewAESCryptoV2 added in v1.11.0

func MustNewAESCryptoV2(key string) Crypto

MustNewAESCryptoV2 包装 NewAESCryptoV2,初始化失败时直接 panic。

type CryptoAES added in v1.9.1

type CryptoAES struct {
	// contains filtered or unexported fields
}

CryptoAES 实现与历史密文兼容的 AES-GCM V1 算法(带 JSON 序列化与截断/填充密钥)。

func NewAESCrypto added in v1.9.1

func NewAESCrypto(key string) (*CryptoAES, error)

NewAESCrypto 创建历史兼容的 V1 AES 加密器。

func (*CryptoAES) Decrypt added in v1.9.1

func (a *CryptoAES) Decrypt(encryptedText string) (string, error)

func (*CryptoAES) Encrypt added in v1.9.1

func (a *CryptoAES) Encrypt(plainText string) (string, error)

type CryptoAESV2 added in v1.11.0

type CryptoAESV2 struct {
	// contains filtered or unexported fields
}

CryptoAESV2 使用 SHA-256 派生的 32 字节高强度密钥加密原始纯文本。

func NewAESCryptoV2 added in v1.11.0

func NewAESCryptoV2(key string) (*CryptoAESV2, error)

NewAESCryptoV2 创建增强型 V2 AES-GCM 加密器。

func (*CryptoAESV2) Decrypt added in v1.11.0

func (a *CryptoAESV2) Decrypt(encryptedText string) (string, error)

func (*CryptoAESV2) Encrypt added in v1.11.0

func (a *CryptoAESV2) Encrypt(plainText string) (string, error)

type CryptoManager added in v1.9.1

type CryptoManager struct {
	// contains filtered or unexported fields
}

CryptoManager 统一管理多种加密算法,支持多版本并兼容没有版本前缀的历史密文。

func NewCryptoManager added in v1.9.1

func NewCryptoManager(defaultVer string) *CryptoManager

NewCryptoManager 创建新的 CryptoManager

func (*CryptoManager) Decrypt added in v1.9.1

func (m *CryptoManager) Decrypt(encryptedText string) (string, error)

Decrypt 解密数据(自动识别版本,兼容历史数据和普通字符串明文)

func (*CryptoManager) DecryptCiphertext added in v1.11.3

func (m *CryptoManager) DecryptCiphertext(encryptedText string) (string, error)

DecryptCiphertext 根据密文格式选择对应算法进行严格解密。 带 ENC:版本: 前缀的数据按指定版本解密;无前缀数据按历史算法列表依次尝试。 无法由任何算法解密时严格返回错误,调用方不得将其当作合法密文。

func (*CryptoManager) Encrypt added in v1.9.1

func (m *CryptoManager) Encrypt(plainText string) (string, error)

Encrypt 加密数据,自动加上 ENC:<defaultVer>: 前缀

func (*CryptoManager) Register added in v1.11.0

func (m *CryptoManager) Register(version string, algo Crypto) *CryptoManager

Register 注册符合 Crypto 接口的加密算法策略,绑定到一个版本号

func (*CryptoManager) WithLegacyAlgo added in v1.9.1

func (m *CryptoManager) WithLegacyAlgo(version string) *CryptoManager

WithLegacyAlgo 设置单个历史兼容算法版本(老数据没加 ENC: 前缀时使用)

func (*CryptoManager) WithLegacyAlgos added in v1.11.3

func (m *CryptoManager) WithLegacyAlgos(versions ...string) *CryptoManager

WithLegacyAlgos 注册没有版本前缀的历史算法列表,按传入顺序依次尝试解密

func (*CryptoManager) WithMigrationHandler added in v1.9.1

func (m *CryptoManager) WithMigrationHandler(handler MigrationHandler) *CryptoManager

WithMigrationHandler 设置迁移回调

type Masker added in v1.11.3

type Masker interface {
	// Mask 返回敏感值的掩码占位形式。
	Mask(value string) string
}

Masker 提供敏感值的安全展示形式。

type MigrationHandler added in v1.9.1

type MigrationHandler func(oldEncrypted, newEncrypted string)

type ValueProtector added in v1.11.3

type ValueProtector interface {
	Crypto
	CiphertextDecryptor
	Masker
}

ValueProtector 统一封装敏感值在存储、运行时和展示边界上的全生命周期处理能力。

func NewValueProtector added in v1.11.3

func NewValueProtector(cipher Crypto) ValueProtector

NewValueProtector 为已有加密组件增加统一的展示掩码与严格解密能力。

Jump to

Keyboard shortcuts

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