summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-10-17 20:10:49 -0300
committerEuAndreh <eu@euandre.org>2024-10-17 20:10:49 -0300
commit8c9933b3a4041a2e280f966d3a73f63e8b7d0e87 (patch)
treeec9abf41465fb10520c22140304a2a993ac4514b
parenttests/scrypt.go: Depend on gobang (diff)
downloadscrypt-8c9933b3a4041a2e280f966d3a73f63e8b7d0e87.tar.gz
scrypt-8c9933b3a4041a2e280f966d3a73f63e8b7d0e87.tar.xz
Remove panicky code
-rw-r--r--src/scrypt.go46
-rw-r--r--tests/libbuild.go16
-rw-r--r--tests/scrypt.go23
3 files changed, 47 insertions, 38 deletions
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))
}
diff --git a/tests/libbuild.go b/tests/libbuild.go
index 724d17a..31e93c9 100644
--- a/tests/libbuild.go
+++ b/tests/libbuild.go
@@ -2,13 +2,21 @@ package main
import (
"encoding/hex"
- "fmt"
+
+ g "gobang"
"scrypt"
)
func main() {
- fmt.Println(
- hex.EncodeToString(scrypt.Hash(scrypt.Salt(), scrypt.Salt())),
- )
+ s1, err := scrypt.Salt()
+ g.TErrorIf(err)
+
+ s2, err := scrypt.Salt()
+ g.TErrorIf(err)
+
+ hash, err := scrypt.Hash(s1, s2)
+ g.TErrorIf(err)
+
+ println(hex.EncodeToString(hash))
}
diff --git a/tests/scrypt.go b/tests/scrypt.go
index 16ea570..cb97b08 100644
--- a/tests/scrypt.go
+++ b/tests/scrypt.go
@@ -4,7 +4,7 @@ import (
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
- "encoding/hex"
+ // "encoding/hex"
"hash"
g "gobang"
@@ -353,6 +353,9 @@ func test_scrypt() {
func test_SaltFrom() {
g.TestStart("SaltFrom()")
+
+ g.Testing("error when reader errors", func() {
+ })
// FIXME
}
@@ -369,6 +372,7 @@ func test_CheckFrom() {
func test_Salt() {
g.TestStart("Salt()")
+ /*
g.Testing("we generate a random salt of a fixed size", func() {
salt := Salt()
g.TAssertEqual(len(salt), saltMinLength)
@@ -377,16 +381,25 @@ func test_Salt() {
hex.Encode(buffer[:], salt)
// FIXME
})
+ */
}
func test_Hash() {
g.TestStart("Hash()")
g.Testing("same input, same output", func() {
- password := Salt()
- salt := Salt()
- hash1 := Hash(password, salt)
- hash2 := Hash(password, salt)
+ password, err := Salt()
+ g.TErrorIf(err)
+
+ salt, err := Salt()
+ g.TErrorIf(err)
+
+ hash1, err := Hash(password, salt)
+ g.TErrorIf(err)
+
+ hash2, err := Hash(password, salt)
+ g.TErrorIf(err)
+
g.TAssertEqual(hash1, hash2)
})
}