Documentation ¶
Overview ¶
Package pbkdf2 provides helpful abstractions for an implementation of PBKDF2 and implements github.com/go-crypt/crypt interfaces.
This implementation is loaded by crypt.NewDefaultDecoder and crypt.NewDecoderAll.
Index ¶
- Constants
- func Decode(encodedDigest string) (digest algorithm.Digest, err error)
- func DecodeVariant(v Variant) func(encodedDigest string) (digest algorithm.Digest, err error)
- func RegisterDecoder(r algorithm.DecoderRegister) (err error)
- func RegisterDecoderSHA1(r algorithm.DecoderRegister) (err error)
- func RegisterDecoderSHA224(r algorithm.DecoderRegister) (err error)
- func RegisterDecoderSHA256(r algorithm.DecoderRegister) (err error)
- func RegisterDecoderSHA384(r algorithm.DecoderRegister) (err error)
- func RegisterDecoderSHA512(r algorithm.DecoderRegister) (err error)
- type Digest
- func (d *Digest) Encode() string
- func (d *Digest) Match(password string) (match bool)
- func (d *Digest) MatchAdvanced(password string) (match bool, err error)
- func (d *Digest) MatchBytes(passwordBytes []byte) (match bool)
- func (d *Digest) MatchBytesAdvanced(passwordBytes []byte) (match bool, err error)
- func (d *Digest) String() string
- type Hasher
- func New(opts ...Opt) (hasher *Hasher, err error)
- func NewSHA1(opts ...Opt) (hasher *Hasher, err error)
- func NewSHA224(opts ...Opt) (hasher *Hasher, err error)
- func NewSHA256(opts ...Opt) (hasher *Hasher, err error)
- func NewSHA384(opts ...Opt) (hasher *Hasher, err error)
- func NewSHA512(opts ...Opt) (hasher *Hasher, err error)
- func (h *Hasher) Hash(password string) (digest algorithm.Digest, err error)
- func (h *Hasher) HashWithSalt(password string, salt []byte) (digest algorithm.Digest, err error)
- func (h *Hasher) MustHash(password string) (digest algorithm.Digest)
- func (h *Hasher) Validate() (err error)
- func (h *Hasher) WithOptions(opts ...Opt) (err error)
- type Opt
- type Variant
Constants ¶
const ( // EncodingFmt is the encoding format for this algorithm. EncodingFmt = "$%s$%d$%s$%s" // AlgName is the name for this algorithm. AlgName = "pbkdf2" // AlgIdentifier is the identifier used in encoded digests for this algorithm. AlgIdentifier = AlgName // AlgIdentifierSHA1 is the identifier used in encoded SHA1 variants of this algorithm. AlgIdentifierSHA1 = "pbkdf2-sha1" // AlgIdentifierSHA224 is the identifier used in encoded SHA224 variants of this algorithm. AlgIdentifierSHA224 = "pbkdf2-sha224" // AlgIdentifierSHA256 is the identifier used in encoded SHA256 variants of this algorithm. AlgIdentifierSHA256 = "pbkdf2-sha256" // AlgIdentifierSHA384 is the identifier used in encoded SHA384 variants of this algorithm. AlgIdentifierSHA384 = "pbkdf2-sha384" // AlgIdentifierSHA512 is the identifier used in encoded SHA512 variants of this algorithm. AlgIdentifierSHA512 = "pbkdf2-sha512" // KeyLengthMax is the maximum tag size accepted. KeyLengthMax = math.MaxInt32 // SaltLengthMin is the minimum salt size accepted. SaltLengthMin = 8 // SaltLengthMax is the maximum salt size accepted. SaltLengthMax = math.MaxInt32 // IterationsMin is the minimum iterations accepted. IterationsMin = 100000 // IterationsMax is the maximum iterations accepted. IterationsMax = math.MaxInt32 // IterationsDefaultSHA1 is the default iterations for algorithms SHA1 and SHA224. IterationsDefaultSHA1 = 720000 // IterationsDefaultSHA256 is the default iterations for algorithms SHA256 and SHA384. IterationsDefaultSHA256 = 310000 // IterationsDefaultSHA512 is the default iterations for algorithms SHA512. IterationsDefaultSHA512 = 120000 )
Variables ¶
This section is empty.
Functions ¶
func DecodeVariant ¶
DecodeVariant the encoded digest into a algorithm.Digest provided it matches the provided pbkdf2.Variant. If pbkdf2.VariantNone is used all variants can be decoded.
func RegisterDecoder ¶
func RegisterDecoder(r algorithm.DecoderRegister) (err error)
RegisterDecoder the decoder with the algorithm.DecoderRegister.
func RegisterDecoderSHA1 ¶
func RegisterDecoderSHA1(r algorithm.DecoderRegister) (err error)
RegisterDecoderSHA1 registers specifically the sha1 decoder variant with the algorithm.DecoderRegister.
func RegisterDecoderSHA224 ¶
func RegisterDecoderSHA224(r algorithm.DecoderRegister) (err error)
RegisterDecoderSHA224 registers specifically the sha224 decoder variant with the algorithm.DecoderRegister.
func RegisterDecoderSHA256 ¶
func RegisterDecoderSHA256(r algorithm.DecoderRegister) (err error)
RegisterDecoderSHA256 registers specifically the sha256 decoder variant with the algorithm.DecoderRegister.
func RegisterDecoderSHA384 ¶
func RegisterDecoderSHA384(r algorithm.DecoderRegister) (err error)
RegisterDecoderSHA384 registers specifically the sha384 decoder variant with the algorithm.DecoderRegister.
func RegisterDecoderSHA512 ¶
func RegisterDecoderSHA512(r algorithm.DecoderRegister) (err error)
RegisterDecoderSHA512 registers specifically the sha512 decoder variant with the algorithm.DecoderRegister.
Types ¶
type Digest ¶
type Digest struct {
// contains filtered or unexported fields
}
Digest is a pbkdf2.Digest which handles PBKDF2 hashes.
func (*Digest) MatchAdvanced ¶
MatchAdvanced is the same as Match except if there is an error it returns that as well.
func (*Digest) MatchBytes ¶
MatchBytes returns true if the []byte passwordBytes matches the current pbkdf2.Digest.
func (*Digest) MatchBytesAdvanced ¶
MatchBytesAdvanced is the same as MatchBytes except if there is an error it returns that as well.
type Hasher ¶
type Hasher struct {
// contains filtered or unexported fields
}
Hasher is a crypt.Hash for PBKDF2 which can be initialized via pbkdf2.New using a functional options pattern.
func NewSHA1 ¶
NewSHA1 returns a SHA1 variant *pbkdf2.Hasher with the additional opts applied if any.
func NewSHA224 ¶
NewSHA224 returns a SHA224 variant *pbkdf2.Hasher with the additional opts applied if any.
func NewSHA256 ¶
NewSHA256 returns a SHA256 variant *pbkdf2.Hasher with the additional opts applied if any.
func NewSHA384 ¶
NewSHA384 returns a SHA384 variant *pbkdf2.Hasher with the additional opts applied if any.
func NewSHA512 ¶
NewSHA512 returns a SHA512 variant *pbkdf2.Hasher with the additional opts applied if any.
func (*Hasher) Hash ¶
Hash performs the hashing operation and returns either a algorithm.Digest or an error.
func (*Hasher) HashWithSalt ¶
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 ¶
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 ¶
Validate checks the settings/parameters for this Hash and returns an error.
func (*Hasher) WithOptions ¶
WithOptions applies the provided functional options provided as a pbkdf2.Opt to the pbkdf2.Hasher.
type Opt ¶
Opt describes the functional option pattern for the pbkdf2.Hasher.
func WithIterations ¶
WithIterations sets the iterations parameter of the resulting pbkdf2.Digest. Minimum is 100000, Maximum is 2147483647. Default is 29000.
func WithKeyLength ¶
WithKeyLength adjusts the tag length (in bytes) of the resulting pbkdf2.Digest. Default is the output length of the HMAC digest. Generally it's NOT recommended to change this value at all and let the default values be applied. Longer tag lengths technically reduce security by forcing a longer hash calculation for legitimate users but not requiring this for an attacker. In addition most implementations expect the tag length to match the output length of the HMAC digest. This option MUST come after a specific pbkdf2.WithVariant.
func WithSaltLength ¶
WithSaltLength adjusts the salt size (in bytes) of the resulting pbkdf2.Digest. Minimum is 8, Maximum is 2147483647. Default is 16.
func WithVariant ¶
WithVariant configures the pbkdf2.Variant of the resulting pbkdf2.Digest. Default is pbkdf2.VariantSHA256.
func WithVariantName ¶
WithVariantName uses the variant name or identifier to configure the pbkdf2.Variant of the resulting pbkdf2.Digest. Default is pbkdf2.VariantSHA256.
type Variant ¶
type Variant int
Variant is a variant of the pbkdf2.Digest.
const ( // VariantNone is a variant of the pbkdf2.Digest which is unknown. VariantNone Variant = iota // VariantSHA1 is a variant of the pbkdf2.Digest which uses HMAC-SHA-1. VariantSHA1 // VariantSHA224 is a variant of the pbkdf2.Digest which uses HMAC-SHA-224. VariantSHA224 // VariantSHA256 is a variant of the pbkdf2.Digest which uses HMAC-SHA-256. VariantSHA256 // VariantSHA384 is a variant of the pbkdf2.Digest which uses HMAC-SHA-384. VariantSHA384 // VariantSHA512 is a variant of the pbkdf2.Digest which uses HMAC-SHA-512. VariantSHA512 )
func NewVariant ¶
NewVariant converts an identifier string to a pbkdf2.Variant.
func (Variant) DefaultIterations ¶
DefaultIterations returns the default iterations for a variant.