randomstringutils.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. Copyright 2014 Alexander Okoli
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package goutils
  14. import (
  15. "fmt"
  16. "math"
  17. "math/rand"
  18. "regexp"
  19. "time"
  20. "unicode"
  21. )
  22. // RANDOM provides the time-based seed used to generate random numbers
  23. var RANDOM = rand.New(rand.NewSource(time.Now().UnixNano()))
  24. /*
  25. RandomNonAlphaNumeric creates a random string whose length is the number of characters specified.
  26. Characters will be chosen from the set of all characters (ASCII/Unicode values between 0 to 2,147,483,647 (math.MaxInt32)).
  27. Parameter:
  28. count - the length of random string to create
  29. Returns:
  30. string - the random string
  31. error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
  32. */
  33. func RandomNonAlphaNumeric(count int) (string, error) {
  34. return RandomAlphaNumericCustom(count, false, false)
  35. }
  36. /*
  37. RandomAscii creates a random string whose length is the number of characters specified.
  38. Characters will be chosen from the set of characters whose ASCII value is between 32 and 126 (inclusive).
  39. Parameter:
  40. count - the length of random string to create
  41. Returns:
  42. string - the random string
  43. error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
  44. */
  45. func RandomAscii(count int) (string, error) {
  46. return Random(count, 32, 127, false, false)
  47. }
  48. /*
  49. RandomNumeric creates a random string whose length is the number of characters specified.
  50. Characters will be chosen from the set of numeric characters.
  51. Parameter:
  52. count - the length of random string to create
  53. Returns:
  54. string - the random string
  55. error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
  56. */
  57. func RandomNumeric(count int) (string, error) {
  58. return Random(count, 0, 0, false, true)
  59. }
  60. /*
  61. RandomAlphabetic creates a random string whose length is the number of characters specified.
  62. Characters will be chosen from the set of alpha-numeric characters as indicated by the arguments.
  63. Parameters:
  64. count - the length of random string to create
  65. letters - if true, generated string may include alphabetic characters
  66. numbers - if true, generated string may include numeric characters
  67. Returns:
  68. string - the random string
  69. error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
  70. */
  71. func RandomAlphabetic(count int) (string, error) {
  72. return Random(count, 0, 0, true, false)
  73. }
  74. /*
  75. RandomAlphaNumeric creates a random string whose length is the number of characters specified.
  76. Characters will be chosen from the set of alpha-numeric characters.
  77. Parameter:
  78. count - the length of random string to create
  79. Returns:
  80. string - the random string
  81. error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
  82. */
  83. func RandomAlphaNumeric(count int) (string, error) {
  84. RandomString, err := Random(count, 0, 0, true, true)
  85. if err != nil {
  86. return "", fmt.Errorf("Error: %s", err)
  87. }
  88. match, err := regexp.MatchString("([0-9]+)", RandomString)
  89. if err != nil {
  90. panic(err)
  91. }
  92. if !match {
  93. //Get the position between 0 and the length of the string-1 to insert a random number
  94. position := rand.Intn(count)
  95. //Insert a random number between [0-9] in the position
  96. RandomString = RandomString[:position] + string('0'+rand.Intn(10)) + RandomString[position+1:]
  97. return RandomString, err
  98. }
  99. return RandomString, err
  100. }
  101. /*
  102. RandomAlphaNumericCustom creates a random string whose length is the number of characters specified.
  103. Characters will be chosen from the set of alpha-numeric characters as indicated by the arguments.
  104. Parameters:
  105. count - the length of random string to create
  106. letters - if true, generated string may include alphabetic characters
  107. numbers - if true, generated string may include numeric characters
  108. Returns:
  109. string - the random string
  110. error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
  111. */
  112. func RandomAlphaNumericCustom(count int, letters bool, numbers bool) (string, error) {
  113. return Random(count, 0, 0, letters, numbers)
  114. }
  115. /*
  116. Random creates a random string based on a variety of options, using default source of randomness.
  117. This method has exactly the same semantics as RandomSeed(int, int, int, bool, bool, []char, *rand.Rand), but
  118. instead of using an externally supplied source of randomness, it uses the internal *rand.Rand instance.
  119. Parameters:
  120. count - the length of random string to create
  121. start - the position in set of chars (ASCII/Unicode int) to start at
  122. end - the position in set of chars (ASCII/Unicode int) to end before
  123. letters - if true, generated string may include alphabetic characters
  124. numbers - if true, generated string may include numeric characters
  125. chars - the set of chars to choose randoms from. If nil, then it will use the set of all chars.
  126. Returns:
  127. string - the random string
  128. error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
  129. */
  130. func Random(count int, start int, end int, letters bool, numbers bool, chars ...rune) (string, error) {
  131. return RandomSeed(count, start, end, letters, numbers, chars, RANDOM)
  132. }
  133. /*
  134. RandomSeed creates a random string based on a variety of options, using supplied source of randomness.
  135. If the parameters start and end are both 0, start and end are set to ' ' and 'z', the ASCII printable characters, will be used,
  136. unless letters and numbers are both false, in which case, start and end are set to 0 and math.MaxInt32, respectively.
  137. If chars is not nil, characters stored in chars that are between start and end are chosen.
  138. This method accepts a user-supplied *rand.Rand instance to use as a source of randomness. By seeding a single *rand.Rand instance
  139. with a fixed seed and using it for each call, the same random sequence of strings can be generated repeatedly and predictably.
  140. Parameters:
  141. count - the length of random string to create
  142. start - the position in set of chars (ASCII/Unicode decimals) to start at
  143. end - the position in set of chars (ASCII/Unicode decimals) to end before
  144. letters - if true, generated string may include alphabetic characters
  145. numbers - if true, generated string may include numeric characters
  146. chars - the set of chars to choose randoms from. If nil, then it will use the set of all chars.
  147. random - a source of randomness.
  148. Returns:
  149. string - the random string
  150. error - an error stemming from invalid parameters: if count < 0; or the provided chars array is empty; or end <= start; or end > len(chars)
  151. */
  152. func RandomSeed(count int, start int, end int, letters bool, numbers bool, chars []rune, random *rand.Rand) (string, error) {
  153. if count == 0 {
  154. return "", nil
  155. } else if count < 0 {
  156. err := fmt.Errorf("randomstringutils illegal argument: Requested random string length %v is less than 0.", count) // equiv to err := errors.New("...")
  157. return "", err
  158. }
  159. if chars != nil && len(chars) == 0 {
  160. err := fmt.Errorf("randomstringutils illegal argument: The chars array must not be empty")
  161. return "", err
  162. }
  163. if start == 0 && end == 0 {
  164. if chars != nil {
  165. end = len(chars)
  166. } else {
  167. if !letters && !numbers {
  168. end = math.MaxInt32
  169. } else {
  170. end = 'z' + 1
  171. start = ' '
  172. }
  173. }
  174. } else {
  175. if end <= start {
  176. err := fmt.Errorf("randomstringutils illegal argument: Parameter end (%v) must be greater than start (%v)", end, start)
  177. return "", err
  178. }
  179. if chars != nil && end > len(chars) {
  180. err := fmt.Errorf("randomstringutils illegal argument: Parameter end (%v) cannot be greater than len(chars) (%v)", end, len(chars))
  181. return "", err
  182. }
  183. }
  184. buffer := make([]rune, count)
  185. gap := end - start
  186. // high-surrogates range, (\uD800-\uDBFF) = 55296 - 56319
  187. // low-surrogates range, (\uDC00-\uDFFF) = 56320 - 57343
  188. for count != 0 {
  189. count--
  190. var ch rune
  191. if chars == nil {
  192. ch = rune(random.Intn(gap) + start)
  193. } else {
  194. ch = chars[random.Intn(gap)+start]
  195. }
  196. if letters && unicode.IsLetter(ch) || numbers && unicode.IsDigit(ch) || !letters && !numbers {
  197. if ch >= 56320 && ch <= 57343 { // low surrogate range
  198. if count == 0 {
  199. count++
  200. } else {
  201. // Insert low surrogate
  202. buffer[count] = ch
  203. count--
  204. // Insert high surrogate
  205. buffer[count] = rune(55296 + random.Intn(128))
  206. }
  207. } else if ch >= 55296 && ch <= 56191 { // High surrogates range (Partial)
  208. if count == 0 {
  209. count++
  210. } else {
  211. // Insert low surrogate
  212. buffer[count] = rune(56320 + random.Intn(128))
  213. count--
  214. // Insert high surrogate
  215. buffer[count] = ch
  216. }
  217. } else if ch >= 56192 && ch <= 56319 {
  218. // private high surrogate, skip it
  219. count++
  220. } else {
  221. // not one of the surrogates*
  222. buffer[count] = ch
  223. }
  224. } else {
  225. count++
  226. }
  227. }
  228. return string(buffer), nil
  229. }