pbkdf2

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2025 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPwdIter    = errors.New("iterations is set to negatice number")
	ErrComparePwd = errors.New("incorrect password")
)

Global package-level configuration object. This will be set via SetPwdConfig and used throughout the package.

Functions

func ComparePassword

func ComparePassword(currentPwd, hashedPwd, salt []byte) error

ComparePassword securely verifies whether the provided plain-text password matches the previously hashed password using the original salt.

Parameters: - currentPwd: The user-supplied password input (plain-text, as []byte). - hashedPwd: The stored hashed password to compare against. - salt: The original salt used during hashing (must match the salt used to generate `hashedPwd`).

Returns: - nil if the password is correct (matches the hash). - ErrComparePwd if the password is incorrect. - Any error returned by the hashing process.

func GeneratePasswordWithAutoSalt

func GeneratePasswordWithAutoSalt(password []byte) ([]byte, []byte, error)

GeneratePasswordWithAutoSalt hashes a password using PBKDF2 with a randomly generated salt. It returns the derived key (hashed password), the generated salt, and an error if any occurs.

The function uses configurations from a global `config` object which should include: - Iter: Number of PBKDF2 iterations - SaltLen: Desired salt length - Hash: Hash function to use (defaults to SHA-256 if not set) - KeyLen: Desired length of the derived key

func GeneratePasswordWithSalt

func GeneratePasswordWithSalt(password, salt []byte) ([]byte, error)

GeneratePasswordWithSalt securely hashes a password using PBKDF2 with a user-provided salt. It uses the global `config` for parameters like iteration count, key length, and hash function.

Parameters: - password: The plain-text password in byte slice form. - salt: A cryptographically secure salt (must be consistent for password verification).

Returns: - The derived key (hashed password) as a byte slice. - An error if the hashing process fails or configuration is invalid.

func SetPwdConfig

func SetPwdConfig(h string, itr, keyLen, sLen int)

SetPwdConfig initializes or updates the global password hashing configuration.

Parameters: - h: Hash function name as a string ("SHA256", "SHA512"). - itr: Number of iterations for PBKDF2. - keyLen: Desired length of the derived key in bytes. - sLen: Salt length in bytes.

It sets default hash function to SHA-256 if an unknown hash type is provided.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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