Initial Codebase (untested)

This commit is contained in:
James Mills
2021-01-30 14:05:04 +10:00
parent c1dc91b7e0
commit 4529ea3196
60 changed files with 9807 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
package passwords
// Passwords is an interface for creating and verifying secure passwords
// An implementation must implement all methods and it is up to the impl
// which underlying crypto to use for hasing cleartext passwrods.
type Passwords interface {
CreatePassword(password string) (string, error)
CheckPassword(hash, password string) error
}

View File

@@ -0,0 +1,72 @@
package passwords
import (
"time"
scrypt "github.com/elithrar/simple-scrypt"
log "github.com/sirupsen/logrus"
)
const (
// DefaultMaxTimeout default max timeout in ms
DefaultMaxTimeout = 500 * time.Millisecond
// DefaultMaxMemory default max memory in MB
DefaultMaxMemory = 64
)
// Options ...
type Options struct {
maxTimeout time.Duration
maxMemory int
}
// NewOptions ...
func NewOptions(maxTimeout time.Duration, maxMemory int) *Options {
return &Options{maxTimeout, maxMemory}
}
// ScryptPasswords ...
type ScryptPasswords struct {
options *Options
params scrypt.Params
}
// NewScryptPasswords ...
func NewScryptPasswords(options *Options) Passwords {
if options == nil {
options = &Options{}
}
if options.maxTimeout == 0 {
options.maxTimeout = DefaultMaxTimeout
}
if options.maxMemory == 0 {
options.maxMemory = DefaultMaxMemory
}
log.Info("Calibrating scrypt parameters ...")
params, err := scrypt.Calibrate(
options.maxTimeout,
options.maxMemory,
scrypt.DefaultParams,
)
if err != nil {
log.Fatalf("error calibrating scrypt params: %s", err)
}
log.WithField("params", params).Info("scrypt params")
return &ScryptPasswords{options, params}
}
// CreatePassword ...
func (sp *ScryptPasswords) CreatePassword(password string) (string, error) {
hash, err := scrypt.GenerateFromPassword([]byte(password), sp.params)
return string(hash), err
}
// CheckPassword ...
func (sp *ScryptPasswords) CheckPassword(hash, password string) error {
return scrypt.CompareHashAndPassword([]byte(hash), []byte(password))
}