password

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2025 License: MIT Imports: 7 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("密码与哈希不匹配")
)

错误定义

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 HashPassword

func HashPassword(password string) (string, error)

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

func NeedsRehash

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

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

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 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