1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- package utils
- import (
- "crypto/rand"
- r "math/rand"
- "time"
- )
- var alphaNum = []byte(`0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`)
- func RandomCreateBytes(n int, alphabets ...byte) []byte {
- if len(alphabets) == 0 {
- alphabets = alphaNum
- }
- var bytes = make([]byte, n)
- var randBy bool
- if num, err := rand.Read(bytes); num != n || err != nil {
- r.Seed(time.Now().UnixNano())
- randBy = true
- }
- for i, b := range bytes {
- if randBy {
- bytes[i] = alphabets[r.Intn(len(alphabets))]
- } else {
- bytes[i] = alphabets[b%byte(len(alphabets))]
- }
- }
- return bytes
- }
|