id.go 744 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 NewID(uuid *uuid.UUID) *ID {
  28. md5hash := md5.New()
  29. md5hash.Write(uuid.Bytes())
  30. md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
  31. cmdKey := md5.Sum(nil)
  32. return &ID{
  33. uuid: uuid,
  34. cmdKey: cmdKey,
  35. }
  36. }
  37. func (v ID) CmdKey() []byte {
  38. return v.cmdKey[:]
  39. }