cryptorandomstringutils.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. "crypto/rand"
  16. "fmt"
  17. "math"
  18. "math/big"
  19. "unicode"
  20. )
  21. /*
  22. CryptoRandomNonAlphaNumeric creates a random string whose length is the number of characters specified.
  23. Characters will be chosen from the set of all characters (ASCII/Unicode values between 0 to 2,147,483,647 (math.MaxInt32)).
  24. Parameter:
  25. count - the length of random string to create
  26. Returns:
  27. string - the random string
  28. error - an error stemming from an invalid parameter within underlying function, CryptoRandom(...)
  29. */
  30. func CryptoRandomNonAlphaNumeric(count int) (string, error) {
  31. return CryptoRandomAlphaNumericCustom(count, false, false)
  32. }
  33. /*
  34. CryptoRandomAscii creates a random string whose length is the number of characters specified.
  35. Characters will be chosen from the set of characters whose ASCII value is between 32 and 126 (inclusive).
  36. Parameter:
  37. count - the length of random string to create
  38. Returns:
  39. string - the random string
  40. error - an error stemming from an invalid parameter within underlying function, CryptoRandom(...)
  41. */
  42. func CryptoRandomAscii(count int) (string, error) {
  43. return CryptoRandom(count, 32, 127, false, false)
  44. }
  45. /*
  46. CryptoRandomNumeric creates a random string whose length is the number of characters specified.
  47. Characters will be chosen from the set of numeric characters.
  48. Parameter:
  49. count - the length of random string to create
  50. Returns:
  51. string - the random string
  52. error - an error stemming from an invalid parameter within underlying function, CryptoRandom(...)
  53. */
  54. func CryptoRandomNumeric(count int) (string, error) {
  55. return CryptoRandom(count, 0, 0, false, true)
  56. }
  57. /*
  58. CryptoRandomAlphabetic creates a random string whose length is the number of characters specified.
  59. Characters will be chosen from the set of alpha-numeric characters as indicated by the arguments.
  60. Parameters:
  61. count - the length of random string to create
  62. letters - if true, generated string may include alphabetic characters
  63. numbers - if true, generated string may include numeric characters
  64. Returns:
  65. string - the random string
  66. error - an error stemming from an invalid parameter within underlying function, CryptoRandom(...)
  67. */
  68. func CryptoRandomAlphabetic(count int) (string, error) {
  69. return CryptoRandom(count, 0, 0, true, false)
  70. }
  71. /*
  72. CryptoRandomAlphaNumeric creates a random string whose length is the number of characters specified.
  73. Characters will be chosen from the set of alpha-numeric characters.
  74. Parameter:
  75. count - the length of random string to create
  76. Returns:
  77. string - the random string
  78. error - an error stemming from an invalid parameter within underlying function, CryptoRandom(...)
  79. */
  80. func CryptoRandomAlphaNumeric(count int) (string, error) {
  81. return CryptoRandom(count, 0, 0, true, true)
  82. }
  83. /*
  84. CryptoRandomAlphaNumericCustom creates a random string whose length is the number of characters specified.
  85. Characters will be chosen from the set of alpha-numeric characters as indicated by the arguments.
  86. Parameters:
  87. count - the length of random string to create
  88. letters - if true, generated string may include alphabetic characters
  89. numbers - if true, generated string may include numeric characters
  90. Returns:
  91. string - the random string
  92. error - an error stemming from an invalid parameter within underlying function, CryptoRandom(...)
  93. */
  94. func CryptoRandomAlphaNumericCustom(count int, letters bool, numbers bool) (string, error) {
  95. return CryptoRandom(count, 0, 0, letters, numbers)
  96. }
  97. /*
  98. CryptoRandom creates a random string based on a variety of options, using using golang's crypto/rand source of randomness.
  99. If the parameters start and end are both 0, start and end are set to ' ' and 'z', the ASCII printable characters, will be used,
  100. unless letters and numbers are both false, in which case, start and end are set to 0 and math.MaxInt32, respectively.
  101. If chars is not nil, characters stored in chars that are between start and end are chosen.
  102. Parameters:
  103. count - the length of random string to create
  104. start - the position in set of chars (ASCII/Unicode int) to start at
  105. end - the position in set of chars (ASCII/Unicode int) to end before
  106. letters - if true, generated string may include alphabetic characters
  107. numbers - if true, generated string may include numeric characters
  108. chars - the set of chars to choose randoms from. If nil, then it will use the set of all chars.
  109. Returns:
  110. string - the random string
  111. error - an error stemming from invalid parameters: if count < 0; or the provided chars array is empty; or end <= start; or end > len(chars)
  112. */
  113. func CryptoRandom(count int, start int, end int, letters bool, numbers bool, chars ...rune) (string, error) {
  114. if count == 0 {
  115. return "", nil
  116. } else if count < 0 {
  117. err := fmt.Errorf("randomstringutils illegal argument: Requested random string length %v is less than 0.", count) // equiv to err := errors.New("...")
  118. return "", err
  119. }
  120. if chars != nil && len(chars) == 0 {
  121. err := fmt.Errorf("randomstringutils illegal argument: The chars array must not be empty")
  122. return "", err
  123. }
  124. if start == 0 && end == 0 {
  125. if chars != nil {
  126. end = len(chars)
  127. } else {
  128. if !letters && !numbers {
  129. end = math.MaxInt32
  130. } else {
  131. end = 'z' + 1
  132. start = ' '
  133. }
  134. }
  135. } else {
  136. if end <= start {
  137. err := fmt.Errorf("randomstringutils illegal argument: Parameter end (%v) must be greater than start (%v)", end, start)
  138. return "", err
  139. }
  140. if chars != nil && end > len(chars) {
  141. err := fmt.Errorf("randomstringutils illegal argument: Parameter end (%v) cannot be greater than len(chars) (%v)", end, len(chars))
  142. return "", err
  143. }
  144. }
  145. buffer := make([]rune, count)
  146. gap := end - start
  147. // high-surrogates range, (\uD800-\uDBFF) = 55296 - 56319
  148. // low-surrogates range, (\uDC00-\uDFFF) = 56320 - 57343
  149. for count != 0 {
  150. count--
  151. var ch rune
  152. if chars == nil {
  153. ch = rune(getCryptoRandomInt(gap) + int64(start))
  154. } else {
  155. ch = chars[getCryptoRandomInt(gap)+int64(start)]
  156. }
  157. if letters && unicode.IsLetter(ch) || numbers && unicode.IsDigit(ch) || !letters && !numbers {
  158. if ch >= 56320 && ch <= 57343 { // low surrogate range
  159. if count == 0 {
  160. count++
  161. } else {
  162. // Insert low surrogate
  163. buffer[count] = ch
  164. count--
  165. // Insert high surrogate
  166. buffer[count] = rune(55296 + getCryptoRandomInt(128))
  167. }
  168. } else if ch >= 55296 && ch <= 56191 { // High surrogates range (Partial)
  169. if count == 0 {
  170. count++
  171. } else {
  172. // Insert low surrogate
  173. buffer[count] = rune(56320 + getCryptoRandomInt(128))
  174. count--
  175. // Insert high surrogate
  176. buffer[count] = ch
  177. }
  178. } else if ch >= 56192 && ch <= 56319 {
  179. // private high surrogate, skip it
  180. count++
  181. } else {
  182. // not one of the surrogates*
  183. buffer[count] = ch
  184. }
  185. } else {
  186. count++
  187. }
  188. }
  189. return string(buffer), nil
  190. }
  191. func getCryptoRandomInt(count int) int64 {
  192. nBig, err := rand.Int(rand.Reader, big.NewInt(int64(count)))
  193. if err != nil {
  194. panic(err)
  195. }
  196. return nBig.Int64()
  197. }