id.go 687 B

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