password

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMismatchedHashAndPassword = errors.New("密码校验失败")     // ErrMismatchedHashAndPassword 密码不匹配错误
	ErrUnsupportedAlgorithm      = errors.New("不支持的hash算法") // ErrUnsupportedAlgorithm 不支持的算法错误
)

Functions

func Compare

func Compare(password, hashedPassword string) error

Compare 使用默认配置比较密码

func Hash

func Hash(password string) (string, error)

Hash 使用默认配置哈希密码

Types

type Argon2Config

type Argon2Config struct {
	Memory      uint32 `json:"memory" yaml:"memory" ini:"memory"`                // 内存大小 (KB)
	Iterations  uint32 `json:"iterations" yaml:"iterations" ini:"iterations"`    // 迭代次数
	Parallelism uint8  `json:"parallelism" yaml:"parallelism" ini:"parallelism"` // 并行度
	SaltLength  uint32 `json:"salt_length" yaml:"salt_length" ini:"salt_length"` // 盐值长度
	KeyLength   uint32 `json:"key_length" yaml:"key_length" ini:"key_length"`    // 密钥长度
}

Argon2Config argon2 算法配置

type Argon2Hasher

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

Argon2Hasher argon2 哈希器

func (*Argon2Hasher) Compare

func (h *Argon2Hasher) Compare(password, hashedPassword string) error

func (*Argon2Hasher) Hash

func (h *Argon2Hasher) Hash(password string) (string, error)

type BcryptConfig

type BcryptConfig struct {
	Cost int `json:"cost" yaml:"cost" ini:"cost"` // 计算成本,范围 4-31,默认 10
}

BcryptConfig bcrypt 算法配置

type BcryptHasher

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

BcryptHasher bcrypt 哈希器

func (*BcryptHasher) Compare

func (h *BcryptHasher) Compare(password, hashedPassword string) error

func (*BcryptHasher) Hash

func (h *BcryptHasher) Hash(password string) (string, error)

type HashAlgorithm

type HashAlgorithm string

HashAlgorithm 密码哈希算法

const (
	HashBcrypt   HashAlgorithm = "bcrypt"   // bcrypt 算法,安全性高,抗GPU破解,默认算法
	HashArgon2   HashAlgorithm = "argon2"   // Argon2 算法,密码哈希竞赛冠军,抗侧信道攻击
	HashArgon2id HashAlgorithm = "argon2id" // Argon2id 算法,混合模式,平衡抗GPU和侧信道攻击
	HashScrypt   HashAlgorithm = "scrypt"   // scrypt 算法,内存困难型,抗ASIC和GPU攻击
	HashPBKDF2   HashAlgorithm = "pbkdf2"   // PBKDF2 算法,经典可靠,配置灵活
)

func (HashAlgorithm) String

func (h HashAlgorithm) String() string

type Hasher

type Hasher interface {
	Hash(password string) (string, error)          // Hash 对密码进行哈希
	Compare(password, hashedPassword string) error // Compare 比较密码和哈希值
}

Hasher 密码哈希器接口

type PBKDF2Config

type PBKDF2Config struct {
	Iterations int    `json:"iterations" yaml:"iterations" ini:"iterations"`    // 迭代次数
	KeyLength  int    `json:"key_length" yaml:"key_length" ini:"key_length"`    // 密钥长度
	HashFunc   string `json:"hash_func" yaml:"hash_func" ini:"hash_func"`       // 哈希函数 (sha1, sha256, sha512)
	SaltLength int    `json:"salt_length" yaml:"salt_length" ini:"salt_length"` // 盐值长度
}

PBKDF2Config PBKDF2 算法配置

type PBKDF2Hasher

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

PBKDF2Hasher PBKDF2 哈希器

func (*PBKDF2Hasher) Compare

func (h *PBKDF2Hasher) Compare(password, hashedPassword string) error

func (*PBKDF2Hasher) Hash

func (h *PBKDF2Hasher) Hash(password string) (string, error)

type Password

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

func NewPassword

func NewPassword(cogs ...*Security) *Password

func (*Password) Compare

func (pm *Password) Compare(password, hashedPassword string) error

Compare 比较密码和哈希值

func (*Password) Hash

func (pm *Password) Hash(password string) (string, error)

Hash 使用配置的算法对密码进行哈希

func (*Password) SetAutoDetect

func (pm *Password) SetAutoDetect(autoDetect bool)

SetAutoDetect 设置是否自动检测算法

type ScryptConfig

type ScryptConfig struct {
	N       int `json:"n" yaml:"n" ini:"n"`                      // CPU/内存成本参数
	R       int `json:"r" yaml:"r" ini:"r"`                      // 块大小参数
	P       int `json:"p" yaml:"p" ini:"p"`                      // 并行度参数
	KeyLen  int `json:"key_len" yaml:"key_len" ini:"key_len"`    // 密钥长度
	SaltLen int `json:"salt_len" yaml:"salt_len" ini:"salt_len"` // 盐值长度
}

ScryptConfig scrypt 算法配置

type ScryptHasher

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

ScryptHasher scrypt 哈希器

func (*ScryptHasher) Compare

func (h *ScryptHasher) Compare(password, hashedPassword string) error

func (*ScryptHasher) Hash

func (h *ScryptHasher) Hash(password string) (string, error)

type Security

type Security struct {
	PasswordAlgorithm HashAlgorithm `json:"password_algorithm" yaml:"password_algorithm" ini:"password_algorithm"` // 密码hash算法
	Bcrypt            BcryptConfig  `json:"bcrypt" yaml:"bcrypt" ini:"bcrypt"`                                     // bcrypt 配置
	Argon2            Argon2Config  `json:"argon2" yaml:"argon2" ini:"argon2"`                                     // argon2 配置
	Argon2id          Argon2Config  `json:"argon2id" yaml:"argon2id" ini:"argon2id"`                               // argon2id 配置
	Scrypt            ScryptConfig  `json:"scrypt" yaml:"scrypt" ini:"scrypt"`                                     // scrypt 配置
	PBKDF2            PBKDF2Config  `json:"pbkdf2" yaml:"pbkdf2" ini:"pbkdf2"`                                     // pbkdf2 配置
}

Security 安全配置

Jump to

Keyboard shortcuts

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