diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index f83ff4e..0000000 --- a/.travis.yml +++ /dev/null @@ -1,23 +0,0 @@ -language: go -sudo: false - -matrix: - include: - - go: 1.5.x - - go: 1.6.x - - go: 1.7.x - - go: 1.8.x - - go: 1.9.x - - go: 1.10.x - - go: tip - allow_failures: - - go: tip - -install: - - # Skip - -script: - - go get -t -v ./... - - diff -u <(echo -n) <(gofmt -d .) - - go tool vet . - - go test -v ./... diff --git a/README.md b/README.md index 4a093e1..45a383f 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ # simple-scrypt -[![GoDoc](https://godoc.org/github.com/elithrar/simple-scrypt?status.svg)](https://godoc.org/github.com/elithrar/simple-scrypt) [![Build Status](https://travis-ci.org/elithrar/simple-scrypt.svg?branch=master)](https://travis-ci.org/elithrar/simple-scrypt) +[![GoDoc](https://godoc.org/github.com/elithrar/simple-scrypt?status.svg)](https://godoc.org/github.com/elithrar/simple-scrypt) simple-scrypt provides a convenience wrapper around Go's existing [scrypt](http://golang.org/x/crypto/scrypt) package that makes it easier to securely derive strong keys ("hash user passwords"). This library allows you to: -* Generate a scrypt derived key with a crytographically secure salt and sane +* Generate a scrypt derived key with a cryptographically secure salt and sane default parameters for N, r and p. * Upgrade the parameters used to generate keys as hardware improves by storing them with the derived key (the scrypt spec. doesn't allow for this by @@ -28,7 +28,7 @@ go get -u github.com/elithrar/simple-scrypt simple-scrypt doesn't try to re-invent the wheel or do anything "special". It wraps the `scrypt.Key` function as thinly as possible, generates a -crytographically secure salt for you using Go's `crypto/rand` package, and +cryptographically secure salt for you using Go's `crypto/rand` package, and returns the derived key with the parameters prepended: ```go @@ -47,7 +47,7 @@ func main() { // Generates a derived key of the form "N$r$p$salt$dk" where N, r and p are defined as per // Colin Percival's scrypt paper: http://www.tarsnap.com/scrypt/scrypt.pdf - // scrypt.Defaults (N=16384, r=8, p=1) makes it easy to provide these parameters, and + // scrypt.DefaultParams (N=16384, r=8, p=1) makes it easy to provide these parameters, and // (should you wish) provide your own values via the scrypt.Params type. hash, err := scrypt.GenerateFromPassword([]byte(passwordFromForm), scrypt.DefaultParams) if err != nil { @@ -58,7 +58,7 @@ func main() { fmt.Printf("%s\n", hash) // Uses the parameters from the existing derived key. Return an error if they don't match. - err := scrypt.CompareHashAndPassword(hash, []byte(passwordFromForm)) + err = scrypt.CompareHashAndPassword(hash, []byte(passwordFromForm)) if err != nil { log.Fatal(err) } @@ -86,7 +86,7 @@ func main() { log.Fatal(err) } - // Now to check them against our own Params struct (e.g. using reflect.DeepEquals) + // Now to check them against our own Params struct (e.g. using reflect.DeepEqual) // and determine whether we want to generate a new key with our "upgraded" parameters. slower := scrypt.Params{ N: 32768, @@ -115,9 +115,9 @@ var params scrypt.Params func main() { var err error // 500ms, 64MB of RAM per hash. - params, err = scrypt.Calibrate(500*time.Millisecond, 64, Params{}) + params, err = scrypt.Calibrate(500*time.Millisecond, 64, scrypt.Params{}) if err != nil { - return nil, err + log.Fatal(err) } ... diff --git a/coverage.out b/coverage.out new file mode 100644 index 0000000..c6c67be --- /dev/null +++ b/coverage.out @@ -0,0 +1,73 @@ +mode: atomic +github.com/elithrar/simple-scrypt/scrypt.go:61.49,65.16 3 38 +github.com/elithrar/simple-scrypt/scrypt.go:65.16,67.3 1 0 +github.com/elithrar/simple-scrypt/scrypt.go:69.2,69.15 1 38 +github.com/elithrar/simple-scrypt/scrypt.go:77.75,79.16 2 23 +github.com/elithrar/simple-scrypt/scrypt.go:79.16,81.3 1 0 +github.com/elithrar/simple-scrypt/scrypt.go:83.2,83.39 1 23 +github.com/elithrar/simple-scrypt/scrypt.go:83.39,85.3 1 9 +github.com/elithrar/simple-scrypt/scrypt.go:88.2,89.16 2 14 +github.com/elithrar/simple-scrypt/scrypt.go:89.16,91.3 1 0 +github.com/elithrar/simple-scrypt/scrypt.go:95.2,95.91 1 14 +github.com/elithrar/simple-scrypt/scrypt.go:102.65,105.16 2 3 +github.com/elithrar/simple-scrypt/scrypt.go:105.16,107.3 1 1 +github.com/elithrar/simple-scrypt/scrypt.go:110.2,111.16 2 2 +github.com/elithrar/simple-scrypt/scrypt.go:111.16,113.3 1 0 +github.com/elithrar/simple-scrypt/scrypt.go:116.2,116.48 1 2 +github.com/elithrar/simple-scrypt/scrypt.go:116.48,118.3 1 1 +github.com/elithrar/simple-scrypt/scrypt.go:120.2,120.37 1 1 +github.com/elithrar/simple-scrypt/scrypt.go:125.32,127.44 1 35 +github.com/elithrar/simple-scrypt/scrypt.go:127.44,129.3 1 5 +github.com/elithrar/simple-scrypt/scrypt.go:132.2,132.29 1 30 +github.com/elithrar/simple-scrypt/scrypt.go:132.29,134.3 1 1 +github.com/elithrar/simple-scrypt/scrypt.go:137.2,137.29 1 29 +github.com/elithrar/simple-scrypt/scrypt.go:137.29,139.3 1 1 +github.com/elithrar/simple-scrypt/scrypt.go:143.2,143.106 1 28 +github.com/elithrar/simple-scrypt/scrypt.go:143.106,145.3 1 1 +github.com/elithrar/simple-scrypt/scrypt.go:148.2,148.50 1 27 +github.com/elithrar/simple-scrypt/scrypt.go:148.50,150.3 1 1 +github.com/elithrar/simple-scrypt/scrypt.go:153.2,153.44 1 26 +github.com/elithrar/simple-scrypt/scrypt.go:153.44,155.3 1 1 +github.com/elithrar/simple-scrypt/scrypt.go:157.2,157.12 1 25 +github.com/elithrar/simple-scrypt/scrypt.go:163.62,167.20 2 11 +github.com/elithrar/simple-scrypt/scrypt.go:167.20,169.3 1 2 +github.com/elithrar/simple-scrypt/scrypt.go:171.2,175.16 4 9 +github.com/elithrar/simple-scrypt/scrypt.go:175.16,177.3 1 1 +github.com/elithrar/simple-scrypt/scrypt.go:179.2,180.16 2 8 +github.com/elithrar/simple-scrypt/scrypt.go:180.16,182.3 1 1 +github.com/elithrar/simple-scrypt/scrypt.go:184.2,185.16 2 7 +github.com/elithrar/simple-scrypt/scrypt.go:185.16,187.3 1 1 +github.com/elithrar/simple-scrypt/scrypt.go:189.2,190.16 2 6 +github.com/elithrar/simple-scrypt/scrypt.go:190.16,192.3 1 1 +github.com/elithrar/simple-scrypt/scrypt.go:193.2,196.16 3 5 +github.com/elithrar/simple-scrypt/scrypt.go:196.16,198.3 1 1 +github.com/elithrar/simple-scrypt/scrypt.go:199.2,201.39 2 4 +github.com/elithrar/simple-scrypt/scrypt.go:201.39,203.3 1 1 +github.com/elithrar/simple-scrypt/scrypt.go:205.2,205.30 1 3 +github.com/elithrar/simple-scrypt/scrypt.go:211.40,215.2 2 8 +github.com/elithrar/simple-scrypt/scrypt.go:225.86,227.72 2 8 +github.com/elithrar/simple-scrypt/scrypt.go:227.72,229.3 1 8 +github.com/elithrar/simple-scrypt/scrypt.go:229.8,229.41 1 0 +github.com/elithrar/simple-scrypt/scrypt.go:229.41,231.3 1 0 +github.com/elithrar/simple-scrypt/scrypt.go:232.2,232.18 1 8 +github.com/elithrar/simple-scrypt/scrypt.go:232.18,234.3 1 0 +github.com/elithrar/simple-scrypt/scrypt.go:235.2,235.21 1 8 +github.com/elithrar/simple-scrypt/scrypt.go:235.21,237.3 1 0 +github.com/elithrar/simple-scrypt/scrypt.go:238.2,239.16 2 8 +github.com/elithrar/simple-scrypt/scrypt.go:239.16,241.3 1 0 +github.com/elithrar/simple-scrypt/scrypt.go:242.2,263.50 6 8 +github.com/elithrar/simple-scrypt/scrypt.go:263.50,265.3 1 122 +github.com/elithrar/simple-scrypt/scrypt.go:266.2,270.78 3 8 +github.com/elithrar/simple-scrypt/scrypt.go:270.78,272.3 1 0 +github.com/elithrar/simple-scrypt/scrypt.go:273.2,276.20 2 8 +github.com/elithrar/simple-scrypt/scrypt.go:276.20,280.79 3 10 +github.com/elithrar/simple-scrypt/scrypt.go:280.79,282.4 1 0 +github.com/elithrar/simple-scrypt/scrypt.go:283.3,283.26 1 10 +github.com/elithrar/simple-scrypt/scrypt.go:288.2,288.20 1 8 +github.com/elithrar/simple-scrypt/scrypt.go:288.20,292.23 2 23 +github.com/elithrar/simple-scrypt/scrypt.go:292.23,295.4 1 12 +github.com/elithrar/simple-scrypt/scrypt.go:295.9,297.4 1 11 +github.com/elithrar/simple-scrypt/scrypt.go:299.3,300.79 2 23 +github.com/elithrar/simple-scrypt/scrypt.go:300.79,302.4 1 0 +github.com/elithrar/simple-scrypt/scrypt.go:303.3,303.26 1 23 +github.com/elithrar/simple-scrypt/scrypt.go:306.2,308.21 2 8 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..523e847 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/elithrar/simple-scrypt + +go 1.20 + +require golang.org/x/crypto v0.31.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9615005 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= +golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= diff --git a/scrypt.go b/scrypt.go index 2742ea0..170f2b9 100644 --- a/scrypt.go +++ b/scrypt.go @@ -38,7 +38,7 @@ type Params struct { // DefaultParams provides sensible default inputs into the scrypt function // for interactive use (i.e. web applications). -// These defaults will consume approxmiately 16MB of memory (128 * r * N). +// These defaults will consume approximately 16MB of memory (128 * r * N). // The default key length is 256 bits. var DefaultParams = Params{N: 16384, R: 8, P: 1, SaltLen: 16, DKLen: 32} @@ -163,7 +163,7 @@ func (p *Params) Check() error { func decodeHash(hash []byte) (Params, []byte, []byte, error) { vals := strings.Split(string(hash), "$") - // P, N, R, salt, scrypt derived key + // N, R, P, salt, scrypt derived key if len(vals) != 5 { return Params{}, nil, nil, ErrInvalidHash } @@ -218,10 +218,9 @@ func Cost(hash []byte) (Params, error) { // The returned params will not use more memory than the given (MiB); // will not take more time than the given timeout, but more than timeout/2. // -// -// The default timeout (when the timeout arg is zero) is 200ms. -// The default memMiBytes (when memMiBytes is zero) is 16MiB. -// The default parameters (when params == Params{}) is DefaultParams. +// The default timeout (when the timeout arg is zero) is 200ms. +// The default memMiBytes (when memMiBytes is zero) is 16MiB. +// The default parameters (when params == Params{}) is DefaultParams. func Calibrate(timeout time.Duration, memMiBytes int, params Params) (Params, error) { p := params if p.N == 0 || p.R == 0 || p.P == 0 || p.SaltLen == 0 || p.DKLen == 0 { @@ -272,7 +271,7 @@ func Calibrate(timeout time.Duration, memMiBytes int, params Params) (Params, er } dur := time.Since(start) - // reduce N if scrypt takes to long + // reduce N if scrypt takes too long for dur > timeout { p.N >>= 1