security

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultIteration = 10_000
	MinIteration     = 1_000
	MaxIteration     = 1_000_000

	DefaultKeyLength = 32
	MinKeyLength     = 8
	MaxKeyLength     = 1_024
)

Variables

This section is empty.

Functions

func GeneratePBKDF2

func GeneratePBKDF2(password, salt string, iteration, keyLength int, hashAlgo string) string

GeneratePBKDF2 generates a PBKDF2 key based on the given password, salt, iteration and key length.

The iteration and key length are parsed and validated before being used. The hash algorithm is case-insensitive and defaults to "sha512" if not supported. Supported hash algorithms are "md5", "sha1", "sha256", and "sha512".

The output string is in the format of "hashAlgo$salt$iteration$keyLength$hashedPassword".

func GetHashFunc

func GetHashFunc(hashAlgo string) (string, func() hash.Hash)

GetHashFunc returns the given hash algorithm and a function to create a new instance of the hash algorithm.

The given hash algorithm is case-insensitive. If the given hash algorithm is not supported, it defaults to "sha512".

Supported hash algorithms are: - md5 - sha1 - sha256 - sha512

func ParseIteration

func ParseIteration(i int) int

ParseIteration returns the given iteration if it is within the valid range. Otherwise, it returns the default iteration.

func ParseKeyLength

func ParseKeyLength(k int) int

ParseKeyLength returns the given key length if it is within the valid range. Otherwise, it returns the default key length.

func ValidatePBKDF2

func ValidatePBKDF2(password, hash string) bool

ValidatePBKDF2 validates the given hash string against the given password.

The given hash string is expected to be in the format of "pbkdf2_<hashAlgo>$<salt>$<iteration>$<keyLength>$<hashedPassword>". The given password is hashed using the given hash string's algorithm, salt, iteration, and key length. If the generated hash string matches the given hash string, the function returns true. Otherwise, it returns false.

Types

This section is empty.

Jump to

Keyboard shortcuts

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