Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. // uuid returns an RFC 4122 compliant universally unique
  2. // identifier using the crypto API
  3. function uuid() {
  4.  
  5. // get sixteen unsigned 8 bit random values
  6. var u = window
  7. .crypto
  8. .getRandomValues(new Uint8Array(16));
  9.  
  10. // set the version bit to v4
  11. u[6] = (u[6] & 0x0f) | 0x40
  12.  
  13. // set the variant bit to "don't care" (yes, the RFC
  14. // calls it that)
  15. u[8] = (u[8] & 0xbf) | 0x80
  16.  
  17. // hex encode them and add the dashes
  18. var uid = "";
  19. uid += u[0].toString(16);
  20. uid += u[1].toString(16);
  21. uid += u[2].toString(16);
  22. uid += u[3].toString(16);
  23. uid += "-";
  24.  
  25. uid += u[4].toString(16);
  26. uid += u[5].toString(16);
  27. uid += "-";
  28.  
  29. uid += u[6].toString(16);
  30. uid += u[7].toString(16);
  31. uid += "-";
  32.  
  33. uid += u[8].toString(16);
  34. uid += u[9].toString(16);
  35. uid += "-";
  36.  
  37. uid += u[10].toString(16);
  38. uid += u[11].toString(16);
  39. uid += u[12].toString(16);
  40. uid += u[13].toString(16);
  41. uid += u[14].toString(16);
  42. uid += u[15].toString(16);
  43.  
  44. return uid;
  45. }
  46.  
  47. // outputs something like:
  48. // 6fc4b899-44af-4092-a0ee-b391e90eef12
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement