uuid.go 668 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package cryptutil
  10. import (
  11. "crypto/rand"
  12. "devt.de/krotik/common/errorutil"
  13. )
  14. /*
  15. GenerateUUID generates a version 4 (randomly generated) UUID according to RFC4122.
  16. */
  17. func GenerateUUID() [16]byte {
  18. var u [16]byte
  19. _, err := rand.Read(u[:])
  20. errorutil.AssertOk(err)
  21. // Set version 4
  22. u[6] = (u[6] & 0x0f) | 0x40
  23. // Set variant bits - variant of RFC 4122
  24. u[8] = (u[8] & 0xbf) | 0x80
  25. return u
  26. }