You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
768 B

4 years ago
  1. package generate
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "log"
  6. "strings"
  7. )
  8. func id(prefix string, length int) string {
  9. var id [16]byte
  10. var buffer [32]byte
  11. builder := strings.Builder{}
  12. builder.Grow(length + 31)
  13. builder.WriteString(prefix)
  14. for builder.Len() < length {
  15. _, err := rand.Read(id[:])
  16. if err != nil {
  17. log.Panicln("generate.id: failed to use OS random:", err)
  18. }
  19. hex.Encode(buffer[:], id[:])
  20. builder.Write(buffer[:])
  21. }
  22. return builder.String()[:length]
  23. }
  24. func GroupID() string {
  25. return id("G", 16)
  26. }
  27. func ItemID() string {
  28. return id("I", 16)
  29. }
  30. func ProjectID() string {
  31. return id("P", 16)
  32. }
  33. func TaskID() string {
  34. return id("T", 16)
  35. }
  36. func LogID() string {
  37. return id("L", 16)
  38. }
  39. func GoalID() string {
  40. return id("A", 16)
  41. }