scrypt

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package scrypt provides helpful abstractions for an implementation of RFC7914 and implements github.com/go-crypt/crypt interfaces.

This implementation is loaded by crypt.NewDefaultDecoder and crypt.NewDecoderAll.

Index

Constants

View Source
const (
	// EncodingFormat is the format of the encoded digest.
	EncodingFormat = "$%s$ln=%d,r=%d,p=%d$%s$%s"

	// AlgName is the name for this algorithm.
	AlgName = "scrypt"

	// KeyLengthMin is the minimum key length accepted.
	KeyLengthMin = 1

	// SaltLengthMin is the minimum salt length accepted.
	SaltLengthMin = 8

	// SaltLengthMax is the maximum salt length accepted.
	SaltLengthMax = 1024

	// IterationsMin is the minimum number of iterations accepted.
	IterationsMin = 1

	// IterationsMax is the maximum number of iterations accepted.
	IterationsMax = 58

	// IterationsDefault is the default number of iterations.
	IterationsDefault = 16

	// BlockSizeMin is the minimum block size accepted.
	BlockSizeMin = 1

	// BlockSizeMax is the maximum block size accepted.
	BlockSizeMax = math.MaxInt / 256

	// BlockSizeDefault is the default block size.
	BlockSizeDefault = 8

	// ParallelismMin is the minimum parallelism factor accepted.
	ParallelismMin = 1

	// ParallelismMax is the maximum parallelism factor accepted.
	//
	// Equation is based on the following text from RFC:
	//
	//   The parallelization parameter p
	//   ("parallelizationParameter") is a positive integer less than or equal
	//   to ((2^32-1) * 32) / (128 * r).
	//
	//   When r has a minimum of 1, this makes the equation ((2^32-1) * 32) / 128.
	ParallelismMax = 1073741823

	// ParallelismDefault is the default parallelism factor.
	ParallelismDefault = ParallelismMin
)
View Source
const (
	// KeyLengthMax is the maximum key size accepted.
	KeyLengthMax = math.MaxUint32 * 32
)

Variables

This section is empty.

Functions

func Decode

func Decode(encodedDigest string) (digest algorithm.Digest, err error)

Decode the encoded digest into a algorithm.Digest.

func RegisterDecoder

func RegisterDecoder(r algorithm.DecoderRegister) (err error)

RegisterDecoder the decoder with the algorithm.DecoderRegister.

Types

type Digest

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

Digest is a scrypt.Digest which handles scrypt hashes.

func (*Digest) Encode

func (d *Digest) Encode() string

Encode returns the encoded form of this scrypt.Digest.

func (*Digest) Match

func (d *Digest) Match(password string) (match bool)

Match returns true if the string password matches the current scrypt.Digest.

func (*Digest) MatchAdvanced

func (d *Digest) MatchAdvanced(password string) (match bool, err error)

MatchAdvanced is the same as Match except if there is an error it returns that as well.

func (*Digest) MatchBytes

func (d *Digest) MatchBytes(passwordBytes []byte) (match bool)

MatchBytes returns true if the []byte passwordBytes matches the current scrypt.Digest.

func (*Digest) MatchBytesAdvanced

func (d *Digest) MatchBytesAdvanced(passwordBytes []byte) (match bool, err error)

MatchBytesAdvanced is the same as MatchBytes except if there is an error it returns that as well.

func (*Digest) String

func (d *Digest) String() string

String returns the storable format of the scrypt.Digest encoded hash.

type Hasher

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

Hasher is a crypt.Hash for scrypt which can be initialized via New using a functional options pattern.

func New

func New(opts ...Opt) (hasher *Hasher, err error)

New returns a new scrypt.Hasher with the provided functional options applied.

func (*Hasher) Hash

func (h *Hasher) Hash(password string) (digest algorithm.Digest, err error)

Hash performs the hashing operation and returns either a Digest or an error.

func (*Hasher) HashWithSalt

func (h *Hasher) HashWithSalt(password string, salt []byte) (digest algorithm.Digest, err error)

HashWithSalt overloads the Hash method allowing the user to provide a salt. It's recommended instead to configure the salt size and let this be a random value generated using crypto/rand.

func (*Hasher) MustHash

func (h *Hasher) MustHash(password string) (digest algorithm.Digest)

MustHash overloads the Hash method and panics if the error is not nil. It's recommended if you use this option to utilize the Validate method first or handle the panic appropriately.

func (*Hasher) Validate

func (h *Hasher) Validate() (err error)

Validate checks the settings/parameters for this Hash and returns an error.

func (*Hasher) WithOptions

func (h *Hasher) WithOptions(opts ...Opt) (err error)

WithOptions defines the options for this scrypt.Hasher.

type Opt

type Opt func(h *Hasher) (err error)

Opt describes the functional option pattern for the scrypt.Hasher.

func WithBlockSize

func WithBlockSize(r int) Opt

WithBlockSize is an alias for WithR.

func WithK

func WithK(k int) Opt

WithK adjusts the key length of the resulting scrypt.Digest. Minimum is 1, Maximum is 137438953440. Default is 32.

func WithKeyLength

func WithKeyLength(k int) Opt

WithKeyLength is an alias for WithK.

func WithLN

func WithLN(ln int) Opt

WithLN sets the ln parameter (logN) of the resulting scrypt.Digest. Minimum is 1, Maximum is 58. Default is 16.

func WithP

func WithP(p int) Opt

WithP sets the p parameter (parallelism factor) of the resulting scrypt.Digest. Minimum is 1, Maximum is 1073741823. Default is 1.

func WithParallelism

func WithParallelism(p int) Opt

WithParallelism is an alias for WithP.

func WithR

func WithR(r int) Opt

WithR sets the r parameter (block size) of the resulting scrypt.Digest. Minimum is 1, Maximum is math.MaxInt / 256. Default is 8.

func WithS

func WithS(s int) Opt

WithS adjusts the salt length of the resulting scrypt.Digest. Minimum is 8, Maximum is 1024. Default is 16.

func WithSaltLength

func WithSaltLength(s int) Opt

WithSaltLength is an alias for WithS.

Jump to

Keyboard shortcuts

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