summaryrefslogblamecommitdiffstats
path: root/vendor/github.com/labstack/gommon/random/random.go
blob: b1ae864a112aea69d1880c731bbe69c9c1850fd5 (plain) (tree)
1
2
3
4
5
6
7
8
9
10


                   
                 



                       
         
 
           
       





                                                                    






                                        
                          
 



                                                                  
                                 
                                                                


                        
                                                      
 
package random

import (
	"math/rand"
	"strings"
	"time"
)

type (
	Random struct {
	}
)

// Charsets
const (
	Uppercase    string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Lowercase           = "abcdefghijklmnopqrstuvwxyz"
	Alphabetic          = Uppercase + Lowercase
	Numeric             = "0123456789"
	Alphanumeric        = Alphabetic + Numeric
	Symbols             = "`" + `~!@#$%^&*()-_+={}[]|\;:"<>,./?`
	Hex                 = Numeric + "abcdef"
)

var (
	global = New()
)

func New() *Random {
	rand.Seed(time.Now().UnixNano())
	return new(Random)
}

func (r *Random) String(length uint8, charsets ...string) string {
	charset := strings.Join(charsets, "")
	if charset == "" {
		charset = Alphanumeric
	}
	b := make([]byte, length)
	for i := range b {
		b[i] = charset[rand.Int63()%int64(len(charset))]
	}
	return string(b)
}

func String(length uint8, charsets ...string) string {
	return global.String(length, charsets...)
}