gitlab.com/beacon-software/gadget@v0.0.0-20181217202115-54565ea1ed5e/generator/strings.go (about) 1 package generator 2 3 import ( 4 "encoding/base64" 5 "fmt" 6 "math/rand" 7 "strings" 8 "time" 9 10 "github.com/google/uuid" 11 ) 12 13 var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") 14 var hex = []rune("0123456789ABCDEF") 15 var numbers = []rune("0123456789") 16 var characters = []rune("!@#$%`'^&*()_+") 17 var base32 = []rune("13456789abcdefghijkmnopqrstuwxyz") 18 19 // Password generates a random password of a given length 20 func Password(length int) string { 21 source := append(letters, characters...) 22 source = append(source, numbers...) 23 return random(length, source) 24 } 25 26 func random(length int, source []rune) string { 27 r := rand.New(rand.NewSource(time.Now().UnixNano())) 28 maxInt := len(source) - 1 29 30 randomString := make([]rune, length) 31 for i := range randomString { 32 randomString[i] = source[r.Intn(maxInt)] 33 } 34 return string(randomString) 35 } 36 37 // IDPrefix is the human readable prefix that is attached to a generated ID 38 type IDPrefix string 39 40 // IDSizeBytes is the length of an identifier in bytes in the system. 41 const IDSizeBytes = 32 42 43 // Base32IDSizeBytes is the length of a base 32 identifier in bytes in the system. 44 const Base32IDSizeBytes = 18 45 46 // MaxPrefix is the maximum ID prefix length 47 const MaxPrefix = 8 48 49 // ID creates a random id starting with prefix to idSize length 50 func ID(prefix IDPrefix) string { 51 if len(prefix) > MaxPrefix { 52 panic(fmt.Sprintf("%s is too long of a prefix ID", prefix)) 53 } 54 n := IDSizeBytes - len(prefix) - 1 55 s := base64.StdEncoding.EncodeToString([]byte(strings.Replace(uuid.New().String(), "-", "", -1))) 56 return fmt.Sprintf("%s_%s", prefix, s[:n]) 57 } 58 59 // Base32ID creates a random id starting with prefix to idSize length 60 func Base32ID(prefix IDPrefix) string { 61 if len(prefix) > MaxPrefix { 62 panic(fmt.Sprintf("%s is too long of a prefix ID", prefix)) 63 } 64 n := Base32IDSizeBytes - len(prefix) - 1 65 return fmt.Sprintf("%s_%s", prefix, random(n, base32)) 66 } 67 68 const secretLength = 32 69 70 // Secret returns a string that is suitable as a Salt or a Client Secret 71 func Secret() string { 72 return Password(secretLength) 73 } 74 75 // Code returns random letters & numbers of the requested length 76 func Code(length int) string { 77 return random(length, append(letters, numbers...)) 78 } 79 80 // Hex returns a hex string of the requested length 81 func Hex(length int) string { 82 return random(length, hex) 83 } 84 85 /* 86 Random generators to assist with testing 87 */ 88 89 // TestID returns an ID with a random prefix for testing purposes 90 func TestID() string { 91 return ID(IDPrefix(String(3))) 92 } 93 94 // String returns random letters of the requested length 95 func String(length int) string { 96 return random(length, letters) 97 } 98 99 // HexColor returns a 7 length string from #000000 - #FFFFFF 100 func HexColor() string { 101 return "#" + random(6, hex) 102 } 103 104 // Year returns random year between 1976 and 2017 105 func Year() int32 { 106 r := rand.New(rand.NewSource(time.Now().UnixNano())) 107 return int32(r.Intn(41) + 1976) 108 } 109 110 // Email returns a fake email address for testing 111 func Email() string { 112 return fmt.Sprintf("fake+%s@kasita.com", random(10, letters)) 113 } 114 115 // Name returns a fake name for testing 116 func Name() string { 117 return fmt.Sprintf("%s %s", random(6, letters), random(12, letters)) 118 }