Documentation ¶
Index ¶
- Constants
- Variables
- func Argon2iHashSize(keyLen int) int
- func Argon2idHashSize(keyLen int) int
- func Base64Decode(src []byte) ([]byte, error)
- func Base64Encode(src []byte) []byte
- func BcryptHashSize() int
- func Compare(v Validator, hashed []byte, password string) (err error)
- func CompareHashes(x, y []byte) bool
- func CompareInt(a, b int64, rel BinRelation) bool
- func CompareUint(a, b uint64, rel BinRelation) bool
- func EncodePHCHash(w io.Writer, hash string, minLength, maxLength int) (int, error)
- func EncodePHCID(w io.Writer, id string) (int, error)
- func EncodePHCParams(w io.Writer, values []string, infos []*PHCParamInfo) (int, error)
- func EncodePHCSalt(w io.Writer, salt string, minLength, maxLength int) (int, error)
- func GenSalt(numBytes int) ([]byte, error)
- func Generate(h Hasher, password string) (res []byte, err error)
- func PHCParseParams(s string, infos []*PHCParamInfo) ([]string, error)
- func PHCSplitParam(s string) (string, string, error)
- func PHCSplitString(s string) []string
- func PHCValidateHash(hash string, minLength, maxLength int) error
- func PHCValidateID(id string) error
- func PHCValidateParamName(param string) error
- func PHCValidateSalt(salt string, minLength, maxLength int) error
- func PHCValidateValue(value string, maxLength int) error
- func ParseAlgIgnoreLine(line string) (algorithm string, err error)
- func ParseArgon2Conf(hashed []byte) (*PHC, *Argon2Conf, error)
- func ParseHeadLine(line string) (algorithm, name string, err error)
- func Pow(base, exp int64) int64
- func SycryptHashSize(keyLen int) int
- func WriteArgon2iConstraints(w io.Writer, cs []Argon2Constraint) (int, error)
- func WriteArgon2idConstraints(w io.Writer, cs []Argon2Constraint) (int, error)
- func WriteBcryptConstraints(w io.Writer, cs []BcryptConstraint) (int, error)
- func WriteHasherConf(w io.Writer, hasher Hasher) (int, error)
- func WriteScryptConstraints(w io.Writer, cs []ScryptConstraint) (int, error)
- type AbstractArgon2iConstraint
- type AbstractArgon2idConstraint
- type AbstractBcryptConstraint
- type AbstractScryptConstraint
- type AccType
- type AlgConstraint
- type AlgIDError
- type Argon2Conf
- type Argon2Constraint
- type Argon2iAcc
- type Argon2iConf
- type Argon2iData
- type Argon2iHasher
- type Argon2iValidator
- type Argon2idAcc
- type Argon2idConf
- type Argon2idData
- type Argon2idHasher
- type Argon2idValidator
- type Base64Encoding
- type BcryptAcc
- type BcryptConf
- type BcryptConstraint
- type BcryptHasher
- type BcryptValidator
- type BinRelation
- func ParseConstraintInt(line string, bitSize int) (lhs string, rhs int64, rel BinRelation, err error)
- func ParseConstraintLine(line string) (lhs, rhs string, rel BinRelation, err error)
- func ParseConstraintUint(line string, bitSize int) (lhs string, rhs uint64, rel BinRelation, err error)
- func ParseRelation(s string) (BinRelation, error)
- type Constraint
- type ConstraintConjunction
- type ConstraintDisjunction
- type ConstraintSyntaxError
- type ConstraintsCol
- type HashAlg
- type HashGenerator
- type Hasher
- type MultiConstraint
- type PHC
- type PHCError
- type PHCInfo
- type PHCParamInfo
- type PasswordMismatchError
- type ScryptAcc
- type ScryptConf
- type ScryptConstraint
- type ScryptData
- type ScryptHasher
- type ScryptValidator
- type SyntaxError
- type UnknownAlgError
- type Validator
- type ValidatorFunc
- type VersionError
Constants ¶
const ( // BcryptName is the name of the bcrypt algorithm. BcryptName = "bcrypt" // ScryptName is the name of the scrypt algorithm. ScryptName = "scrypt" // Argon2iName is the name of the Argon2i algorithm. Argon2iName = "argon2i" // Argon2idName is the name of the Argon2id algorithm. Argon2idName = "argon2id" // BcryptPrefix is the algorithm prefix in the hash encoding. BcryptPrefix = "$2a" // ScryptPrefix is the algorithm prefix in the hash encoding. ScryptPrefix = "$scrypt" // Argon2iPrefix is the algorithm prefix in the hash encoding. Argon2iPrefix = "$argon2i$" // Argon2idPrefix is the algorithm prefix in the hash encoding. Argon2idPrefix = "$argon2id$" )
const Defaultalphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Defaultalphabet is the alphabet used by most algorithms.
const ( // MaxIntLength is the maximal length that we assume an integer encoding // as a string can have. MaxIntLength = 20 )
Variables ¶
var ( // ConstraintLineRx is the regex used to parse a single constraint line. ConstraintLineRx = regexp.MustCompile(`^\s*([a-zA-Z]+)\s+(<|>|<=|>=|=|≤|≥)\s+(-?\d+)\s*$`) // HeadLineRx is the regex used to parse a heading line. HeadLineRx = regexp.MustCompile(`^\s*\[\s*(\w+)(\s*=\s*(\w+))?\s*\]\s*$`) // IgnoreAlgLineRx is the regex used to parse a algorithm ignore line. IgnoreAlgLineRx = regexp.MustCompile(`^\s*ignore\s+([a-zA-Z]+)\s*$`) )
var ( // Bcrypt is a bcrypt Hasher. Bcrypt = NewBcryptHasher(nil) // Scrypt is a scrypt Hasher. Scrypt = NewScryptHasher(nil) // Argon2i is a argon2 Hasher using the Argon2i key function. Argon2i = NewArgon2iHasher(nil) // Argon2id is a argon2 Hasher using the Argon2id key function. // Argon2id is considered more secure than Argon2i. Argon2id = NewArgon2idHasher(nil) // DefaultHasher ia a rather secure Hasher that should be safe to be used // by most applications. At the moment it's Argon2id with the default // paramters. DefaultHasher = NewArgon2idHasher(nil) )
var ( // BcryptVal is a Validator for bcrypt encoded hashes. BcryptVal = BcryptValidator{} // ScryptVal is a Validator for scrypt encoded hashes. ScryptVal = ScryptValidator{} // Argon2iVal is a Validator for argon2i encoded hashes. Argon2iVal = Argon2iValidator{} // Argon2idVal is a Validator for argon2id encoded hashes. Argon2idVal = Argon2idValidator{} )
var ( // PHCScryptConfig is the phc description of scrypt hashes. PHCScryptConfig = &PHCInfo{ MinSaltLength: -1, MaxSaltLength: -1, MinHashLength: -1, MaxHashLength: -1, ParamInfos: []*PHCParamInfo{ &PHCParamInfo{"ln", 2, false}, &PHCParamInfo{"r", -1, false}, &PHCParamInfo{"p", -1, false}, }, } // PHCArgon2Config is the phc description of argon2 hashes. PHCArgon2Config = &PHCInfo{ MinSaltLength: -1, MaxSaltLength: -1, MinHashLength: -1, MaxHashLength: -1, ParamInfos: []*PHCParamInfo{ &PHCParamInfo{"m", 10, false}, &PHCParamInfo{"t", 10, false}, &PHCParamInfo{"p", 3, false}, }, } )
var DefaultBcryptConf = &BcryptConf{Cost: 12}
DefaultBcryptConf is the default configuration for bcrypt.
var DefaultEncoding = NewBase64Encoding(Defaultalphabet)
DefaultEncoding is the internal encoding based on Defaultalphabet.
var DefaultScryptConf = NewScryptConf(16, 8, 1, 64)
DefaultScryptConf is the default configuration for scrypt.
Functions ¶
func Argon2iHashSize ¶
Argon2iHashSize returns the maximal hash size of a argon2i hash with a key and salt of with KeyLen bytes. The length is the maximal length, not the actual length.
func Argon2idHashSize ¶
Argon2idHashSize returns the maximal hash size of a argon2id hash with a key and salt of with KeyLen bytes. The length is the maximal length, not the actual length.
func Base64Decode ¶
Base64Decode decodes the source using the encoding.
func Base64Encode ¶
Base64Encode encodes the source to base64 using the default encoding.
func BcryptHashSize ¶
func BcryptHashSize() int
BcryptHashSize returns the hash size of bcrypt hashes.
func Compare ¶
Compare wraps a call to v.Compare(hashed, password) and recovers from any panic that might occur, it's advised to always use Compare instead of using the validator directly. v is not allowed to be nil.
func CompareHashes ¶
CompareHashes uses a constant time compare algorithm to compare to key hashes. Constant time compare functions are important or otherwise attackers might infer knowledge about the real password.
func CompareInt ¶
func CompareInt(a, b int64, rel BinRelation) bool
CompareInt compares two integers given the relation. For example CompareInt(21, 42, Less) would return true. For an unkown relation it returns false and logs a warning.
func CompareUint ¶
func CompareUint(a, b uint64, rel BinRelation) bool
CompareUint compares to uints given the relation. It works as CompareInt, but works with uints instead of ints.
func EncodePHCHash ¶
EncodePHCHash writes the hash to the writer. If the hash is empty nothing is written. It returns the number of bytes written. An error might occur if the hash is invalid (according to min/max length) or if writing to w fails. The length can be < 0 in which case they're ignored.
func EncodePHCID ¶
EncodePHCID writes the algorithm id in the phc format to the writer. The string written is simply "$ID". It returns the number of bytes written.
func EncodePHCParams ¶
EncodePHCParams writes the pch parameters to the writer. values and infos must be of the same length. See PCH type documentation for more details. If no parameters are written (parameter is empty, all parameters are optional and not given) nothing is written to the writer. It returns the number of bytes written. An error might occur if the parameter values do not match the description (length, non-optional and not given etc.) and if writing to w fails.
func EncodePHCSalt ¶
EncodePHCSalt writes the salt to the writer. If the salt is empty nothing is written. It returns the number of bytes written. An error might occur if the salt is invalid (according to min/max length) or if writing to w fails. The length can be < 0 in which case they're ignored.
func Generate ¶
Generate wraps a call to h.Generate and recovers from any panic that might occur, it's advised to always use Generate instead of using the hasher directly. h is not allowed to be nil.
func PHCParseParams ¶
func PHCParseParams(s string, infos []*PHCParamInfo) ([]string, error)
PHCParseParams parses the parameter part from a phc string. That is the list of parameter / value pairs. infos is the information about all parameters, the result is always exactly the size of the infos. Optional parameters are set to "". This function validates all names, the order of the parameters and validates the value as well.
func PHCSplitParam ¶
PHCSplitParam parses a single phc parameter of the form PARAM=VALUE. No validation checking is performed: No test if the param or value is a valid sequence of characters. It just finds the first = in the string and splits accordingly.
func PHCSplitString ¶
PHCSplitString splits the string according to the separator $.
func PHCValidateHash ¶
PHCValidateHash checks if a string is a valid phc hash. It checks if the string only contains valid characters and if the length of the hash is in the given boundaries. Setting a boundary to -1 means that there is no restriction.
func PHCValidateID ¶
PHCValidateID checks if a string is a valid phc id. The function tests if the string has a length of at most 32 and contains only valid characters.
func PHCValidateParamName ¶
PHCValidateParamName checks if a string is a valid phc parameter name. The function tests if the string has a length of at most 32 and contains only valid characters.
func PHCValidateSalt ¶
PHCValidateSalt checks if a string is a valid phc salt. It checks if the string only contains valid characters and if the length of the salt is in the given boundaries. Setting a boundary to -1 means that there is no restriction.
func PHCValidateValue ¶
PHCValidateValue checks if a string is a valid phc parameter value. It cecks if the string only contains valid characters and if the length is not greater than maxLength, MaxLength < 0 meaning that now boundary exists.
func ParseAlgIgnoreLine ¶
ParseAlgIgnoreLine parses a algorithm ignore line (with IgnoreAlgLineRx). Example of a line: "ignore bcrypt". This would return "bcrypt". This function does not check if the algorithm name is valid, for example "ignore foo" would be valid.
func ParseArgon2Conf ¶
func ParseArgon2Conf(hashed []byte) (*PHC, *Argon2Conf, error)
func ParseHeadLine ¶
ParseHeadLine parses a heading line (with HeadLineRx). Example of a line "[bcypt]" or with a name [scrypt = foo]. The first one yields to "bcrypt" and the empty string, the second to "scrypt" and "foo". This function does not check if the algorithm name is valid, for example "[foo]" would be valid.
func SycryptHashSize ¶
SycryptHashSize returns the maximal hash size of a scrypt hash with a key and salt of with KeyLen bytes. The length is the maximal length, not the actual length.
func WriteArgon2iConstraints ¶
func WriteArgon2iConstraints(w io.Writer, cs []Argon2Constraint) (int, error)
WriteArgon2iConstraints writes all constraints to a file s.t. they can be parsed again later.
func WriteArgon2idConstraints ¶
func WriteArgon2idConstraints(w io.Writer, cs []Argon2Constraint) (int, error)
WriteArgon2idConstraints writes all constraints to a file s.t. they can be parsed again later.
func WriteBcryptConstraints ¶
func WriteBcryptConstraints(w io.Writer, cs []BcryptConstraint) (int, error)
WriteBcryptConstraints writes all constraints to a file s.t. they can be parsed again later.
func WriteHasherConf ¶
WriteHasherConf writes the config of a hasher in a format that can be read by ParseHasher.
func WriteScryptConstraints ¶
func WriteScryptConstraints(w io.Writer, cs []ScryptConstraint) (int, error)
WriteScryptConstraints writes all constraints to a file s.t. they can be parsed again later.
Types ¶
type AbstractArgon2iConstraint ¶
type AbstractArgon2iConstraint interface {
CheckArgon2i(data *Argon2iData) bool
}
AbstractArgon2iConstraint is a constraint based on argon2i data.
type AbstractArgon2idConstraint ¶
type AbstractArgon2idConstraint interface {
CheckArgon2id(data *Argon2idData) bool
}
AbstractArgon2idConstraint is a constraint based on argon2id data.
type AbstractBcryptConstraint ¶
type AbstractBcryptConstraint interface {
CheckBcrypt(conf *BcryptConf) bool
}
AbstractBcryptConstraint is a constraint based on bcrypt configs.
type AbstractScryptConstraint ¶
type AbstractScryptConstraint interface {
CheckScrypt(data *ScryptData) bool
}
AbstractScryptConstraint is a constraint based on scrypt data.
type AccType ¶
type AccType int
AccType is an accumlator type used to combine constraints. Constraints can be a Conjunction (and) or Disjunction (or).
type AlgConstraint ¶
type AlgConstraint HashAlg
AlgConstraint implements Constraint and only tests if the hashed string represents a specific type.
func NewAlgConstraint ¶
func NewAlgConstraint(alg HashAlg) AlgConstraint
NewAlgConstraint returns a new AlgConstraint that returns true if the algorithm represented by a hashed password is alg.
func (AlgConstraint) Check ¶
func (c AlgConstraint) Check(hashed []byte) bool
Check implements the Constraint interface.
type AlgIDError ¶
AlgIDError is returned if a hash version has the wrong prefix for a certain Validator. This does not mean that the hashes version is invalid in general, only for the specific Validator.
func NewAlgIDError ¶
func NewAlgIDError(prefix, expected, got string) AlgIDError
NewAlgIDError returns a new AlgIDError. Prefix is appended to the error message. Expected and got are the resp. ids.
func (AlgIDError) Error ¶
func (err AlgIDError) Error() string
Error returns the string representation of the error.
func (AlgIDError) String ¶
func (err AlgIDError) String() string
String returns the string representation of the error.
type Argon2Conf ¶
Argon2Conf contains all parameters for argon2, it is used by argon2i and argon2id.
func (*Argon2Conf) Copy ¶
func (conf *Argon2Conf) Copy() *Argon2Conf
Copy returns a copy of a config.
func (*Argon2Conf) String ¶
func (conf *Argon2Conf) String() string
String returns a human-readable string reprensetation.
type Argon2Constraint ¶
type Argon2Constraint struct { Bound uint64 VarName string Rel BinRelation }
Argon2Constraint imposes a restriction on one of the parameters of an Argon2Conf. The parameter is describec by the string name. It imposes the restriction [VarName] [Rel] [Bound]. For example time < 2.
It implements both AbstractArgon2iConstraint and AbstractArgon2idConstraint.
VarName must be either "time", "memory", "keylen" or "threads".
func NewArgon2Constraint ¶
func NewArgon2Constraint(bound uint64, varName string, rel BinRelation) Argon2Constraint
NewArgon2Constraint returns a new Argon2Constraint. It does not check if varName is valid.
func ParseArgon2Cons ¶
func ParseArgon2Cons(line string) (Argon2Constraint, error)
ParseArgon2Cons parses a constraint for argon2 (argon2i and argon2id). It allows the following format: "LHS RELATION BOUND" where LHS is either "time", "memory", "Threads" or "KeyLen".
func (Argon2Constraint) CheckArgon2i ¶
func (c Argon2Constraint) CheckArgon2i(data *Argon2iData) bool
// CheckArgon2i checks the argon2 data based on the variable name, relation and bound.
func (Argon2Constraint) CheckArgon2id ¶
func (c Argon2Constraint) CheckArgon2id(data *Argon2idData) bool
CheckArgon2id checks the argon2 data based on the variable name, relation and bound.
func (Argon2Constraint) String ¶
func (c Argon2Constraint) String() string
type Argon2iAcc ¶
type Argon2iAcc struct { Constraints []AbstractArgon2iConstraint Type AccType }
Argon2iAcc is an accumulation of argon2i constraints. It implements AbstractArgon2iConstraint.
func NewArgon2iAcc ¶
func NewArgon2iAcc(t AccType, constraints ...AbstractArgon2iConstraint) Argon2iAcc
NewArgon2iAcc returns a new Argon2iAcc given the accumulation type and the constraints it's composed of.
func (Argon2iAcc) CheckArgon2i ¶
func (acc Argon2iAcc) CheckArgon2i(data *Argon2iData) bool
CheckArgon2i composes the constraints based on the accumulation type.
type Argon2iConf ¶
type Argon2iConf struct {
*Argon2Conf
}
Argon2iConf contains all parameters for argon2i.
func TuneArgon2i ¶
func TuneArgon2i(base *Argon2iConf, duration time.Duration) (*Argon2iConf, time.Duration, error)
TuneArgon2i runs argon2i with increasing time values until an average runtime of at least duration is reached. Do not use this function to automatically compute your configuration, it is not safe enough! Run it, check the result and draw your own conclusions.
func (*Argon2iConf) Copy ¶
func (conf *Argon2iConf) Copy() *Argon2iConf
Copy returns a copy of a config.
type Argon2iData ¶
type Argon2iData struct { *Argon2iConf Salt, Key string RawSalt, RawKey []byte }
Argon2iData stores in addition to a config also the salt and key, both base64 encoded (Salt and Key) as well as the raw version (decoded from Salt and Key).
func ParseArgon2iData ¶
func ParseArgon2iData(hashed []byte) (*Argon2iData, error)
ParseArgon2iData parses argon2i data from the hashed version.
type Argon2iHasher ¶
type Argon2iHasher struct {
*Argon2iConf
}
Argon2iHasher is a Hasher using argon2i.
func NewArgon2iHasher ¶
func NewArgon2iHasher(conf *Argon2iConf) *Argon2iHasher
NewArgon2iHasher returns a new NewArgon2iHasher with the given parameter.
func (*Argon2iHasher) Copy ¶
func (h *Argon2iHasher) Copy() *Argon2iHasher
Copy returns a copy of the hasher.
type Argon2iValidator ¶
type Argon2iValidator struct{}
Argon2iValidator implements Validator for argon2i hashes.
type Argon2idAcc ¶
type Argon2idAcc struct { Constraints []AbstractArgon2idConstraint Type AccType }
Argon2idAcc is an accumulation of argon2id constraints. It implements AbstractArgon2idConstraint.
func NewArgon2idAcc ¶
func NewArgon2idAcc(t AccType, constraints ...AbstractArgon2idConstraint) Argon2idAcc
NewArgon2idAcc returns a new Argon2idAcc given the accumulation type and the constraints it's composed of.
func (Argon2idAcc) CheckArgon2id ¶
func (acc Argon2idAcc) CheckArgon2id(data *Argon2idData) bool
CheckArgon2id composes the constraints based on the accumulation type.
type Argon2idConf ¶
type Argon2idConf struct {
*Argon2Conf
}
Argon2idConf contains all parameters for argon2id.
func TuneArgon2id ¶
func TuneArgon2id(base *Argon2idConf, duration time.Duration) (*Argon2idConf, time.Duration, error)
TuneArgon2id runs argon2id with increasing time values until an average runtime of at least duration is reached. Do not use this function to automatically compute your configuration, it is not safe enough! Run it, check the result and draw your own conclusions.
func (*Argon2idConf) Copy ¶
func (conf *Argon2idConf) Copy() *Argon2idConf
Copy returns a copy of a config.
type Argon2idData ¶
type Argon2idData struct { *Argon2idConf // encoded with base64 Salt, Key string RawSalt, RawKey []byte }
Argon2idData stores in addition to a config also the salt and key, both base64 encoded (Salt and Key) as well as the raw version (decoded from Salt and Key).
func ParseArgon2idData ¶
func ParseArgon2idData(hashed []byte) (*Argon2idData, error)
ParseArgon2idData parses argon2id data from the hashed version.
type Argon2idHasher ¶
type Argon2idHasher struct {
*Argon2idConf
}
Argon2idHasher is a Hasher using argon2id.
func NewArgon2idHasher ¶
func NewArgon2idHasher(conf *Argon2idConf) *Argon2idHasher
NewArgon2idHasher returns a new NewArgon2idHasher with the given parameter.
func (*Argon2idHasher) Copy ¶
func (h *Argon2idHasher) Copy() *Argon2idHasher
Copy returns a copy of the hasher.
type Argon2idValidator ¶
type Argon2idValidator struct{}
Argon2idValidator implements Validator for argon2id hashes.
type Base64Encoding ¶
Base64Encoding is the internal reprensetation of a base64 encoding. The base64 encoding / decoding is inspired by https://github.com/golang/crypto/blob/master/bcrypt/base64.go
func NewBase64Encoding ¶
func NewBase64Encoding(alphabet string) *Base64Encoding
NewBase64Encoding returns a new Base64Encoding.
func (*Base64Encoding) Base64Decode ¶
func (enc *Base64Encoding) Base64Decode(src []byte) ([]byte, error)
Base64Decode decodes the source using the alphabet.
func (*Base64Encoding) Base64Encode ¶
func (enc *Base64Encoding) Base64Encode(src []byte) []byte
Base64Encode encodes the source to base64 using the alphabet.
func (*Base64Encoding) SetAlphabet ¶
func (enc *Base64Encoding) SetAlphabet(alphabet string)
SetAlphabet sets the encoding alphabet to a new alphabet.
type BcryptAcc ¶
type BcryptAcc struct { Constraints []AbstractBcryptConstraint Type AccType }
BcryptAcc is an accumulation of bcrypt constraints. It implements AbstractBcryptConstraint.
func NewBcryptAcc ¶
func NewBcryptAcc(t AccType, constraints ...AbstractBcryptConstraint) BcryptAcc
NewBcryptAcc returns a new BcryptAcc given the accumulation type and the constraints it's composed of.
func (BcryptAcc) CheckBcrypt ¶
func (acc BcryptAcc) CheckBcrypt(conf *BcryptConf) bool
CheckBcrypt composes the constraints based on the accumulation type.
type BcryptConf ¶
type BcryptConf struct {
Cost int
}
BcryptConf contains all parameters for bcrypt.
func ParseBcryptConf ¶
func ParseBcryptConf(hashed []byte) (*BcryptConf, error)
ParseBcryptConf parses a configuration from a hashes version.
func TuneBcrypt ¶
func TuneBcrypt(base *BcryptConf, duration time.Duration) (*BcryptConf, time.Duration, error)
TuneBcrypt runs bcrypt with increasing cost values until an average runtime of at least duration is reached. Do not use this function to automatically compute your configuration, it is not safe enough! Run it, check the result and draw your own conclusions.
func (*BcryptConf) Copy ¶
func (conf *BcryptConf) Copy() *BcryptConf
Copy returns a copy of a config.
func (*BcryptConf) String ¶
func (conf *BcryptConf) String() string
String returns a human-readable string reprensetation.
type BcryptConstraint ¶
type BcryptConstraint struct { CostBound int64 Rel BinRelation }
BcryptConstraint implements AbstractBcryptConstraint and imposes a restriction on the cost of the config.
func NewBcryptConstraint ¶
func NewBcryptConstraint(bound int, rel BinRelation) BcryptConstraint
NewBcryptConstraint returns a new BcryptConstraint.
func ParseBcryptCons ¶
func ParseBcryptCons(line string) (BcryptConstraint, error)
ParseBcryptCons parses a constraint for bcrypt. The only allowed form is "cost RELATION BOUND".
func (BcryptConstraint) CheckBcrypt ¶
func (c BcryptConstraint) CheckBcrypt(conf *BcryptConf) bool
CheckBcrypt checks the cost according to the provided relation.
func (BcryptConstraint) String ¶
func (c BcryptConstraint) String() string
type BcryptHasher ¶
type BcryptHasher struct {
*BcryptConf
}
BcryptHasher is a Hasher using bcrypt.
func NewBcryptHasher ¶
func NewBcryptHasher(conf *BcryptConf) *BcryptHasher
NewBcryptHasher returns a new BcryptHasher with the given parameters.
func (*BcryptHasher) Copy ¶
func (h *BcryptHasher) Copy() BcryptHasher
Copy returns a copy of the hasher.
type BcryptValidator ¶
type BcryptValidator struct{}
BcryptValidator implements Validator for bcrypt hashes.
type BinRelation ¶
type BinRelation int
BinRelation is a type used to identify relations on integers (<, = etc.).
const ( // Less describes the relation <. Less BinRelation = iota // Greater describes the relation >. Greater // Leq describes the relation ≤. Leq // Geq describes the relation ≥. Geq // Eq describes the equality relation =. Eq )
func ParseConstraintInt ¶
func ParseConstraintInt(line string, bitSize int) (lhs string, rhs int64, rel BinRelation, err error)
ParseConstraintInt works as ParseConstraintLine but the right-hand side is parsed into a int64 (with the given bitSize).
func ParseConstraintLine ¶
func ParseConstraintLine(line string) (lhs, rhs string, rel BinRelation, err error)
ParseConstraintLine parses a single line constraint line (with ConstraintLineRx). It returns the lhs and rhs as a string. Example of a line "cost < 10" that would return "cost" "10" Less. This function does not check if the identifiers are valid. For example "foo < 10" would be valid.
func ParseConstraintUint ¶
func ParseConstraintUint(line string, bitSize int) (lhs string, rhs uint64, rel BinRelation, err error)
ParseConstraintUint works as ParseConstraintInt but parses a uint.
func ParseRelation ¶
func ParseRelation(s string) (BinRelation, error)
ParseRelation parses the relation type from a string. It accepts the "obvious" symbols like <, >=, =. It also accepts ≤ and ≥.
func (BinRelation) String ¶
func (rel BinRelation) String() string
type Constraint ¶
Constraint describes a property a hasher must have. Usually they inclucde a restriction on the type of the hasher and restrictions on the hasher's parameters. Like: A bcrypt hasher with cost < 10. Constraints are used to find hashes that should be renewed / replaced. A constraints check function gets a hashed entry as input and decides what to do with it, like decoding it. Usually there are accumlator functions to avoid decoding an entry again and again.
type ConstraintConjunction ¶
type ConstraintConjunction []Constraint
ConstraintConjunction is a conjunction of Constraints and itself implements the Constraint interface. An empty conjunction is considered true.
func NewConstraintConjunction ¶
func NewConstraintConjunction(constraints ...Constraint) ConstraintConjunction
NewConstraintConjunction returns a new conjunction.
func (ConstraintConjunction) Check ¶
func (conj ConstraintConjunction) Check(hashed []byte) bool
Check checks if all conjuncts are true.
type ConstraintDisjunction ¶
type ConstraintDisjunction []Constraint
ConstraintDisjunction is a disjunction of Constraints and itself implements the Constraint interface.
func NewConstraintDisjunction ¶
func NewConstraintDisjunction(constraints ...Constraint) ConstraintDisjunction
NewConstraintDisjunction returns a new disjunction.
func (ConstraintDisjunction) Check ¶
func (disj ConstraintDisjunction) Check(hashed []byte) bool
Check checks if at least one disjunct is true.
type ConstraintSyntaxError ¶
type ConstraintSyntaxError string
ConstraintSyntaxError is an error returned if an error occurred due to invalid syntax while parsing constraints.
func NewConstraintSyntaxError ¶
func NewConstraintSyntaxError(cause string) ConstraintSyntaxError
NewConstraintSyntaxError returns a new ConstraintSyntaxError.
func (ConstraintSyntaxError) Error ¶
func (err ConstraintSyntaxError) Error() string
type ConstraintsCol ¶
type ConstraintsCol struct { AlgConstraints []HashAlg BcryptConstraints []BcryptConstraint ScryptConstraints []ScryptConstraint Argon2iConstraints []Argon2Constraint Argon2idConstraints []Argon2Constraint }
ConstraintsCol is a collection of "standard" constraints (for bcrypt, scrypt, argon2i and argon2id). Such a collection can be parsed from a file (or any reader with the correct syntax) with ParseConstraints.
It also stores a list of all algorithms that should be completely ignored (AlgConstraints).
func NewConstraintCol ¶
func NewConstraintCol() *ConstraintsCol
NewConstraintCol returns an empty constraints collection.
func ParseConstraints ¶
func ParseConstraints(r io.Reader) (*ConstraintsCol, error)
ParseConstraints parses all constraints from a reader, see the README for more details.
func ParseConstraintsFromFile ¶
func ParseConstraintsFromFile(filename string) (*ConstraintsCol, error)
ParseConstraintsFromFile works like ParseConstraints and reads the content from a file.
func (*ConstraintsCol) WriteConstraints ¶
WriteConstraints writes all constraints to a file. The output is a file that can be parsed by ParseConstraints. If printTime is true a timestamp will be added as a comment to the output specifying the time the config file was created.
type HashAlg ¶
type HashAlg int
HashAlg is a type used to enumerate all implemented algorithms.
func GuessAlg ¶
GuessAlg returns the algorithm used to create the specified hashed version. If the algorithm is unknown it returns -1.
type HashGenerator ¶
HashGenerator is an interface describing all algorithms that can be used to directly create a hashed version of a password. The difference between HashGenerator and Hasher is that Hasher returns a formatted string whereas HashGenerator returns the raw generated key.
type Hasher ¶
Hasher is the general interface for creating hashed versions of passwords. The returned encoded string contains all information required for parsing the parameters of the key function (like https://openwall.info/wiki/john/sample-hashes).
func ParseHasher ¶
ParseHasher parses a hasher from a config file. The syntax is the same as for constraint, but the relation must be =.
func ParseHasherConfFile ¶
ParseHasherConfFile works as ParseHasher and reads the content from a file.
type MultiConstraint ¶
type MultiConstraint struct { BcryptConstraint AbstractBcryptConstraint ScryptConstraint AbstractScryptConstraint Argon2iConstraint AbstractArgon2iConstraint Argon2idConstraint AbstractArgon2idConstraint DefaultConstraint Constraint // contains filtered or unexported fields }
MultiConstraint composes the basic constraint types (bcrypt, scrypt argon2i and argon2id). It implements the general Constraint interface.
It's behaviour is as follows: It checks the hashed string and decides which algorithm it belongs to. First it checks if there is a general constraint for this algorithm. For example if bcrypt should be completely ignored you can use AddAlgConstraint(BcryptAlg). If this is the case it returns true. Otherwise it decodes the config / data from the hash and passes it to the corresponding constraint.
For example bcrypt hashes (beginning with $2a$) are passed to the BcryptConstraint. The config is parsed from that hash.
It also has a "fallback" constraint that is applied if the hashing algorithm is unkown.
func NewMultiConstraint ¶
func NewMultiConstraint() *MultiConstraint
NewMultiConstraint returns a new MultiConstraint where all constraints are set to nil, that is it always returns false.
func (*MultiConstraint) AddAlgConstraint ¶
func (c *MultiConstraint) AddAlgConstraint(alg HashAlg)
AddAlgConstraint adds a constraint that all hashes of algorithm alg should be ignored.
func (*MultiConstraint) Check ¶
func (c *MultiConstraint) Check(hashed []byte) bool
Check implements the Constraint interface.
func (*MultiConstraint) HasAlgConstraint ¶
func (c *MultiConstraint) HasAlgConstraint(alg HashAlg) bool
HasAlgConstraint checks if there is a constraint that all hashes of algorithm alg should be ignored.
func (*MultiConstraint) RemoveAlgConstraint ¶
func (c *MultiConstraint) RemoveAlgConstraint(alg HashAlg)
RemoveAlgConstraint removes the constraint that all hashes of algorithm alg should be ignored.
type PHC ¶
PHC is a format for hash encodings, see https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md scrypt and argon2 use this encoding, though not everything is supported at the moment.
A PHC value is used to describe the parsed data. The ID is the algorithm id, for example "scrypt". Salt and Hash are the salt and hash strings, usually that is the base64 encoding of a binary salt/hash and must be decoded first.
Params are the value of the phc parameters. The names of these parameters are given by the algorithm specification, that is before parsing. See PHCInfo type for more information. For optional parameters that are not present in the phc string the parameter value is set to the empty string "". This should be fine since each other valid value should have at least length one.
func ParsePHC ¶
ParsePHC parses a phc string and returns the result as a PHC object. The info is used to check the format against the input string.
func ParsePHCAlternate ¶
ParsePHCAlternate parses a phc string and returns the result as a PHC object. The info is used to check the format against the input string. This version is callend "Alternate" because: It was the first draft of a parse function, I wrote another one with less code, which I thought would be slower. It turned out to be faster and is now called ParsePHC. This one is here if we ever decide to build a faster one without string splitting. Maybe some of the code can be used.
func ParsePHCFromParts ¶
ParsePHCFromParts parses a phc (like ParsePHC) from the parts, that is the array of strings after splitting an input on $. This is useful if you have a slightly different encoding for a hash but want to parse it with the phc parser. For example argon2 has an additional field $v=... The argon2 parser splits the string, removes the $v=... and composes the other parts to a new string slice that is used on this function.
func (*PHC) Encode ¶
Encode encodes the pch object to a string. The info contains the specification used for the encoding. Optional parameters (set to the empty string in phc.Params) are not contained in the result.
If you want to create your own phc-like format you may want to look at the EncodePHC... functions, like EncodePHCParams. It basically combines those methods.
It returns the number of bytes written. An error might occur if the parameters do not satisfy the description or if writing to w fails.
type PHCInfo ¶
type PHCInfo struct { ParamInfos []*PHCParamInfo MinSaltLength, MaxSaltLength int MinHashLength, MaxHashLength int }
PHCInfo bundles information about a PHC hash string. It describes information about the parameters (in the ParamInfos slice, the order in the slice implies the order of the parameters) as well as minimum and maximum lengths for both the salt and the hash. All boundaries can be set to -1, meaning that no min/max value is set.
func NewPHCInfo ¶
func NewPHCInfo() *PHCInfo
NewPHCInfo returns a new PHCInfo with empty parameters and no restrictions on the salt / hash length.
type PHCParamInfo ¶
PHCParamInfo describes information about a parameter in pch. A parameter has a name (like "r" in scrypt). MaxLength is the maximal length the parameter is allowed to have. We allow -1 which means that there is no boundary. Optional should be set to true if the parameter is optional.
func NewPHCParamInfo ¶
func NewPHCParamInfo(name string) *PHCParamInfo
NewPHCParamInfo returns a new phc parameter without a max length and optional set to false.
type PasswordMismatchError ¶
type PasswordMismatchError struct{}
PasswordMismatchError is returned if a hashes version and a clear text version do not match. No password details are made public by this error.
func NewPasswordMismatchError ¶
func NewPasswordMismatchError() PasswordMismatchError
NewPasswordMismatchError returns a new PasswordMismatchError.
func (PasswordMismatchError) Error ¶
func (err PasswordMismatchError) Error() string
Error returns the string representation of the error.
func (PasswordMismatchError) String ¶
func (err PasswordMismatchError) String() string
String returns the string representation of the error.
type ScryptAcc ¶
type ScryptAcc struct { Constraints []AbstractScryptConstraint Type AccType }
ScryptAcc is an accumulation of bcrypt constraints. It implements AbstractScryptConstraint.
func NewScryptAcc ¶
func NewScryptAcc(t AccType, constraints ...AbstractScryptConstraint) ScryptAcc
NewScryptAcc returns a new ScryptAcc given the accumulation type and the constraints it's composed of.
func (ScryptAcc) CheckScrypt ¶
func (acc ScryptAcc) CheckScrypt(data *ScryptData) bool
CheckScrypt composes the constraints based on the accumulation type.
type ScryptConf ¶
type ScryptConf struct {
R, P, KeyLen int
// contains filtered or unexported fields
}
ScryptConf contains all parameters for scrypt.
func NewScryptConf ¶
func NewScryptConf(rounds, r, p, keyLen int) *ScryptConf
func TuneScrypt ¶
func TuneScrypt(base *ScryptConf, duration time.Duration) (*ScryptConf, time.Duration, error)
TuneScrypt runs scrypt with increasing N values until an average runtime of at least duration is reached. Do not use this function to automatically compute your configuration, it is not safe enough! Run it, check the result and draw your own conclusions.
func (*ScryptConf) Copy ¶
func (conf *ScryptConf) Copy() *ScryptConf
Copy returns a copy of a config.
func (*ScryptConf) GetN ¶
func (conf *ScryptConf) GetN() int
func (*ScryptConf) GetRounds ¶
func (conf *ScryptConf) GetRounds() int
func (*ScryptConf) SetRounds ¶
func (conf *ScryptConf) SetRounds(rounds int)
func (*ScryptConf) String ¶
func (conf *ScryptConf) String() string
String returns a human-readable string reprensetation.
type ScryptConstraint ¶
type ScryptConstraint struct { Bound int64 VarName string Rel BinRelation }
ScryptConstraint implements AbstractScryptConstraint and imposes a restriction on one of the parameters. The parameter is describec by the string name. It imposes the restriction [VarName] [Rel] [Bound]. For example n < 32768.
Note that n = 2^(rounds).
VarName must be either n, rounds, r, p or KeyLen.
func NewScryptConstraint ¶
func NewScryptConstraint(bound int64, varName string, rel BinRelation) ScryptConstraint
NewScryptConstraint returns a new ScryptConstraint. It does not check if varName is valid.
func ParseScryptCons ¶
func ParseScryptCons(line string) (ScryptConstraint, error)
ParseScryptCons parses a constraint for scrypt. It allows the following format: "LHS RELATION BOUND" where LHS is either "N", "R", "P" or "KeyLen".
func (ScryptConstraint) CheckScrypt ¶
func (c ScryptConstraint) CheckScrypt(data *ScryptData) bool
CheckScrypt checks the scrypt data based on the variable name, relation and bound.
func (ScryptConstraint) String ¶
func (c ScryptConstraint) String() string
type ScryptData ¶
type ScryptData struct { *ScryptConf // encoded with base64 Salt, Key string RawSalt, RawKey []byte }
ScryptData stores in addition to a config also the salt and key, both base64 encoded (Salt and Key) as well as the raw version (decoded from Salt and Key).
func ParseScryptData ¶
func ParseScryptData(hashed []byte) (*ScryptData, error)
ParseScryptData parses scrypt data from the hashed version.
type ScryptHasher ¶
type ScryptHasher struct {
*ScryptConf
}
ScryptHasher is a Hasher using scrypt.
func NewScryptHasher ¶
func NewScryptHasher(conf *ScryptConf) *ScryptHasher
NewScryptHasher returns a new ScryptHasher with the given parameters.
func (*ScryptHasher) Copy ¶
func (h *ScryptHasher) Copy() *ScryptHasher
Copy returns a copy of the hasher.
type ScryptValidator ¶
type ScryptValidator struct{}
ScryptValidator implements Validator for scrypt hashes.
type SyntaxError ¶
type SyntaxError string
SyntaxError is used if a hashed version is not stored in a valid syntax.
func NewSyntaxError ¶
func NewSyntaxError(cause string) SyntaxError
NewSyntaxError returns a new SyntaxError.
func (SyntaxError) Error ¶
func (err SyntaxError) Error() string
Error returns the string representation of the error.
func (SyntaxError) String ¶
func (err SyntaxError) String() string
String returns the string representation of the error.
type UnknownAlgError ¶
type UnknownAlgError struct{}
UnknownAlgError if a hashed version describes an unkown algorithm.
func NewUnknownAlgError ¶
func NewUnknownAlgError() UnknownAlgError
NewUnknownAlgError returns a new UnknownAlgError.
func (UnknownAlgError) Error ¶
func (err UnknownAlgError) Error() string
Error returns the string representation of the error.
func (UnknownAlgError) String ¶
func (err UnknownAlgError) String() string
String returns the string representation of the error.
type Validator ¶
Validator is an interface that provides a method to compare a hashed version of a password with a prorivde clear text version. Any error returned should be considered as an authentication fail. Only a nil return value indicates success.
There are some predefined errors that can help you to narrow the cause. But not all implementaions are required to use these errors. Special errors include: Syntax error if the hashes version can't be parsed. VersionError: IF the version used to created the hashes value is not compatible with the implemented algorithm. AlgIDError: The provided algorithm prefix does not match the prefix required by the validator. PasswordMismatchError: If the clear text version is not the password used to create the hash.
Note that a valdiator implementation provides validation for a specific hashing algorithm, like one implementation for bcrypt, scrypt etc. If you want to validate a hashed version without knowing the used algorithm use GuessValidator or GuessValidatorFunc.
func GuessValidator ¶
GuessValidator returns a validator for the hashes version. That is a clear text password can be compared with the hashed version using the validator. If the algorithm is unknown it returns nil.
type ValidatorFunc ¶
ValidatorFunc is a function that returns an error as specified in the Validator interface. GuessValidatorFunc can be used to create a ValidatorFunc from the hashed version of a password.
func GuessValidatorFunc ¶
func GuessValidatorFunc(hashed []byte) ValidatorFunc
GuessValidatorFunc guesses the algorithm based on the hashe's version. The returned error is compatible with the Validator interface specification. In addition it might return an UnknownAlgError if the algorithm cannot be guessed from the hashed version.
The returned function gets wrapped by SecureValidatorFunc, so calling SecureValidatorFunc on the function is not required.
func SecureValidatorFunc ¶
func SecureValidatorFunc(f ValidatorFunc) ValidatorFunc
SecureValidatorFunc wraps a call to f(password) and recovers from any panic that might occur, it's advised to always use SecureValidatorFunc if dealing with raw validator functions. But validator functions retrieved from GuessValidatorFunc are already wrapped by SecureValidatorFunc. f is not allowed to be nil.
type VersionError ¶
VersionError is an error returned if the version of a hashes version is not compatible with the library.
func NewVersionError ¶
func NewVersionError(prefix, expected, got string) VersionError
NewVersionError returns a new VersionError. Prefix is appended to the error message, expected and got describe the the version execpted resp. found.
func (VersionError) Error ¶
func (err VersionError) Error() string
Error returns the string representation of the error.
func (VersionError) String ¶
func (err VersionError) String() string
String returns the string representation of the error.