diff options
author | EuAndreh <eu@euandre.org> | 2024-10-19 09:07:14 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-10-19 09:07:14 -0300 |
commit | e37c1fc7fe6b211c05f9124653c9b338e587ca8a (patch) | |
tree | 4b73a455eafe03646403e4ae84dab50ce53bb834 /src/scrypt.go | |
parent | Remove Go code in favor of upstream libscrypt-kdf (diff) | |
download | scrypt-e37c1fc7fe6b211c05f9124653c9b338e587ca8a.tar.gz scrypt-e37c1fc7fe6b211c05f9124653c9b338e587ca8a.tar.xz |
src/scrypt.go: Create HashInput and CheckInput for named arguments
Diffstat (limited to 'src/scrypt.go')
-rw-r--r-- | src/scrypt.go | 49 |
1 files changed, 34 insertions, 15 deletions
diff --git a/src/scrypt.go b/src/scrypt.go index a4f03d2..70140ab 100644 --- a/src/scrypt.go +++ b/src/scrypt.go @@ -38,6 +38,19 @@ var ( +type HashInput struct{ + Password []byte + Salt []byte +} + +type CheckInput struct{ + Password []byte + Salt []byte + Hash []byte +} + + + // Package scrypt implements the scrypt key derivation function as defined in // Colin Percival's paper "Stronger Key Derivation via Sequential Memory-Hard // Functions" (https://www.tarsnap.com/scrypt/scrypt.pdf). @@ -94,24 +107,20 @@ func scrypt( return out, nil } -func Hash(password []byte, salt []byte) ([]byte, error) { - if len(salt) < _SALT_MIN_LENGTH { +func Hash(input HashInput) ([]byte, error) { + if len(input.Salt) < _SALT_MIN_LENGTH { return nil, ErrSaltTooSmall } hash, err := scrypt( - password, - salt, + input.Password, + input.Salt, _N, r, p, _DESIRED_LENGTH, ) - if err != nil { - return nil, err - } - - return hash, nil + return hash, err } func SaltFrom(r io.Reader) ([]byte, error) { @@ -127,13 +136,18 @@ func Salt() ([]byte, error) { return SaltFrom(rand.Reader) } -func Check(password []byte, salt []byte, hash []byte) (bool, error) { - candidate, err := Hash(password, salt) +func Check(input CheckInput) (bool, error) { + hashInput := HashInput{ + Password: input.Password, + Salt: input.Salt, + } + + candidate, err := Hash(hashInput) if err != nil { return false, err } - return slices.Equal(candidate, hash), nil + return slices.Equal(candidate, input.Hash), nil } @@ -143,10 +157,15 @@ func Main() { fmt.Fprintf(os.Stderr, "Usage: scrypt PASSWORD SALT\n") os.Exit(2) } - password := os.Args[1] - salt := os.Args[2] - payload, err := Hash([]byte(password), []byte(salt)) + password := []byte(os.Args[1]) + salt := []byte(os.Args[2]) + input := HashInput{ + Password: password, + Salt: salt, + } + + payload, err := Hash(input) if err != nil { if err == ErrSaltTooSmall { fmt.Fprintln(os.Stderr, err) |