summaryrefslogtreecommitdiffstats
path: root/modules/util
diff options
context:
space:
mode:
authorsilverwind <me@silverwind.io>2021-05-10 08:45:17 +0200
committerGitHub <noreply@github.com>2021-05-10 07:45:17 +0100
commit1e6fa57acbe3c05cb996b789e8c2d381c953826f (patch)
treec4f1ce55b3423f97952b630462cef5b2035961ec /modules/util
parent270aab429ef025df9a0b9bf9e3982729ae8df449 (diff)
downloadgitea-1e6fa57acbe3c05cb996b789e8c2d381c953826f.tar.gz
gitea-1e6fa57acbe3c05cb996b789e8c2d381c953826f.zip
Use single shared random string generation function (#15741)
* Use single shared random string generation function - Replace 3 functions that do the same with 1 shared one - Use crypto/rand over math/rand for a stronger RNG - Output only alphanumerical for URL compatibilty Fixes: #15536 * use const string method * Update modules/avatar/avatar.go Co-authored-by: a1012112796 <1012112796@qq.com> Co-authored-by: a1012112796 <1012112796@qq.com>
Diffstat (limited to 'modules/util')
-rw-r--r--modules/util/util.go27
-rw-r--r--modules/util/util_test.go38
2 files changed, 65 insertions, 0 deletions
diff --git a/modules/util/util.go b/modules/util/util.go
index 9de1710ac7..d26e6f13e4 100644
--- a/modules/util/util.go
+++ b/modules/util/util.go
@@ -6,7 +6,9 @@ package util
import (
"bytes"
+ "crypto/rand"
"errors"
+ "math/big"
"strings"
)
@@ -124,3 +126,28 @@ func MergeInto(dict map[string]interface{}, values ...interface{}) (map[string]i
return dict, nil
}
+
+// RandomInt returns a random integer between 0 and limit, inclusive
+func RandomInt(limit int64) (int64, error) {
+ int, err := rand.Int(rand.Reader, big.NewInt(limit))
+ if err != nil {
+ return 0, err
+ }
+ return int.Int64(), nil
+}
+
+const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+
+// RandomString generates a random alphanumerical string
+func RandomString(length int64) (string, error) {
+ bytes := make([]byte, length)
+ limit := int64(len(letters))
+ for i := range bytes {
+ num, err := RandomInt(limit)
+ if err != nil {
+ return "", err
+ }
+ bytes[i] = letters[num]
+ }
+ return string(bytes), nil
+}
diff --git a/modules/util/util_test.go b/modules/util/util_test.go
index 1d4f23de90..f82671787c 100644
--- a/modules/util/util_test.go
+++ b/modules/util/util_test.go
@@ -5,6 +5,7 @@
package util
import (
+ "regexp"
"strings"
"testing"
@@ -118,3 +119,40 @@ func Test_NormalizeEOL(t *testing.T) {
assert.Equal(t, []byte("mix\nand\nmatch\n."), NormalizeEOL([]byte("mix\r\nand\rmatch\n.")))
}
+
+func Test_RandomInt(t *testing.T) {
+ int, err := RandomInt(255)
+ assert.True(t, int >= 0)
+ assert.True(t, int <= 255)
+ assert.NoError(t, err)
+}
+
+func Test_RandomString(t *testing.T) {
+ str1, err := RandomString(32)
+ assert.NoError(t, err)
+ matches, err := regexp.MatchString(`^[a-zA-Z0-9]{32}$`, str1)
+ assert.NoError(t, err)
+ assert.True(t, matches)
+
+ str2, err := RandomString(32)
+ assert.NoError(t, err)
+ matches, err = regexp.MatchString(`^[a-zA-Z0-9]{32}$`, str1)
+ assert.NoError(t, err)
+ assert.True(t, matches)
+
+ assert.NotEqual(t, str1, str2)
+
+ str3, err := RandomString(256)
+ assert.NoError(t, err)
+ matches, err = regexp.MatchString(`^[a-zA-Z0-9]{256}$`, str3)
+ assert.NoError(t, err)
+ assert.True(t, matches)
+
+ str4, err := RandomString(256)
+ assert.NoError(t, err)
+ matches, err = regexp.MatchString(`^[a-zA-Z0-9]{256}$`, str4)
+ assert.NoError(t, err)
+ assert.True(t, matches)
+
+ assert.NotEqual(t, str3, str4)
+}