crypto

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

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

This section is empty.

Variables

This section is empty.

Functions

func Decrypt

func Decrypt(cipherStr string) (plain []byte, err error)

Decrypt decodes base64 string and decrypts to plaintext. Expects format: base64(nonce | ciphertext | authtag)

func DecryptWithKey added in v1.0.0

func DecryptWithKey(key []byte, cipherStr string) ([]byte, error)

DecryptWithKey 用给定的 AES-256 密钥解 Encrypt 产生的密文,不碰进程里的密钥: 校验备份时要用备份里的密钥解它的数据库。

func DiscardGeneratedKey added in v1.0.0

func DiscardGeneratedKey() error

DiscardGeneratedKey 删除本次启动新生成的密钥文件并停用它。启动检查发现库里已有旧密文时调用: 新密钥解不开旧数据,留着它,下次启动就会被当成正常密钥使用。

func Encrypt

func Encrypt(plain []byte) (cipherStr string, err error)

Encrypt encrypts plaintext and returns base64-encoded "nonce|ciphertext|authtag" All three components are packed into the base64 string for transport.

func ExportKey added in v0.35.0

func ExportKey() ([]byte, error)

func KeyStatus added in v1.0.0

func KeyStatus() (path string, generated bool, err error)

KeyStatus 报告启动时密钥的来源,供运行时初始化做安全检查: path 是密钥文件路径(用 PT_TOOLS_SECRET_KEY 时为空),generated 表示本次启动新生成了密钥文件, err 非空表示密钥文件存在却无法使用。

func ReloadKey added in v1.0.0

func ReloadKey()

ReloadKey 按启动时的规则重新读取密钥(环境变量优先,其次 ~/.pt-tools/secret.key,都没有时生成)。

func ResetForTest

func ResetForTest()

ResetForTest re-reads the secret key from environment, intended for tests that need to switch the active key via t.Setenv across packages. Production code MUST NOT call this.

func RestoreKeyState added in v1.0.0

func RestoreKeyState(s KeyState)

RestoreKeyState 把 SnapshotKeyState 记下的状态原样放回去,不读磁盘。

Types

type AESGCMEncryptor

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

type KeyState added in v1.0.0

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

KeyState 是内存里密钥状态的原样快照:加密器、密钥文件路径、是否本进程新生成、读取错误、是否已经读过。

func SnapshotKeyState added in v1.0.0

func SnapshotKeyState() KeyState

SnapshotKeyState 原样记下内存里的密钥状态(还没读过时先读一次,和 KeyStatus 一样)。

Jump to

Keyboard shortcuts

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