vid.go 824 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. // Hash generates a MD5 hash based on current VID and a suffix string.
  10. func (v VID) Hash(suffix []byte) []byte {
  11. md5 := md5.New()
  12. md5.Write(v[:])
  13. md5.Write(suffix)
  14. return md5.Sum(nil)
  15. }
  16. var byteGroups = []int{8, 4, 4, 4, 12}
  17. // TODO: leverage a full functional UUID library
  18. func UUIDToVID(uuid string) (v VID, err error) {
  19. text := []byte(uuid)
  20. if len(text) < 32 {
  21. err = fmt.Errorf("uuid: invalid UUID string: %s", text)
  22. return
  23. }
  24. b := v[:]
  25. for _, byteGroup := range byteGroups {
  26. if text[0] == '-' {
  27. text = text[1:]
  28. }
  29. _, err = hex.Decode(b[:byteGroup/2], text[:byteGroup])
  30. if err != nil {
  31. return
  32. }
  33. text = text[byteGroup:]
  34. b = b[byteGroup/2:]
  35. }
  36. return
  37. }