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 ¶
View Source
var DefaultPasswordConfig = PasswordConfig{ MinLength: 8, MaxLength: 128, RequireUpper: true, RequireLower: true, RequireDigit: true, RequireSpecial: false, }
DefaultPasswordConfig 默认密码配置
View Source
var Validator *validator.Validate
Validator 全局验证器实例
Functions ¶
func CheckPassword ¶
CheckPassword 验证密码是否匹配 hashedPassword 是加密后的密码,password 是明文密码
func CheckPasswordAndUpgrade ¶
func CheckPasswordAndUpgrade(hashedPassword, password string, targetCost int) (match bool, needUpgrade bool, newHash string, err error)
CheckPasswordAndUpgrade 验证密码并在需要时升级加密成本 返回:是否匹配、是否需要升级、升级后的密码、错误
func GetPasswordCost ¶
GetPasswordCost 获取加密密码的成本
func HashPasswordWithCost ¶
HashPasswordWithCost 使用指定成本对密码进行加密 cost 范围: 4-31,值越大越安全但越慢
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
Click to show internal directories.
Click to hide internal directories.