Skip to content

Commit 458c5dc

Browse files
committed
fixup! fix: user passwords cleanup
1 parent 44c04ec commit 458c5dc

File tree

3 files changed

+14
-50
lines changed

3 files changed

+14
-50
lines changed

coderd/userpassword/hashing_bench_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
)
1111

1212
var (
13-
salt = []byte(cryptorand.MustString(16))
14-
secret = []byte(cryptorand.MustString(24))
13+
salt = []byte(must(cryptorand.String(16)))
14+
secret = []byte(must(cryptorand.String(24)))
1515

1616
resBcrypt []byte
1717
resPbkdf2 []byte
@@ -60,3 +60,11 @@ func BenchmarkPbkdf2(b *testing.B) {
6060

6161
resPbkdf2 = r
6262
}
63+
64+
func must(s string, err error) string {
65+
if err != nil {
66+
panic(err)
67+
}
68+
69+
return s
70+
}

coderd/userpassword/userpassword.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import (
1313
"golang.org/x/crypto/pbkdf2"
1414
"golang.org/x/exp/slices"
1515
"golang.org/x/xerrors"
16-
17-
"github.com/coder/coder/cryptorand"
1816
)
1917

2018
var (
@@ -40,7 +38,7 @@ var (
4038

4139
// The simulated hash is used when trying to simulate password checks for
4240
// users that don't exist.
43-
simulatedHash, _ = Hash(cryptorand.MustString(10))
41+
simulatedHash, _ = Hash("hunter2")
4442
)
4543

4644
// Make password hashing much faster in tests.

cryptorand/strings.go

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"crypto/rand"
55
"encoding/binary"
66
"strings"
7-
8-
"golang.org/x/xerrors"
97
)
108

119
// Charsets
@@ -34,7 +32,7 @@ const (
3432
Human = "23456789abcdefghjkmnpqrstuvwxyz"
3533
)
3634

37-
// StringCharset generates a random string using the provided charset and size.
35+
// StringCharset generates a random string using the provided charset and size
3836
func StringCharset(charSetStr string, size int) (string, error) {
3937
charSet := []rune(charSetStr)
4038

@@ -69,58 +67,18 @@ func StringCharset(charSetStr string, size int) (string, error) {
6967
return buf.String(), nil
7068
}
7169

72-
// MustStringCharset generates a random string of the given length, using the
73-
// provided charset. It will panic if an error occurs.
74-
func MustStringCharset(charSet string, size int) string {
75-
s, err := StringCharset(charSet, size)
76-
must(err)
77-
return s
78-
}
79-
8070
// String returns a random string using Default.
8171
func String(size int) (string, error) {
8272
return StringCharset(Default, size)
8373
}
8474

85-
// MustString generates a random string of the given length, using
86-
// the Default charset. It will panic if an error occurs.
87-
func MustString(size int) string {
88-
s, err := String(size)
89-
must(err)
90-
return s
91-
}
92-
9375
// HexString returns a hexadecimal string of given length.
9476
func HexString(size int) (string, error) {
9577
return StringCharset(Hex, size)
9678
}
9779

98-
// MustHexString generates a random hexadecimal string of the given
99-
// length. It will panic if an error occurs.
100-
func MustHexString(size int) string {
101-
s, err := HexString(size)
102-
must(err)
103-
return s
104-
}
105-
106-
// Sha1String returns a 40-character hexadecimal string, which matches the
107-
// length of a SHA-1 hash (160 bits).
80+
// Sha1String returns a 40-character hexadecimal string, which matches
81+
// the length of a SHA-1 hash (160 bits).
10882
func Sha1String() (string, error) {
10983
return StringCharset(Hex, 40)
11084
}
111-
112-
// MustSha1String returns a 40-character hexadecimal string, which matches the
113-
// length of a SHA-1 hash (160 bits). It will panic if an error occurs.
114-
func MustSha1String() string {
115-
s, err := Sha1String()
116-
must(err)
117-
return s
118-
}
119-
120-
// must is a utility function that panics with the given error if
121-
// err is non-nil.
122-
func must(err error) {
123-
if err != nil {
124-
panic(xerrors.Errorf("crand: %w", err))
125-
}
126-
}

0 commit comments

Comments
 (0)