diff options
Diffstat (limited to 'internal/userutil')
-rw-r--r-- | internal/userutil/userutil.go | 7 | ||||
-rw-r--r-- | internal/userutil/userutil_test.go | 8 |
2 files changed, 15 insertions, 0 deletions
diff --git a/internal/userutil/userutil.go b/internal/userutil/userutil.go index 6d324ff6..bafdaf58 100644 --- a/internal/userutil/userutil.go +++ b/internal/userutil/userutil.go @@ -23,6 +23,7 @@ import ( "gogs.io/gogs/internal/avatar" "gogs.io/gogs/internal/conf" + "gogs.io/gogs/internal/strutil" "gogs.io/gogs/internal/tool" ) @@ -132,3 +133,9 @@ func MailResendCacheKey(userID int64) string { func TwoFactorCacheKey(userID int64, passcode string) string { return fmt.Sprintf("twoFactor::%d::%s", userID, passcode) } + +// RandomSalt returns randomly generated 10-character string that can be used as +// the user salt. +func RandomSalt() (string, error) { + return strutil.RandomChars(10) +} diff --git a/internal/userutil/userutil_test.go b/internal/userutil/userutil_test.go index e46546bd..6cdd6dd3 100644 --- a/internal/userutil/userutil_test.go +++ b/internal/userutil/userutil_test.go @@ -191,3 +191,11 @@ func TestTwoFactorCacheKey(t *testing.T) { got := TwoFactorCacheKey(1, "113654") assert.Equal(t, "twoFactor::1::113654", got) } + +func TestRandomSalt(t *testing.T) { + salt1, err := RandomSalt() + require.NoError(t, err) + salt2, err := RandomSalt() + require.NoError(t, err) + assert.NotEqual(t, salt1, salt2) +} |