Documentation
¶
Index ¶
- Variables
- func CheckPassword(hashedPassword, password string) bool
- func CheckPasswordAndUpgrade(hashedPassword, password string, targetCost int) (match bool, needUpgrade bool, newHash string, err error)
- func GetPasswordCost(hashedPassword string) (int, error)
- func HashPassword(password string) (string, error)
- func HashPasswordWithCost(password string, cost int) (string, error)
- func InitValidator()
- func ValidatePassword(password string) (bool, string)
- func ValidatePasswordWithConfig(password string, config PasswordConfig) (bool, string)
- type PasswordConfig
- type ValidationError
- type ValidationErrors
- func BindAndValidate(c *gin.Context, req any) ValidationErrors
- func BindForm(c *gin.Context, req any) ValidationErrors
- func BindJSON(c *gin.Context, req any) ValidationErrors
- func BindQuery(c *gin.Context, req any) ValidationErrors
- func ShouldBindAndValidate(c *gin.Context, req any) (ValidationErrors, bool)
- func ValidateStruct(s any) ValidationErrors
Constants ¶
This section is empty.
Variables ¶
var DefaultPasswordConfig = PasswordConfig{ MinLength: 8, MaxLength: 72, RequireUpper: true, RequireLower: true, RequireDigit: true, RequireSpecial: false, }
DefaultPasswordConfig 默认密码配置。
P1 #17:MaxLength 由 128 收敛到 72(字节)。bcrypt 在 72 字节处静默截断—— 若允许更长密码,两个前 72 字节相同的不同密码会被视为同一密码通过认证。 length 用 len()(字节)度量,与 bcrypt 的字节截断口径一致。需支持超长密码时, 应在哈希前先做 SHA-256 预哈希再交给 bcrypt,而非放宽此上限。
var Validator atomic.Pointer[validator.Validate]
Validator 全局验证器实例。
H-13 修复:改用 atomic.Pointer 保护读写,消除原裸指针(InitValidator 无锁写、 ValidateStruct 无锁读)在运行期热重载/并发校验时的数据竞争。类型由 *validator.Validate 变更为 atomic.Pointer[validator.Validate](breaking:下游若直接读 validation.Validator.Struct 需改用 ValidateStruct,或 validation.Validator.Load().Struct)。
Functions ¶
func CheckPassword ¶
CheckPassword 校验 hashedPassword 是否匹配明文密码。
func CheckPasswordAndUpgrade ¶
func CheckPasswordAndUpgrade(hashedPassword, password string, targetCost int) (match bool, needUpgrade bool, newHash string, err error)
CheckPasswordAndUpgrade 校验密码,并在存量 hash 的 cost 低于归一化后的目标 cost 时重新哈希。
func GetPasswordCost ¶
GetPasswordCost 返回 hashedPassword 中编码的 bcrypt cost。
func HashPassword ¶
HashPassword 使用框架默认 cost 哈希明文密码。
func HashPasswordWithCost ¶
HashPasswordWithCost 使用指定 cost 哈希明文密码。
低于 bcrypt.MinCost 的 cost 会提升到 bcrypt.MinCost,高于 bcrypt.MaxCost 的 cost 会降到 bcrypt.MaxCost。
func ValidatePassword ¶
ValidatePassword 验证密码强度 返回:是否有效,错误信息
func ValidatePasswordWithConfig ¶
func ValidatePasswordWithConfig(password string, config PasswordConfig) (bool, string)
ValidatePasswordWithConfig 使用指定配置验证密码强度
Types ¶
type PasswordConfig ¶
type PasswordConfig struct {
MinLength int // 最小长度
MaxLength int // 最大长度
RequireUpper bool // 需要大写字母
RequireLower bool // 需要小写字母
RequireDigit bool // 需要数字
RequireSpecial bool // 需要特殊字符
}
PasswordConfig 密码验证配置
type ValidationError ¶
type ValidationError struct {
Field string `json:"field"` // 字段名(使用 label 或 json tag)
Label string `json:"label"` // 字段中文名(用于显示)
Message string `json:"message"` // 错误消息
}
ValidationError 验证错误
type ValidationErrors ¶
type ValidationErrors []ValidationError
ValidationErrors 验证错误列表
func BindAndValidate ¶
func BindAndValidate(c *gin.Context, req any) ValidationErrors
BindAndValidate 绑定并验证请求
func ShouldBindAndValidate ¶
func ShouldBindAndValidate(c *gin.Context, req any) (ValidationErrors, bool)
ShouldBindAndValidate 绑定并验证请求,返回是否成功
func (ValidationErrors) FirstMessage ¶
func (ve ValidationErrors) FirstMessage() string
FirstMessage 获取第一个错误消息
func (ValidationErrors) ToLabelMap ¶
func (ve ValidationErrors) ToLabelMap() map[string]string
ToLabelMap 转换为带标签的 map