hasher

package
v0.3.10 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPasswordMismatch    = errors.New("password hash does not match")
	ErrInvalidHashFormat   = errors.New("invalid argon2id hash format")
	ErrIncompatibleVersion = errors.New("incompatible argon2 version")
)

hasher errors

Functions

func GenerateRandomString

func GenerateRandomString(length int) (string, error)

GenerateRandomString generates a cryptographically secure random string of the specified length.

Types

type Argon2Hasher

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

Argon2Hasher 是使用 Argon2id 算法的 Hasher 实现。

func NewArgon2Hasher

func NewArgon2Hasher(memory uint32, time uint32, threads uint8, keyLen uint32, saltLen uint32) *Argon2Hasher

NewArgon2Hasher 创建一个带有推荐参数的新实例。 这些参数可以从配置中读取以增加灵活性。

func (*Argon2Hasher) Compare

func (h *Argon2Hasher) Compare(ctx context.Context, hashedPassword []byte, password []byte) error

Compare 将明文密码与已有的哈希值进行比较。

func (*Argon2Hasher) Hash

func (h *Argon2Hasher) Hash(ctx context.Context, password []byte) ([]byte, error)

Hash 对给定的明文密码进行哈希处理。

type BcryptHasher

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

BcryptHasher 是使用Bcrypt算法的Hasher实现。

func NewBcryptHasher

func NewBcryptHasher(cost int) *BcryptHasher

NewBcryptHasher 创建一个新的BcryptHasher实例。 它接收一个cost参数,提供了灵活性,同时在函数内部强制执行一个合理的最小成本值, 防止在配置时意外设置一个不安全的值。

func (*BcryptHasher) Compare

func (h *BcryptHasher) Compare(ctx context.Context, hashedPassword []byte, password []byte) error

Compare 安全地比较哈希值和明文密码。 bcrypt.CompareHashAndPassword 内置了对定时攻击的防护。

func (*BcryptHasher) Hash

func (h *BcryptHasher) Hash(ctx context.Context, password []byte) ([]byte, error)

Hash 使用配置的成本对密码进行哈希。

type Hasher

type Hasher interface {
	// Hash 对给定的明文密码进行哈希处理。
	// 返回哈希后的字节切片或错误。
	Hash(ctx context.Context, password []byte) ([]byte, error)

	// Compare 将明文密码与已有的哈希值进行比较。
	// 如果匹配,则返回nil;否则返回错误。
	Compare(ctx context.Context, hashedPassword []byte, password []byte) error
}

Hasher 定义了密码哈希和验证的接口。 这是一种策略模式的应用,将具体的哈希算法与业务逻辑解耦。

Jump to

Keyboard shortcuts

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