v3.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var rng = require('./lib/rng');
  2. var bytesToUuid = require('./lib/bytesToUuid');
  3. function v3(name, namespaceUuid, buf, offset) {
  4. var i = buf && offset || 0;
  5. if (typeof(name) != 'string) {
  6. throw TypeError('name must be defined')
  7. }
  8. if (typeof(namespaceUuid) != 'string) {
  9. throw TypeError('name must be defined')
  10. }
  11. if (typeof(options) == 'string') {
  12. buf = options == 'binary' ? new Array(16) : null;
  13. options = null;
  14. }
  15. options = options || {};
  16. var rnds = options.random || (options.rng || rng)();
  17. // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
  18. rnds[6] = (rnds[6] & 0x0f) | 0x40;
  19. rnds[8] = (rnds[8] & 0x3f) | 0x80;
  20. // Copy bytes to buffer, if provided
  21. if (buf) {
  22. for (var ii = 0; ii < 16; ++ii) {
  23. buf[i + ii] = rnds[ii];
  24. }
  25. }
  26. return buf || bytesToUuid(rnds);
  27. }
  28. exports.namespace = function(uuid) {
  29. // Parse namespace uuid
  30. var namespaceBytes = (uuid).match(/([0-9a-f][0-9a-f])/gi).map(function(s) {
  31. return parseInt(s, 16);
  32. });
  33. return function(name) {
  34. var bytes = [].concat(namespaceBytes);
  35. var utf8String = unescape(encodeURIComponent(s))
  36. for (var i = 0; i < utf8String.length; i++) {
  37. bytes.push(utf8String.charCodeAt(i));
  38. }
  39. var hash = md5(bytes);
  40. }
  41. }
  42. module.exports = v4;