scrypt

package
v0.0.0-...-5f78c74 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const SEPARATOR = "$"

Variables

View Source
var (
	ErrInvalidParams           = errors.New("scrypt parameters are invalid")
	ErrInvalidHash             = errors.New("invalid scrypt hash format")
	ErrMismatchHashAndPassword = errors.New("password and scrypt hash do not match")
)

Functions

func Hash

func Hash(password []byte, params Params) (result, salt, derivedKey []byte, err error)

Returns the derived key of the given password using the scrypt key-derivation functions based on the given parameters. The parameters are prepended to the derived key and separated by the "$" character (0x24). If the provided parameters are invalid, an error will be returned

func Upgrade

func Upgrade(password, hash []byte, timeout time.Duration, memMiBytes int) (result, salt, derivedKey []byte, err error)

Upgrade the password-hash combination by recalibrating the scrypt parameters to the given timeout and memory constraints. Returns an error if the password and hash do not match, calibration fails or rehash fails. Returns a new hash with the updated parameters.

func Verify

func Verify(password, hash []byte) error

Compares a derived key with the potential password. The parameters from the derived key are used. The comparison is constant-time. It returns nil on success and an error if derived keys do not match.

Types

type Params

type Params struct {
	N          int // CPU/Memory cost parameter (logN)
	R          int // Block size parameter (octets)
	P          int // Parallelization parameter (positive integer)
	SaltLength int // Length of the salt (octets)
	KeyLength  int // Length of the derived key (octets)
}

Input parameters for the scrypt key-derivation function as described in Colin Percival's paper (http://www.tarsnap.com/scrypt/scrypt.pdf) he recommended parameters for interactive logins as of 2017 are N=32768, r=8 and p=1. The parameters N, r, and p should be increased as memory latency and CPU parallelism increases; consider setting N to the highest power of 2 you can derive within 100 milliseconds.

var DefaultParams Params = Params{
	N:          32768,
	R:          8,
	P:          1,
	SaltLength: 16,
	KeyLength:  32,
}

Sensible defaults for the scrypt key-derivation function These defaults will consume 32MB of memory (128 * r * N) The derived key will have a length of 32bytes (256bits)

func Decode

func Decode(hash []byte) (Params, []byte, []byte, error)

Extracts the scrypt parameters, salt and derived key from the given hash. It returns an error if the hash format is invalid and/or the parameters are invalid

func (*Params) Calibrate

func (p *Params) Calibrate(timeout time.Duration, memMiBytes int) error

Calibrates the parameters to be the hardest setting for the given time and memory constraints. The params will use the given memory (MiB) and will take the given time to compute (but not less that timeout / 2) The default timeout (when timeout == 0) is 200ms The default memory usage (when memMiBytes = 0) is 32MiB

func (*Params) Check

func (p *Params) Check() error

Check whether the input parameters are valid for the scrypt algorithm

func (*Params) Encode

func (p *Params) Encode(salt, derivedKey []byte) []byte

Encode the parameters along with the salt and key

Jump to

Keyboard shortcuts

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