Documentation ¶
Overview ¶
Package crypt provides helpful abstractions for github.com/go-crypt/x. These abstractions allow generating password hashes, encoding them in a common storage format, and comparing them to ensure they are valid.
It's recommended that you either use crypt.NewDefaultDecoder for decoding existing encoded digests into the algorithm.Digest. The Match function on the algorithm.Digest as well as the other methods described by algorithm.Matcher can be utilized to validate passwords.
The algorithm.Digest implementations include an Encode method which encodes the algorithm.Digest in the PHC String Format.
To create new algorithm.Digest results you can utilize the algorithm.Hash implementations which exist for each algorithm. The implementations utilize the functional options pattern where all options methods have the pattern With* or Without*.
Index ¶
- Constants
- func CheckPassword(password, encodedDigest string) (valid bool, err error)
- func CheckPasswordWithPlainText(password, encodedDigest string) (valid bool, err error)
- func Decode(encodedDigest string) (digest algorithm.Digest, err error)
- func Normalize(encodedDigest string) string
- type Decoder
- type Digest
- func (d *Digest) Encode() string
- func (d *Digest) MarshalBinary() (data []byte, err error)
- func (d *Digest) MarshalText() (data []byte, err error)
- 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) Scan(src any) (err error)
- func (d *Digest) String() string
- func (d *Digest) UnmarshalBinary(data []byte) (err error)
- func (d *Digest) UnmarshalText(data []byte) (err error)
- func (d *Digest) Value() (value driver.Value, err error)
- type NullDigest
- func (d *NullDigest) Encode() string
- func (d *NullDigest) MarshalBinary() (data []byte, err error)
- func (d *NullDigest) MarshalText() (data []byte, err error)
- func (d *NullDigest) Match(password string) (match bool)
- func (d *NullDigest) MatchAdvanced(password string) (match bool, err error)
- func (d *NullDigest) MatchBytes(passwordBytes []byte) (match bool)
- func (d *NullDigest) MatchBytesAdvanced(passwordBytes []byte) (match bool, err error)
- func (d *NullDigest) Scan(src any) (err error)
- func (d *NullDigest) String() string
- func (d *NullDigest) UnmarshalBinary(data []byte) (err error)
- func (d *NullDigest) UnmarshalText(data []byte) (err error)
- func (d *NullDigest) Value() (value driver.Value, err error)
Constants ¶
const ( // StorageFormatPrefixLDAPCrypt is a prefix used by OpenLDAP for crypt format encoded digests. StorageFormatPrefixLDAPCrypt = "{CRYPT}" // StorageFormatPrefixLDAPArgon2 is a prefix used by OpenLDAP for argon2 format encoded digests. StorageFormatPrefixLDAPArgon2 = "{ARGON2}" )
const ( // Delimiter for all storage formats. Delimiter = encoding.DelimiterStr )
Variables ¶
This section is empty.
Functions ¶
func CheckPassword ¶
CheckPassword takes the string password and an encoded digest. It decodes the Digest, then performs the MatchAdvanced() function on the Digest. If any process returns an error it returns false with the error, otherwise it returns the result of MatchAdvanced(). This is just a helper function and implementers can manually invoke this process themselves in situations where they may want to store the Digest to perform matches at a later date to avoid decoding multiple times for example.
CRITICAL STABILITY NOTE: the decoders loaded via this function are not guaranteed to remain the same. It is strongly recommended that users implementing this library use the NewDecoder function and explicitly register each decoder which they wish to support.
func CheckPasswordWithPlainText ¶
CheckPasswordWithPlainText is the same as CheckPassword however it also allows the plaintext passwords.
CRITICAL STABILITY NOTE: the decoders loaded via this function are not guaranteed to remain the same. It is strongly recommended that users implementing this library use the NewDecoder function and explicitly register each decoder which they wish to support.
func Decode ¶
Decode is a convenience function which wraps the Decoder functionality. It's recommended to create your own decoder instead via NewDecoder or NewDefaultDecoder.
CRITICAL STABILITY NOTE: the decoders loaded via this function are not guaranteed to remain the same. It is strongly recommended that users implementing this library use the NewDecoder function and explicitly register each decoder which they wish to support.
Types ¶
type Decoder ¶ added in v0.2.0
type Decoder struct {
// contains filtered or unexported fields
}
Decoder is a struct which allows registering algorithm.DecodeFunc's and utilizing the programmatically to decode an encoded digest with them.
func NewDecoder ¶ added in v0.2.0
func NewDecoder() *Decoder
NewDecoder returns a new empty *Decoder.
See Also: NewDefaultDecoder and NewDecoderAll.
func NewDecoderAll ¶ added in v0.2.0
NewDecoderAll is the same as NewDefaultDecoder but it also adds legacy and/or insecure decoders.
Loaded Decoders (in addition to NewDefaultDecoder): plaintext, md5crypt, sha1crypt.
CRITICAL STABILITY NOTE: the decoders loaded via this function are not guaranteed to remain the same. It is strongly recommended that users implementing this library use this or NewDecodersAll only as an example for building their own decoder via NewDecoder instead which returns an empty decoder. It is much safer for security and stability to be explicit in harmony with your specific use case. It is the responsibility of the implementer to determine which password algorithms are sufficiently safe for their particular use case.
func NewDefaultDecoder ¶ added in v0.2.0
NewDefaultDecoder returns the default decoder recommended for new implementations.
Loaded Decoders: argon2, bcrypt, pbkdf2, scrypt, shacrypt.
CRITICAL STABILITY NOTE: the decoders loaded via this function are not guaranteed to remain the same. It is strongly recommended that users implementing this library use this or NewDecodersAll only as an example for building their own decoder via NewDecoder instead which returns an empty decoder. It is much safer for security and stability to be explicit in harmony with your specific use case. It is the responsibility of the implementer to determine which password algorithms are sufficiently safe for their particular use case.
func (*Decoder) RegisterDecodeFunc ¶ added in v0.2.0
func (d *Decoder) RegisterDecodeFunc(identifier string, decoder algorithm.DecodeFunc) (err error)
RegisterDecodeFunc registers a new algorithm.DecodeFunc with this Decoder against a specific identifier.
func (*Decoder) RegisterDecodePrefix ¶ added in v0.2.0
RegisterDecodePrefix registers a prefix which is matched by strings.HasPrefix.
type Digest ¶
type Digest struct {
// contains filtered or unexported fields
}
Digest is a decorator struct which wraps the algorithm.Digest and adds sql.Scanner/driver.Valuer, encoding.TextMarshaler/encoding.TextUnmarshaler, and encoding.BinaryMarshaler/encoding.BinaryUnmarshaler implementations.
func NewDigestDecode ¶ added in v0.2.0
NewDigestDecode decodes a string into a algorithm.Digest and wraps it in the convenience layer of the crypt.Digest.
func (*Digest) MarshalBinary ¶ added in v0.2.0
MarshalBinary implements encoding.BinaryMarshaler.
func (*Digest) MarshalText ¶ added in v0.2.0
MarshalText implements encoding.TextMarshaler.
func (*Digest) MatchAdvanced ¶ added in v0.2.0
MatchAdvanced decorates the algorithm.Digest MatchAdvanced function.
func (*Digest) MatchBytes ¶ added in v0.2.0
MatchBytes decorates the algorithm.Digest MatchBytes function.
func (*Digest) MatchBytesAdvanced ¶ added in v0.2.0
MatchBytesAdvanced decorates the algorithm.Digest MatchBytesAdvanced function.
func (*Digest) UnmarshalBinary ¶ added in v0.2.0
UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (*Digest) UnmarshalText ¶ added in v0.2.0
UnmarshalText implements encoding.TextUnmarshaler.
type NullDigest ¶ added in v0.2.0
type NullDigest struct {
// contains filtered or unexported fields
}
NullDigest is variation of crypt.Digest which accepts nulls.
func NewNullDigest ¶ added in v0.2.0
func NewNullDigest(d algorithm.Digest) (digest *NullDigest)
NewNullDigest wraps an algorithm.Digest in the convenience layer of the crypt.NullDigest.
func NewNullDigestDecode ¶ added in v0.2.0
func NewNullDigestDecode(encodedDigest string) (digest *NullDigest, err error)
NewNullDigestDecode decodes a string into a algorithm.Digest and wraps it in the convenience layer of the crypt.NullDigest.
func (*NullDigest) Encode ¶ added in v0.2.0
func (d *NullDigest) Encode() string
Encode decorates the algorithm.Digest Encode function.
func (*NullDigest) MarshalBinary ¶ added in v0.2.0
func (d *NullDigest) MarshalBinary() (data []byte, err error)
MarshalBinary implements encoding.BinaryMarshaler.
func (*NullDigest) MarshalText ¶ added in v0.2.0
func (d *NullDigest) MarshalText() (data []byte, err error)
MarshalText implements encoding.TextMarshaler.
func (*NullDigest) Match ¶ added in v0.2.0
func (d *NullDigest) Match(password string) (match bool)
Match decorates the algorithm.Digest Match function.
func (*NullDigest) MatchAdvanced ¶ added in v0.2.0
func (d *NullDigest) MatchAdvanced(password string) (match bool, err error)
MatchAdvanced decorates the algorithm.Digest MatchAdvanced function.
func (*NullDigest) MatchBytes ¶ added in v0.2.0
func (d *NullDigest) MatchBytes(passwordBytes []byte) (match bool)
MatchBytes decorates the algorithm.Digest MatchBytes function.
func (*NullDigest) MatchBytesAdvanced ¶ added in v0.2.0
func (d *NullDigest) MatchBytesAdvanced(passwordBytes []byte) (match bool, err error)
MatchBytesAdvanced decorates the algorithm.Digest MatchBytesAdvanced function.
func (*NullDigest) Scan ¶ added in v0.2.0
func (d *NullDigest) Scan(src any) (err error)
Scan implements sql.Scanner.
func (*NullDigest) String ¶ added in v0.2.0
func (d *NullDigest) String() string
String decorates the algorithm.Digest String function.
func (*NullDigest) UnmarshalBinary ¶ added in v0.2.0
func (d *NullDigest) UnmarshalBinary(data []byte) (err error)
UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (*NullDigest) UnmarshalText ¶ added in v0.2.0
func (d *NullDigest) UnmarshalText(data []byte) (err error)
UnmarshalText implements encoding.TextUnmarshaler.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package algorithm is a package which contains the individual algorithms and interfaces related to their implementation.
|
Package algorithm is a package which contains the individual algorithms and interfaces related to their implementation. |
argon2
Package argon2 provides helpful abstractions for an implementation of RFC9106 and implements github.com/go-crypt/crypt interfaces.
|
Package argon2 provides helpful abstractions for an implementation of RFC9106 and implements github.com/go-crypt/crypt interfaces. |
bcrypt
Package bcrypt provides helpful abstractions for an implementation of bcrypt and implements github.com/go-crypt/crypt interfaces.
|
Package bcrypt provides helpful abstractions for an implementation of bcrypt and implements github.com/go-crypt/crypt interfaces. |
md5crypt
Package md5crypt provides helpful abstractions for an implementation of crypt (MD5) and implements github.com/go-crypt/crypt interfaces.
|
Package md5crypt provides helpful abstractions for an implementation of crypt (MD5) and implements github.com/go-crypt/crypt interfaces. |
pbkdf2
Package pbkdf2 provides helpful abstractions for an implementation of PBKDF2 and implements github.com/go-crypt/crypt interfaces.
|
Package pbkdf2 provides helpful abstractions for an implementation of PBKDF2 and implements github.com/go-crypt/crypt interfaces. |
plaintext
Package plaintext implements github.com/go-crypt/crypt interfaces with variants of plaintext useful for easy uptake of previously unhashed passwords.
|
Package plaintext implements github.com/go-crypt/crypt interfaces with variants of plaintext useful for easy uptake of previously unhashed passwords. |
scrypt
Package scrypt provides helpful abstractions for an implementation of RFC7914 and implements github.com/go-crypt/crypt interfaces.
|
Package scrypt provides helpful abstractions for an implementation of RFC7914 and implements github.com/go-crypt/crypt interfaces. |
sha1crypt
Package sha1crypt provides helpful abstractions for an implementation of crypt (SHA1) and implements github.com/go-crypt/crypt interfaces.
|
Package sha1crypt provides helpful abstractions for an implementation of crypt (SHA1) and implements github.com/go-crypt/crypt interfaces. |
shacrypt
Package shacrypt provides helpful abstractions for an implementation of SHA-crypt and implements github.com/go-crypt/crypt interfaces.
|
Package shacrypt provides helpful abstractions for an implementation of SHA-crypt and implements github.com/go-crypt/crypt interfaces. |
internal
|
|
encoding
Package encoding is an internal encoding helper package.
|
Package encoding is an internal encoding helper package. |
math
Package math is an internal mathematics helper package.
|
Package math is an internal mathematics helper package. |
random
Package random is an internal helper package.
|
Package random is an internal helper package. |