id.go 847 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package core
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "github.com/v2ray/v2ray-core/log"
  6. )
  7. // The ID of en entity, in the form of an UUID.
  8. type ID [16]byte
  9. // Hash generates a MD5 hash based on current ID and a suffix string.
  10. func (v ID) Hash(suffix []byte) []byte {
  11. md5 := md5.New()
  12. md5.Write(v[:])
  13. md5.Write(suffix)
  14. return md5.Sum(nil)
  15. }
  16. var byteGroups = []int{8, 4, 4, 4, 12}
  17. // TODO: leverage a full functional UUID library
  18. func UUIDToID(uuid string) (v ID, err error) {
  19. text := []byte(uuid)
  20. if len(text) < 32 {
  21. err = log.Error("uuid: invalid UUID string: %s", text)
  22. return
  23. }
  24. b := v[:]
  25. for _, byteGroup := range byteGroups {
  26. if text[0] == '-' {
  27. text = text[1:]
  28. }
  29. _, err = hex.Decode(b[:byteGroup/2], text[:byteGroup])
  30. if err != nil {
  31. return
  32. }
  33. text = text[byteGroup:]
  34. b = b[byteGroup/2:]
  35. }
  36. return
  37. }