Skip to content

Commit 9fe8f67

Browse files
committed
generate random string
1 parent aa307cf commit 9fe8f67

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

strings.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package hermes
22

3-
import "strings"
3+
import (
4+
"crypto/rand"
5+
"encoding/base64"
6+
"strings"
7+
)
48

59
func IsStringEmpty(s string) bool { return len(strings.TrimSpace(s)) == 0 }
10+
11+
func Random(length int) (string, error) {
12+
bytes := make([]byte, length)
13+
_, err := rand.Read(bytes)
14+
if err != nil {
15+
return "", err
16+
}
17+
18+
return base64.RawURLEncoding.EncodeToString(bytes), nil
19+
}

0 commit comments

Comments
 (0)