1
0
Simple telnet server for write.as http://nerds.write.as
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

32 рядки
1.0 KiB

  1. package store
  2. import (
  3. "crypto/rand"
  4. )
  5. // Generate62RandomString creates a random string with the given length
  6. // consisting of characters in [A-Za-z0-9].
  7. func Generate62RandomString(l int) string {
  8. return GenerateRandomString("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", l)
  9. }
  10. // GenerateFriendlyRandomString creates a random string of characters with the
  11. // given length consististing of characters in [a-z0-9].
  12. func GenerateFriendlyRandomString(l int) string {
  13. return GenerateRandomString("0123456789abcdefghijklmnopqrstuvwxyz", l)
  14. }
  15. // GenerateRandomString creates a random string of characters of the given
  16. // length from the given dictionary of possible characters.
  17. //
  18. // This example generates a hexadecimal string 6 characters long:
  19. // GenerateRandomString("0123456789abcdef", 6)
  20. func GenerateRandomString(dictionary string, l int) string {
  21. var bytes = make([]byte, l)
  22. rand.Read(bytes)
  23. for k, v := range bytes {
  24. bytes[k] = dictionary[v%byte(len(dictionary))]
  25. }
  26. return string(bytes)
  27. }