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.

56 lines
822 B

3 years ago
3 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 ProjectGroupID() string {
  34. return id("C", 16)
  35. }
  36. func TaskID() string {
  37. return id("T", 16)
  38. }
  39. func LogID() string {
  40. return id("L", 16)
  41. }
  42. func GoalID() string {
  43. return id("A", 16)
  44. }