Documentation
¶
Overview ¶
Package passwd provides simple primitives for hashing and verifying password.
Index ¶
Examples ¶
Constants ¶
const ( // Argon2id constant is to select the argon flavor in Argon2Params version field Argon2id = iota // default // Argon2i constant is to select argon flavor in Argon2Params version field Argon2i )
const ( // ErrParse when a parse error happened ErrParse = Error("parse error") // ErrHash when a hashing error occurs ErrHash = Error("hash error") // ErrUnsupported when a feature is not supported ErrUnsupported = Error("unsupported") // ErrMismatch is returned when Compare() call does not match ErrMismatch = Error("mismatch") // ErrUnsafe is to notify of password hashing parameters strength ErrUnsafe = Error("unsafe parameters") )
const ( Argon2idDefault HashProfile = iota Argon2idParanoid ScryptDefault ScryptParanoid BcryptDefault BcryptParanoid Argon2Custom // value for custom ScryptCustom // value for custom BcryptCustom // value for custom Argon int = iota Scrypt Bcrypt )
Password hashing profiles available
Variables ¶
This section is empty.
Functions ¶
func Compare ¶
Compare verify a non-key'd & non-mask'd hash values against a plaintext password.
Example ¶
hashedPassword := []byte("$2id$aiOE.rPFUFkkehxc6utWY.$1$65536$8$32$Wv1IMP6xwaqVaQGOX6Oxe.eSEbozeRJLzln8ZlthZfS")
err := Compare(hashedPassword, []byte("prout"))
if err != nil {
fmt.Printf("err: %v\n", err)
}
fmt.Printf("compared")
Output: compared
Types ¶
type Argon2Params ¶
type Argon2Params struct {
Version int
Time uint32
Memory uint32
Saltlen uint32
Keylen uint32
Thread uint8
Masked bool // are parameters private
// contains filtered or unexported fields
}
Argon2Params are the parameters for the argon2 key derivation.
type BcryptParams ¶
BcryptParams are the parameters for the bcrypt key derivation.
type HashProfile ¶
type HashProfile int
HashProfile is the type that describes the exported profile type available in this package
type Profile ¶
type Profile struct {
// contains filtered or unexported fields
}
Profile define the hashing profile you have select and is created using New() / NewMasked() / NewCustom()
func New ¶
func New(profile HashProfile) (*Profile, error)
New instantiate a new Profile
Example ¶
p, err := New(Argon2idDefault)
if err != nil {
panic(err)
}
_, err = p.Hash([]byte("mylamepass!!"))
if err != nil {
panic(err)
}
fmt.Printf("argon hashed")
Output: argon hashed
func NewCustom ¶
NewCustom instanciates a new Profile using user defined hash parameters
Example ¶
customParams := Argon2Params{
Version: Argon2id,
Time: 1,
Memory: 32 * 1024,
Thread: 8,
Saltlen: 16,
Keylen: 32,
Masked: true,
}
p, err := NewCustom(&customParams)
if err != nil {
panic(err)
}
_, err = p.Hash([]byte("mylamepass!!"))
if err != nil {
panic(err)
}
fmt.Printf("custom argon hashed")
Output: custom argon hashed
func NewMasked ¶
func NewMasked(profile HashProfile) (*Profile, error)
NewMasked instanciates a new masked Profile. "masked" translate to the fact that no hash parameters will be provided in the resulting hash.
Example ¶
p, err := NewMasked(Argon2idDefault)
if err != nil {
panic(err)
}
_, err = p.Hash([]byte("mylamepass!!"))
if err != nil {
panic(err)
}
fmt.Printf("argon hashed")
Output: argon hashed
func (*Profile) Compare ¶
Compare method compared a computed hash against a plaintext password for the associated profile. This function is mainly here to allow to work with "masked" hashes where we don't provide the Hash parameters in the hashed values.
fixed / another code path might come if (for example): - profile is BcryptSomething - compared hash is $2id$salt$...
Example ¶
hashedPassword := []byte("$2id$PEKC7uef8j09Tx8WHKrpwu$e1ZINFSkJz4/hM0mytQCjrEmVRRb6VtkXwAZibZhgYm")
p, err := NewMasked(Argon2idDefault)
if err != nil {
panic(err)
}
err = p.Compare(hashedPassword, []byte("prout"))
if err != nil {
//panic(err)
fmt.Printf("err: %v\n", err)
}
fmt.Printf("compared")
Output: compared
func (*Profile) Derive ¶
Derive is the Profile's method for computing a cryptographic key usable with symmetric AEAD using the user provided Profile, password and salt it will return the derived key.
type ScryptParams ¶
type ScryptParams struct {
N uint32 // cpu memory cost must be > 1 && %2 == 0
R uint32 // parallelization cost param -> r*p < 2^30 (go implementation specific)
P uint32 // parallelization cost param -> r*p < 2^30 (go implementation specific)
Saltlen uint32 // 128 bits min.
Keylen uint32 // 128 bits min.
Masked bool // are parameters private
// contains filtered or unexported fields
}
ScryptParams are the parameters for the scrypt key derivation.