From 8c9933b3a4041a2e280f966d3a73f63e8b7d0e87 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Thu, 17 Oct 2024 20:10:49 -0300 Subject: Remove panicky code --- src/scrypt.go | 46 +++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 29 deletions(-) (limited to 'src/scrypt.go') diff --git a/src/scrypt.go b/src/scrypt.go index 8451467..fe6cb8d 100644 --- a/src/scrypt.go +++ b/src/scrypt.go @@ -352,7 +352,11 @@ func SaltFrom(r io.Reader) ([]byte, error) { return buffer, nil } -func HashFrom(password []byte, salt []byte) ([]byte, error) { +func Salt() ([]byte, error) { + return SaltFrom(rand.Reader) +} + +func Hash(password []byte, salt []byte) ([]byte, error) { if len(salt) < saltMinLength { return nil, ErrSaltTooSmall } @@ -372,8 +376,8 @@ func HashFrom(password []byte, salt []byte) ([]byte, error) { return hash, nil } -func CheckFrom(password []byte, salt []byte, hash []byte) (bool, error) { - candidate, err := HashFrom(password, salt) +func Check(password []byte, salt []byte, hash []byte) (bool, error) { + candidate, err := Hash(password, salt) if err != nil { return false, err } @@ -381,29 +385,6 @@ func CheckFrom(password []byte, salt []byte, hash []byte) (bool, error) { return slices.Equal(candidate, hash), nil } -func Salt() []byte { - salt, err := SaltFrom(rand.Reader) - if err != nil { - panic(err) - } - return salt -} - -func Hash(password []byte, salt []byte) []byte { - hash, err := HashFrom(password, salt) - if err != nil { - panic(err) - } - return hash -} - -func Check(password []byte, salt []byte, hash []byte) bool { - check, err := CheckFrom(password, salt, hash) - if err != nil { - panic(err) - } - return check -} func Main() { @@ -412,7 +393,14 @@ func Main() { os.Exit(2) } - fmt.Println(hex.EncodeToString( - Hash([]byte(os.Args[1]), []byte(os.Args[2]))), - ) + payload, err := Hash([]byte(os.Args[1]), []byte(os.Args[2])) + if err != nil { + if err == ErrSaltTooSmall { + fmt.Fprintln(os.Stderr, err) + os.Exit(2) + } + panic(err) + } + + fmt.Println(hex.EncodeToString(payload)) } -- cgit v1.2.3