Documentation ¶
Overview ¶
Package bcryptx automates the tuning of bcrypt costs based on an environment's available processing resources. Concurrency throttling is provided, as well as convenience functions for making use of tuned costs with bcrypt functions.
quickCost should be used when a hash should be accessible quickly. strongCost should be used when the delay of processing can be mitigated.
Example ¶
package main import ( "fmt" "time" "github.com/codemodus/bcryptx" ) func main() { bcxOpts := &bcryptx.Options{ GenQuickMaxTime: time.Millisecond * 400, // default is 500 GenStrongMaxTime: time.Millisecond * 1600, // default is 2000 GenConcurrency: 1, // default is 2 } // To use defaults, provide nil instead of a bcryptx.Options object. bcx := bcryptx.New(bcxOpts) if err := bcx.Tune(); err != nil { fmt.Println(err) } hash, err := bcx.GenQuickFromPass("12345") if err != nil { fmt.Println(err) } if err := bcx.CompareHashAndPass(hash, "spaceballs"); err != nil { fmt.Println(`Generated hash for "12345", tested for "spaceballs".`) } if ok := bcx.IsCostStrong(hash); !ok { fmt.Println("Hashed quick, wanted strong.") } }
Output: Generated hash for "12345", tested for "spaceballs". Hashed quick, wanted strong.
Index ¶
- Constants
- type Bcrypter
- func (bc *Bcrypter) CompareHashAndPass(hash, pass string) error
- func (bc *Bcrypter) CurrentQuickCost() (int, error)
- func (bc *Bcrypter) CurrentStrongCost() (int, error)
- func (bc *Bcrypter) GenQuickFromPass(pass string) (string, error)
- func (bc *Bcrypter) GenStrongFromPass(pass string) (string, error)
- func (bc *Bcrypter) IsCostQuick(hash string) bool
- func (bc *Bcrypter) IsCostStrong(hash string) bool
- func (bc *Bcrypter) Tune() error
- func (bc *Bcrypter) ValidateHash(hash string) error
- type Options
Examples ¶
Constants ¶
const ( // GenQuickMaxTime is the default max time used for tuning // Bcrypter.quickCost. GenQuickMaxTime = time.Millisecond * 500 // GenStrongMaxTime is the default max time used for tuning // Bcrypter.strongCost. GenStrongMaxTime = time.Millisecond * 2000 // GenConcurrency is the default goroutine count used for Gen*FromPass. GenConcurrency = 2 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bcrypter ¶
type Bcrypter struct {
// contains filtered or unexported fields
}
Bcrypter provides an API for bcrypt functions with "quick" or "strong" costs. Tune is called on first use of Gen*FromPass if not already called directly.
func (*Bcrypter) CompareHashAndPass ¶
CompareHashAndPass returns an error if comparison fails or any error encountered during handling.
func (*Bcrypter) CurrentQuickCost ¶
CurrentQuickCost returns the quickCost as set by Tune.
func (*Bcrypter) CurrentStrongCost ¶
CurrentStrongCost returns the strongCost as set by Tune.
func (*Bcrypter) GenQuickFromPass ¶
GenQuickFromPass returns a hash produced using Bcrypter.quickCost or any error encountered during handling.
func (*Bcrypter) GenStrongFromPass ¶
GenStrongFromPass returns a hash produced using Bcrypter.strongCost or any error encountered during handling.
func (*Bcrypter) IsCostQuick ¶
IsCostQuick returns false if the apparent cost of the hash is lower than the provided cost, or if any errors are encountered during hash analysis.
func (*Bcrypter) IsCostStrong ¶
IsCostStrong returns false if the apparent cost of the hash is lower than the provided cost, or if any errors are encountered during hash analysis.
func (*Bcrypter) Tune ¶
Tune sets the quick and strong costs based on provided max times. Appropriate costs are determined by producing a handful of low-cost hashes, then using the resulting durations to interpolate the durations of hashes with higher costs.
func (*Bcrypter) ValidateHash ¶
ValidateHash returns an error if the provided argument is not a valid hash.
type Options ¶
type Options struct { // GenQuickMaxTime is the max time used for tuning Bcrypter.quickCost. GenQuickMaxTime time.Duration // GenStrongMaxTime is the max time used for tuning Bcrypter.strongCost. GenStrongMaxTime time.Duration // GenConcurrency is the goroutine count used for Gen*FromPass. GenConcurrency int }
Options holds values to be passed to New.