vid.go 753 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package core
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "fmt"
  6. )
  7. // The ID of en entity, in the form of an UUID.
  8. type VID [16]byte
  9. func (v VID) Hash(suffix []byte) []byte {
  10. md5 := md5.New()
  11. md5.Write(v[:])
  12. md5.Write(suffix)
  13. return md5.Sum(nil)
  14. }
  15. var byteGroups = []int{8, 4, 4, 4, 12}
  16. // TODO: leverage a full functional UUID library
  17. func UUIDToVID(uuid string) (v VID, err error) {
  18. text := []byte(uuid)
  19. if len(text) < 32 {
  20. err = fmt.Errorf("uuid: invalid UUID string: %s", text)
  21. return
  22. }
  23. b := v[:]
  24. for _, byteGroup := range byteGroups {
  25. if text[0] == '-' {
  26. text = text[1:]
  27. }
  28. _, err = hex.Decode(b[:byteGroup/2], text[:byteGroup])
  29. if err != nil {
  30. return
  31. }
  32. text = text[byteGroup:]
  33. b = b[byteGroup/2:]
  34. }
  35. return
  36. }