security

package module
v0.0.0-...-66c7ba8 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 14 Imported by: 4

Documentation

Overview

Package security contains MooX's shared security primitives.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyPlaintext  = errors.New("plaintext cannot be empty")
	ErrEmptyCiphertext = errors.New("ciphertext cannot be empty")
	ErrCiphertextShort = errors.New("ciphertext too short")
	ErrEmptySecret     = errors.New("secret cannot be empty")
)

Functions

func CheckPrivateFile

func CheckPrivateFile(path string) error

CheckPrivateFile 检查保存凭据的私密文件:必须是普通文件、不是符号链接,并且只有当前用户能访问。 “只有当前用户能访问”按平台判断:Unix 上权限必须是 0600;Windows 的文件模式表示不了,改查 ACL (只允许当前用户、SYSTEM 与 Administrators,不能含 Everyone、Users、Authenticated Users 等)。

func Decrypt

func Decrypt(ciphertext, secret string) (string, error)

Decrypt decrypts an AES-256-GCM payload generated by Encrypt.

func Encrypt

func Encrypt(plaintext, secret string) (string, error)

Encrypt encrypts plaintext with AES-256-GCM. The key is derived from secret with SHA-256, so callers do not need to coordinate AES key lengths. Secret must already contain high-entropy key material; this function is not a password-based key derivation function.

func HMACSHA256Hex

func HMACSHA256Hex(secret string, data []byte) string

HMACSHA256Hex returns the lowercase hexadecimal HMAC-SHA256 of data.

func HashPassword

func HashPassword(password string) (string, error)

HashPassword hashes a password with bcrypt. The encoded result contains the randomly generated salt and cost, so callers do not store a second salt.

func NewSalt

func NewSalt() (string, error)

NewSalt returns a random 16-byte salt encoded as lowercase hexadecimal.

func ParseToken

func ParseToken(raw, secret string) (map[string]any, error)

ParseToken verifies an HS256 token and returns its generic claims.

func RandomHex

func RandomHex(size int) (string, error)

RandomHex returns size cryptographically random bytes as lowercase hex.

func SHA256Hex

func SHA256Hex(data []byte) string

SHA256Hex returns the lowercase hexadecimal SHA-256 digest of data.

func SignToken

func SignToken(claims map[string]any, secret, issuer string, ttl time.Duration) (string, error)

SignToken signs generic claims with HS256. Business-specific claim validation belongs to the caller, not this package.

func VerifyPassword

func VerifyPassword(password, encodedHash string) bool

VerifyPassword reports whether password matches a bcrypt encoded hash.

Types

This section is empty.

Directories

Path Synopsis
Package domainpolicy matches explicit DNS names and subdomain suffixes.
Package domainpolicy matches explicit DNS names and subdomain suffixes.

Jump to

Keyboard shortcuts

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