mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-09-19 01:58:00 +08:00
feat:实现项目集功能
This commit is contained in:
@@ -13,6 +13,8 @@ import (
|
||||
"crypto/rand"
|
||||
)
|
||||
|
||||
var stdChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
|
||||
|
||||
//对称加密与解密之加密【从Beego中提取出来的】
|
||||
//@param value 需要加密的字符串
|
||||
//@param secret 加密密钥
|
||||
@@ -71,7 +73,6 @@ func Sha1Crypt(str string, salt ...interface{}) (CryptStr string) {
|
||||
return fmt.Sprintf("%x", sha1.Sum([]byte(str)))
|
||||
}
|
||||
|
||||
|
||||
//生成Guid字串
|
||||
func UniqueId() string {
|
||||
b := make([]byte, 48)
|
||||
@@ -80,4 +81,35 @@ func UniqueId() string {
|
||||
return ""
|
||||
}
|
||||
return Md5Crypt(base64.URLEncoding.EncodeToString(b))
|
||||
}
|
||||
}
|
||||
|
||||
//生成指定长度的字符串.
|
||||
func NewRandChars(length int) string {
|
||||
if length == 0 {
|
||||
return ""
|
||||
}
|
||||
clen := len(stdChars)
|
||||
if clen < 2 || clen > 256 {
|
||||
panic("Wrong charset length for NewLenChars()")
|
||||
}
|
||||
maxrb := 255 - (256 % clen)
|
||||
b := make([]byte, length)
|
||||
r := make([]byte, length+(length/4)) // storage for random bytes.
|
||||
i := 0
|
||||
for {
|
||||
if _, err := rand.Read(r); err != nil {
|
||||
panic("Error reading random bytes: " + err.Error())
|
||||
}
|
||||
for _, rb := range r {
|
||||
c := int(rb)
|
||||
if c > maxrb {
|
||||
continue // Skip this number to avoid modulo bias.
|
||||
}
|
||||
b[i] = stdChars[c%clen]
|
||||
i++
|
||||
if i == length {
|
||||
return string(b)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user