id.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package core
  2. import (
  3. "crypto/hmac"
  4. "crypto/md5"
  5. "encoding/hex"
  6. mrand "math/rand"
  7. "time"
  8. "github.com/v2ray/v2ray-core/log"
  9. )
  10. const (
  11. IDBytesLen = 16
  12. )
  13. // The ID of en entity, in the form of an UUID.
  14. type ID struct {
  15. String string
  16. Bytes []byte
  17. cmdKey []byte
  18. }
  19. func NewID(id string) (ID, error) {
  20. idBytes, err := UUIDToID(id)
  21. if err != nil {
  22. return ID{}, log.Error("Failed to parse id %s", id)
  23. }
  24. md5hash := md5.New()
  25. md5hash.Write(idBytes)
  26. md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
  27. cmdKey := md5.Sum(nil)
  28. return ID{
  29. String: id,
  30. Bytes: idBytes,
  31. cmdKey: cmdKey[:],
  32. }, nil
  33. }
  34. func (v ID) TimeRangeHash(rangeSec int) ([]byte, int64) {
  35. nowSec := time.Now().UTC().Unix()
  36. delta := mrand.Intn(rangeSec*2) - rangeSec
  37. targetSec := nowSec + int64(delta)
  38. return v.TimeHash(targetSec), targetSec
  39. }
  40. func (v ID) TimeHash(timeSec int64) []byte {
  41. buffer := []byte{
  42. byte(timeSec >> 56),
  43. byte(timeSec >> 48),
  44. byte(timeSec >> 40),
  45. byte(timeSec >> 32),
  46. byte(timeSec >> 24),
  47. byte(timeSec >> 16),
  48. byte(timeSec >> 8),
  49. byte(timeSec),
  50. }
  51. return v.Hash(buffer)
  52. }
  53. func (v ID) Hash(data []byte) []byte {
  54. hasher := hmac.New(md5.New, v.Bytes)
  55. hasher.Write(data)
  56. return hasher.Sum(nil)
  57. }
  58. func (v ID) CmdKey() []byte {
  59. return v.cmdKey
  60. }
  61. func TimestampHash(timeSec int64) []byte {
  62. md5hash := md5.New()
  63. buffer := []byte{
  64. byte(timeSec >> 56),
  65. byte(timeSec >> 48),
  66. byte(timeSec >> 40),
  67. byte(timeSec >> 32),
  68. byte(timeSec >> 24),
  69. byte(timeSec >> 16),
  70. byte(timeSec >> 8),
  71. byte(timeSec),
  72. }
  73. md5hash.Write(buffer)
  74. md5hash.Write(buffer)
  75. md5hash.Write(buffer)
  76. md5hash.Write(buffer)
  77. return md5hash.Sum(nil)
  78. }
  79. var byteGroups = []int{8, 4, 4, 4, 12}
  80. // TODO: leverage a full functional UUID library
  81. func UUIDToID(uuid string) (v []byte, err error) {
  82. v = make([]byte, 16)
  83. text := []byte(uuid)
  84. if len(text) < 32 {
  85. err = log.Error("uuid: invalid UUID string: %s", text)
  86. return
  87. }
  88. b := v[:]
  89. for _, byteGroup := range byteGroups {
  90. if text[0] == '-' {
  91. text = text[1:]
  92. }
  93. _, err = hex.Decode(b[:byteGroup/2], text[:byteGroup])
  94. if err != nil {
  95. return
  96. }
  97. text = text[byteGroup:]
  98. b = b[byteGroup/2:]
  99. }
  100. return
  101. }