diff options
author | Unknown <joe2010xtmf@163.com> | 2014-03-22 13:50:58 -0400 |
---|---|---|
committer | Unknown <joe2010xtmf@163.com> | 2014-03-22 13:50:58 -0400 |
commit | e385efcc22665e9d84ffd7644abe58f30fe57e15 (patch) | |
tree | 21bb7fa3e16eeaac990aa5929505e015cebbee0a /modules/base/tool.go | |
parent | 61e29226015fad6451281035948c3d8d1364880c (diff) | |
parent | 5edd57e4822804aef2af64b1ede99cd6e977b143 (diff) |
Merge branch 'master' of github.com:gogits/gogs
Diffstat (limited to 'modules/base/tool.go')
-rw-r--r-- | modules/base/tool.go | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/modules/base/tool.go b/modules/base/tool.go index 4f368aa5..c7ee2ee8 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -25,13 +25,17 @@ func EncodeMd5(str string) string { return hex.EncodeToString(m.Sum(nil)) } -// Random generate string -func GetRandomString(n int) string { +// GetRandomString generate random string by specify chars. +func GetRandomString(n int, alphabets ...byte) string { const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" var bytes = make([]byte, n) rand.Read(bytes) for i, b := range bytes { - bytes[i] = alphanum[b%byte(len(alphanum))] + if len(alphabets) == 0 { + bytes[i] = alphanum[b%byte(len(alphanum))] + } else { + bytes[i] = alphabets[b%byte(len(alphabets))] + } } return string(bytes) } |