security

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2025 License: MIT Imports: 21 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultCipherConfig = CipherConfig{
	Cipher: "AES-128-CBC",
	AllowedCiphers: map[string][]int{
		"AES-128-CBC": {16, 16},
		"AES-192-CBC": {16, 24},
		"AES-256-CBC": {16, 32},
	},
	KdfHash:              "sha256",
	MacHash:              "sha256",
	AuthKeyInfo:          "AuthorizationKey",
	DerivationIterations: 100000,
}

DefaultCipherConfig is the default cipher configuration

Functions

func Decrypt

func Decrypt(data, secret string, passwordBased bool, config CipherConfig) (string, error)

Decrypt decrypts data using a password or a key

func Encrypt

func Encrypt(data, secret string, passwordBased bool, config CipherConfig) (string, error)

Encrypt encrypts data using a password or a key

func GenerateRandomBytes

func GenerateRandomBytes(length int) ([]byte, error)

GenerateRandomBytes generates random bytes of specified length

func PasswordHash

func PasswordHash(password string, cost int) (string, error)

PasswordHash generates a hash of the password

func PasswordVerify

func PasswordVerify(password, hash string) bool

PasswordVerify validates the password against a given hash

Types

type AES

type AES struct {
	Text string `json:"text" default:""`
	Key  string `json:"key" default:"jcbase.aes_key__"`
	Iv   string `json:"iv" default:"jcbase.aes_iv___"`
}

func (AES) Decrypt

func (a AES) Decrypt(plaintext *string) error

Decrypt decrypts the given cipherText using the provided key and iv

func (AES) Encrypt

func (a AES) Encrypt(cipherText *string) error

Encrypt encrypts the given text using the provided key and iv

type CipherConfig

type CipherConfig struct {
	Cipher               string
	AllowedCiphers       map[string][]int
	KdfHash              string
	MacHash              string
	AuthKeyInfo          string
	DerivationIterations int
}

CipherConfig stores the cipher configuration

type Input

type Input struct {
	Value        interface{}
	DefaultValue interface{}
}

func (Input) Belong

func (s Input) Belong(allow interface{}, strict bool) interface{}

Belong 检查值是否属于允许列表

func (Input) Html

func (s Input) Html() string

Html 清理并返回HTML内容

func (Input) Sanitize

func (s Input) Sanitize() interface{}

Sanitize 自动判断类型并进行通用清理

func (Input) SanitizeString added in v0.8.0

func (s Input) SanitizeString(str string, args ...any) string

SanitizeString 清理字符串以防止 SQL 注入和 XSS 攻击

type SM4 added in v0.10.0

type SM4 struct {
	Text string `json:"text" default:""`                // 待加密/解密的文本
	Key  string `json:"key" default:"jcbase.sm4_key__"` // 加密密钥,必须为16字节
	Iv   string `json:"iv" default:"jcbase.sm4_iv___"`  // 初始化向量,必须为16字节
	Mode string `json:"mode" default:"CBC"`             // 加密模式:CBC、GCM、CFB、OFB、CTR
}

SM4 SM4加解密结构体 提供SM4对称加密算法的加解密功能

func (SM4) Decrypt added in v0.10.0

func (s SM4) Decrypt(plaintext *string) error

Decrypt 通用解密方法,根据Mode字段选择解密模式 参数:

  • plaintext: 解密后的明文输出指针

返回:

  • error: 解密失败时的错误信息

func (SM4) DecryptCBC added in v0.10.0

func (s SM4) DecryptCBC(plaintext *string) error

DecryptCBC 使用CBC模式解密数据 参数:

  • plaintext: 解密后的明文输出指针

返回:

  • error: 解密失败时的错误信息

func (SM4) DecryptGCM added in v0.10.0

func (s SM4) DecryptGCM(plaintext *string) error

DecryptGCM 使用GCM模式解密数据 参数:

  • plaintext: 解密后的明文输出指针

返回:

  • error: 解密失败时的错误信息

func (SM4) Encrypt added in v0.10.0

func (s SM4) Encrypt(cipherText *string) error

Encrypt 通用加密方法,根据Mode字段选择加密模式 参数:

  • cipherText: 加密后的密文输出指针

返回:

  • error: 加密失败时的错误信息

func (SM4) EncryptCBC added in v0.10.0

func (s SM4) EncryptCBC(cipherText *string) error

EncryptCBC 使用CBC模式加密数据 参数:

  • cipherText: 加密后的密文输出指针

返回:

  • error: 加密失败时的错误信息

func (SM4) EncryptGCM added in v0.10.0

func (s SM4) EncryptGCM(cipherText *string) error

EncryptGCM 使用GCM模式加密数据(推荐使用,提供认证加密) 参数:

  • cipherText: 加密后的密文输出指针

返回:

  • error: 加密失败时的错误信息

Jump to

Keyboard shortcuts

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