github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/common/protocol/id.go (about) 1 package protocol 2 3 import ( 4 "crypto/md5" 5 6 "github.com/xmplusdev/xmcore/common" 7 "github.com/xmplusdev/xmcore/common/uuid" 8 ) 9 10 const ( 11 IDBytesLen = 16 12 ) 13 14 // The ID of en entity, in the form of a UUID. 15 type ID struct { 16 uuid uuid.UUID 17 cmdKey [IDBytesLen]byte 18 } 19 20 // Equals returns true if this ID equals to the other one. 21 func (id *ID) Equals(another *ID) bool { 22 return id.uuid.Equals(&(another.uuid)) 23 } 24 25 func (id *ID) Bytes() []byte { 26 return id.uuid.Bytes() 27 } 28 29 func (id *ID) String() string { 30 return id.uuid.String() 31 } 32 33 func (id *ID) UUID() uuid.UUID { 34 return id.uuid 35 } 36 37 func (id ID) CmdKey() []byte { 38 return id.cmdKey[:] 39 } 40 41 // NewID returns an ID with given UUID. 42 func NewID(uuid uuid.UUID) *ID { 43 id := &ID{uuid: uuid} 44 md5hash := md5.New() 45 common.Must2(md5hash.Write(uuid.Bytes())) 46 common.Must2(md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))) 47 md5hash.Sum(id.cmdKey[:0]) 48 return id 49 }