Skip to content

Commit 93dc7a3

Browse files
committed
token: Tweak token generation.
1 parent 053008f commit 93dc7a3

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

util/token/token.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ package token
33
import (
44
"crypto/rand"
55
"encoding/base32"
6-
"encoding/base64"
6+
"strings"
77
)
88

9+
var encoder = base32.NewEncoding("0123456789abcdefghjkmnpqrstvwxyz")
10+
911
func New64() string {
10-
b, err := RandN(8)
11-
if err != nil {
12-
panic(err)
13-
}
14-
return base64.StdEncoding.EncodeToString(b)
12+
return New(8)
1513
}
1614

1715
func RandN(n int) ([]byte, error) {
@@ -24,9 +22,13 @@ func RandN(n int) ([]byte, error) {
2422
}
2523

2624
func New32() string {
27-
b, err := RandN(5)
25+
return New(5)
26+
}
27+
28+
func New(n int) string {
29+
b, err := RandN(n)
2830
if err != nil {
2931
panic(err)
3032
}
31-
return base32.StdEncoding.EncodeToString(b)
33+
return strings.TrimRight(encoder.EncodeToString(b), "=")
3234
}

0 commit comments

Comments
 (0)