package corelib import ( "math/rand" "time" ) func randomString(n int, allowedChars []rune) string { r := rand.New(rand.NewSource(time.Now().UnixNano())) b := make([]rune, n) for i := range b { b[i] = allowedChars[r.Intn(len(allowedChars))] } return string(b) } // 生成随机字符串 func generateRandom(randType int, length int) string { var res string const special = "~!@#$%^&*()_+=-" const number = "0123456789" const capitalLetter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" const lowerLetter = "abcdefghijklmnopqrstuvwxyz" switch { case randType == 1: // 特俗符号 res = special case randType == 2: // 数字 res = number case randType == 3: // 大写字母 res = capitalLetter case randType == 4: // 小写字母 res = lowerLetter case randType == 5: // 大写字母+小写字母 res = capitalLetter + lowerLetter case randType == 6: // 大写字母+小写字母+数字 res = capitalLetter + lowerLetter + number default: // 大写字母+小写字母+数字+特殊符号 res = capitalLetter + lowerLetter + number + special } return randomString(length, []rune(res)) } // GenerateSalt 生成一个随机盐 func GenerateSalt() string { const saltSize = 16 // 16字节的随机盐 return generateRandom(0, saltSize) }