version.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package internal
  2. // Version represents the version of UUID. See page 7 in RFC 4122.
  3. type Version byte
  4. // Version List
  5. const (
  6. VersionUnknown Version = iota // Unknwon
  7. VersionTimeBased // V1: The time-based version
  8. VersionDCESecurity // V2: The DCE security version, with embedded POSIX UIDs
  9. VersionNameBasedMD5 // V3: The name-based version that uses MD5 hashing
  10. VersionRandom // V4: The randomly or pseudo-randomly generated version
  11. VersionNameBasedSHA1 // V5: The name-based version that uses SHA-1 hashing
  12. )
  13. // SetVersion sets the version for uuid.
  14. // This is intended to be called from the New function in packages that implement uuid generating functions.
  15. func SetVersion(uuid []byte, version Version) {
  16. switch version {
  17. case VersionTimeBased:
  18. uuid[6] = (uuid[6] | 0x10) & 0x1f
  19. case VersionDCESecurity:
  20. uuid[6] = (uuid[6] | 0x20) & 0x2f
  21. case VersionNameBasedMD5:
  22. uuid[6] = (uuid[6] | 0x30) & 0x3f
  23. case VersionRandom:
  24. uuid[6] = (uuid[6] | 0x40) & 0x4f
  25. case VersionNameBasedSHA1:
  26. uuid[6] = (uuid[6] | 0x50) & 0x5f
  27. default:
  28. panic("version is unknown")
  29. }
  30. }
  31. // GetVersion gets the version of uuid.
  32. func GetVersion(uuid []byte) Version {
  33. ver := uuid[6] >> 4
  34. if ver > 0 && ver < 6 {
  35. return Version(ver)
  36. }
  37. return VersionUnknown
  38. }