password

package
v0.1.27 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidHash 表示哈希格式无效
	ErrInvalidHash = errors.New("提供的密码哈希格式无效")
	// ErrIncompatibleVersion 表示哈希使用了不支持的版本
	ErrIncompatibleVersion = errors.New("不兼容的哈希版本")
	// ErrMismatchedHashAndPassword 表示密码与存储的哈希不匹配
	ErrMismatchedHashAndPassword = errors.New("密码与哈希不匹配")
	// ErrPasswordTooWeak 表示密码强度不满足要求
	ErrPasswordTooWeak = errors.New("密码强度不满足要求")
	// ErrPasswordInHistory 表示密码在历史记录中已存在
	ErrPasswordInHistory = errors.New("密码不能与最近使用的密码相同")
	// ErrPasswordContainsPersonalInfo 表示密码包含个人信息
	ErrPasswordContainsPersonalInfo = errors.New("密码不能包含个人信息")
	// ErrPasswordInDictionary 表示密码在常见密码字典中
	ErrPasswordInDictionary = errors.New("密码过于常见,请选择一个更独特的密码")
)

错误定义

Functions

func CheckPassword

func CheckPassword(password, hash string) error

CheckPassword 是对CompareHashAndPassword的简单封装

func CompareHashAndPassword

func CompareHashAndPassword(encodedHash string, password []byte) error

CompareHashAndPassword 比较密码与其哈希值 成功时返回nil,失败时返回错误

func GenerateFromPassword

func GenerateFromPassword(password []byte, params *Params) (string, error)

GenerateFromPassword 使用Argon2id算法从密码生成加密哈希 返回的哈希格式为:$argon2id$v=19$m=65536,t=3,p=4$<salt>$<hash> 注意:该函数将创建一个新的随机盐用于每次哈希生成

func GetPasswordStrengthDescription

func GetPasswordStrengthDescription(strength PasswordStrength) string

GetPasswordStrengthDescription 获取密码强度的描述

func HashIdentifier added in v0.1.24

func HashIdentifier(password string) string

HashIdentifier 创建密码标识符,用于快速比较密码是否相同 注意:此标识符不用于验证,仅用于判断密码是否已使用过

func HashPassword

func HashPassword(password string) (string, error)

HashPassword 是对GenerateFromPassword的简单封装,使用默认参数

func IsPasswordReused added in v0.1.24

func IsPasswordReused(oldPassword, newPassword string, threshold float64) bool

IsPasswordReused 检查新密码是否重复使用了旧密码中的一大部分

func NeedsRehash

func NeedsRehash(encodedHash string, params *Params) (bool, error)

NeedsRehash 检查给定的哈希是否使用过期的参数,需要重新哈希

func ValidatePasswordPolicy added in v0.1.24

func ValidatePasswordPolicy(password string, policy *PasswordPolicy, personalInfo []string, passwordHistory []string) error

ValidatePasswordPolicy 验证密码是否符合策略

Types

type Params

type Params struct {
	// Memory 内存使用量(KB)
	Memory uint32
	// Iterations 迭代次数
	Iterations uint32
	// Parallelism 并行度
	Parallelism uint8
	// SaltLength 盐长度(字节)
	SaltLength uint32
	// KeyLength 输出密钥长度(字节)
	KeyLength uint32
}

Params 结构定义了用于Argon2id哈希算法的参数

func DefaultParams

func DefaultParams() *Params

DefaultParams 返回默认的Argon2id参数 这些默认参数基于OWASP和Argon2id的建议

type PasswordHistory added in v0.1.24

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

PasswordHistory 管理密码历史记录

func NewPasswordHistory added in v0.1.24

func NewPasswordHistory(maxCount int) *PasswordHistory

NewPasswordHistory 创建新的密码历史记录管理器

func (*PasswordHistory) Add added in v0.1.24

func (ph *PasswordHistory) Add(hash string)

Add 添加新的密码哈希到历史记录

func (*PasswordHistory) Contains added in v0.1.24

func (ph *PasswordHistory) Contains(password string) bool

Contains 检查密码是否在历史记录中

func (*PasswordHistory) GetHashes added in v0.1.24

func (ph *PasswordHistory) GetHashes() []string

GetHashes 获取所有哈希值

type PasswordPolicy added in v0.1.24

type PasswordPolicy struct {
	// MinLength 最小长度
	MinLength int
	// RequireUppercase 是否要求大写字母
	RequireUppercase bool
	// RequireLowercase 是否要求小写字母
	RequireLowercase bool
	// RequireDigits 是否要求数字
	RequireDigits bool
	// RequireSpecialChars 是否要求特殊字符
	RequireSpecialChars bool
	// MinimumStrength 最低强度要求
	MinimumStrength PasswordStrength
	// DisallowCommonPasswords 是否禁止常见密码
	DisallowCommonPasswords bool
	// CheckKeyboardPatterns 是否检查键盘规律
	CheckKeyboardPatterns bool
	// MaxHistoryCount 最大历史记录数量
	MaxHistoryCount int
}

PasswordPolicy 定义密码策略

func DefaultPasswordPolicy added in v0.1.24

func DefaultPasswordPolicy() *PasswordPolicy

DefaultPasswordPolicy 返回默认密码策略

type PasswordStrength

type PasswordStrength int

PasswordStrength 表示密码强度级别

const (
	// VeryWeak 表示非常弱的密码
	VeryWeak PasswordStrength = iota
	// Weak 表示弱密码
	Weak
	// Medium 表示中等强度密码
	Medium
	// Strong 表示强密码
	Strong
	// VeryStrong 表示非常强的密码
	VeryStrong
)

func CheckPasswordStrength

func CheckPasswordStrength(password string) PasswordStrength

CheckPasswordStrength 检查密码强度

Jump to

Keyboard shortcuts

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