1 Star 0 Fork 0

vgos/plugin

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
string.go 1.83 KB
一键复制 编辑 原始数据 按行查看 历史
陈仁焕 提交于 2023-03-30 11:02 +08:00 . 添加
package util
import (
"crypto/md5"
"crypto/sha1"
"encoding/hex"
"math/rand"
"time"
"github.com/spf13/cast"
)
// Md5 md5 加密
func Md5(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
// Sha1 sha1 加密
func Sha1(str string) string {
h := sha1.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
// strType 000, 第一位代表int, 第二位代表 string_upper, 第三位代表 string_lower
const (
RandInt = 1 << iota
RandStringUpper
RandStringLower
RandStringSpecial
)
// RandStr
// @Description: 生成随机字符串
// @param args args[0] length, args[1]strtype
// @return string
func RandStr(args ...interface{}) string {
// 长度处理
if len(args) < 1 {
return ""
}
length := cast.ToInt(args[0])
// 字符处理
intArr := []rune("0123456789")
strLower := []rune("abcdefghijklmnopqrstuvwxyz")
strUpper := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
strSpecial := []rune("{}[]()~!@#$%^&*_-+=/")
allStr := make([]rune, 0)
// 默认全选
typeValue := RandInt + RandStringUpper + RandStringLower
if len(args) > 1 {
typeValue = cast.ToInt(args[1])
}
// 是否包含整数
if RandInt&typeValue == RandInt {
allStr = append(allStr, intArr...)
}
// 是否包含 小写字符串
if RandStringLower&typeValue == RandStringLower {
allStr = append(allStr, strLower...)
}
// 是否包含大写字符串
if RandStringUpper&typeValue == RandStringUpper {
allStr = append(allStr, strUpper...)
}
// 是否包含特殊字符串
if RandStringSpecial&typeValue == RandStringSpecial {
allStr = append(allStr, strSpecial...)
}
if len(allStr) == 0 {
return ""
}
result := make([]rune, length)
rand.Seed(time.Now().UnixNano() + int64(rand.Intn(100)))
for i := 0; i < length; i++ {
result[i] = allStr[rand.Intn(len(allStr))]
}
return string(result)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/vgos/plugin.git
git@gitee.com:vgos/plugin.git
vgos
plugin
plugin
v0.0.2

搜索帮助