diff options
author | ᴜɴᴋɴᴡᴏɴ <u@gogs.io> | 2020-04-14 09:41:54 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-14 09:41:54 +0800 |
commit | cb439a126aa6a2728e423bcfd0d5e948337b8ddb (patch) | |
tree | f7d09181fe5b96ea444f7544091673b3c668b9fe /internal/tool/tool.go | |
parent | 659acd48b1a131476fd98a54604fa6416b1cef9d (diff) |
db: add tests for two factors (#6099)
* Rename to TwoFactors.Create
* Use GORM to execute queries
* TwoFactor.GetByUserID
* Add tests
* Fix failing tests
* Add MD5 tests
* Add tests for RandomChars
Diffstat (limited to 'internal/tool/tool.go')
-rw-r--r-- | internal/tool/tool.go | 52 |
1 files changed, 1 insertions, 51 deletions
diff --git a/internal/tool/tool.go b/internal/tool/tool.go index eeed80e6..e5825018 100644 --- a/internal/tool/tool.go +++ b/internal/tool/tool.go @@ -6,13 +6,11 @@ package tool import ( "crypto/md5" - "crypto/rand" "crypto/sha1" "encoding/base64" "encoding/hex" "fmt" "html/template" - "math/big" "strings" "time" "unicode" @@ -27,22 +25,8 @@ import ( "gogs.io/gogs/internal/conf" ) -// MD5Bytes encodes string to MD5 bytes. -// TODO: Move to hashutil.MD5Bytes. -func MD5Bytes(str string) []byte { - m := md5.New() - _, _ = m.Write([]byte(str)) - return m.Sum(nil) -} - -// MD5 encodes string to MD5 hex value. -// TODO: Move to hashutil.MD5. -func MD5(str string) string { - return hex.EncodeToString(MD5Bytes(str)) -} - // SHA1 encodes string to SHA1 hex value. -// TODO: Move to hashutil.SHA1. +// TODO: Move to cryptoutil.SHA1. func SHA1(str string) string { h := sha1.New() _, _ = h.Write([]byte(str)) @@ -86,40 +70,6 @@ func BasicAuthDecode(encoded string) (string, string, error) { return auth[0], auth[1], nil } -// BasicAuthEncode encodes username and password in HTTP Basic Authentication format. -func BasicAuthEncode(username, password string) string { - return base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) -} - -const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" - -// RandomString returns generated random string in given length of characters. -// It also returns possible error during generation. -func RandomString(n int) (string, error) { - buffer := make([]byte, n) - max := big.NewInt(int64(len(alphanum))) - - for i := 0; i < n; i++ { - index, err := randomInt(max) - if err != nil { - return "", err - } - - buffer[i] = alphanum[index] - } - - return string(buffer), nil -} - -func randomInt(max *big.Int) (int, error) { - rand, err := rand.Int(rand.Reader, max) - if err != nil { - return 0, err - } - - return int(rand.Int64()), nil -} - // verify time limit code func VerifyTimeLimitCode(data string, minutes int, code string) bool { if len(code) <= 18 { |