id.go 727 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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) Bytes() []byte {
  19. return this.uuid.Bytes()
  20. }
  21. func (this *ID) String() string {
  22. return this.uuid.String()
  23. }
  24. func (this *ID) UUID() *uuid.UUID {
  25. return this.uuid
  26. }
  27. func (v ID) CmdKey() []byte {
  28. return v.cmdKey[:]
  29. }
  30. func NewID(uuid *uuid.UUID) *ID {
  31. id := &ID{uuid: uuid}
  32. md5hash := md5.New()
  33. md5hash.Write(uuid.Bytes())
  34. md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
  35. md5.Sum(id.cmdKey[:0])
  36. return id
  37. }