id.go 816 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package vmess
  2. import (
  3. "crypto/md5"
  4. "errors"
  5. "github.com/v2ray/v2ray-core/common/uuid"
  6. )
  7. const (
  8. IDBytesLen = 16
  9. )
  10. var (
  11. InvalidID = errors.New("Invalid ID.")
  12. )
  13. // The ID of en entity, in the form of an UUID.
  14. type ID struct {
  15. uuid *uuid.UUID
  16. cmdKey [IDBytesLen]byte
  17. }
  18. func (this *ID) Equals(another *ID) bool {
  19. return this.uuid.Equals(another.uuid)
  20. }
  21. func (this *ID) Bytes() []byte {
  22. return this.uuid.Bytes()
  23. }
  24. func (this *ID) String() string {
  25. return this.uuid.String()
  26. }
  27. func (this *ID) UUID() *uuid.UUID {
  28. return this.uuid
  29. }
  30. func (v ID) CmdKey() []byte {
  31. return v.cmdKey[:]
  32. }
  33. func NewID(uuid *uuid.UUID) *ID {
  34. id := &ID{uuid: uuid}
  35. md5hash := md5.New()
  36. md5hash.Write(uuid.Bytes())
  37. md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
  38. md5hash.Sum(id.cmdKey[:0])
  39. return id
  40. }