/lib/uuidv5.js

https://gitlab.com/pump.io/pump.io · JavaScript · 119 lines · 67 code · 21 blank · 31 comment · 16 complexity · 7324528dfbb5c485171c827ed3e880ac MD5 · raw file

  1. // uuidv5.js
  2. //
  3. // Make a v5 UUID from a string
  4. //
  5. // Copyright 2011-2013, E14N https://e14n.com/
  6. //
  7. // Licensed under the Apache License, Version 2.0 (the "License");
  8. // you may not use this file except in compliance with the License.
  9. // You may obtain a copy of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS,
  15. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. // See the License for the specific language governing permissions and
  17. // limitations under the License.
  18. "use strict";
  19. // Originally from uuid.js
  20. // uuid.js
  21. //
  22. // Copyright (c) 2010-2012 Robert Kieffer
  23. // MIT License - http://opensource.org/licenses/mit-license.php
  24. //
  25. // Branch for v5 UUIDs by OrangeDog
  26. // http://github.com/OrangeDog
  27. var _ = require("lodash"),
  28. crypto = require("crypto");
  29. // Maps for number <-> hex string conversion
  30. var _byteToHex = [];
  31. var _hexToByte = {};
  32. for (var i = 0; i < 256; i++) {
  33. _byteToHex[i] = (i + 0x100).toString(16).slice(1);
  34. _hexToByte[_byteToHex[i]] = i;
  35. }
  36. // **`parse()` - Parse a UUID into its component bytes**
  37. function parse(s, buf, offset) {
  38. var i = (buf && offset) || 0, ii = 0;
  39. buf = buf || [];
  40. s.toLowerCase().replace(/[0-9a-f]{2}/g, function(oct) {
  41. if (ii < 16) { // Don't overflow!
  42. buf[i + ii++] = _hexToByte[oct];
  43. }
  44. });
  45. // Zero out remaining bytes if string was short
  46. while (ii < 16) {
  47. buf[i + ii++] = 0;
  48. }
  49. return buf;
  50. }
  51. // **`unparse()` - Convert UUID byte array (ala parse()) into a string**
  52. function unparse(buf, offset) {
  53. var i = offset || 0, bth = _byteToHex;
  54. return bth[buf[i++]] + bth[buf[i++]] +
  55. bth[buf[i++]] + bth[buf[i++]] + "-" +
  56. bth[buf[i++]] + bth[buf[i++]] + "-" +
  57. bth[buf[i++]] + bth[buf[i++]] + "-" +
  58. bth[buf[i++]] + bth[buf[i++]] + "-" +
  59. bth[buf[i++]] + bth[buf[i++]] +
  60. bth[buf[i++]] + bth[buf[i++]] +
  61. bth[buf[i++]] + bth[buf[i++]];
  62. }
  63. function uuidv5(data, ns) {
  64. var i, v;
  65. var output = new Buffer(16);
  66. if (!data) {
  67. for (i = 0; i<16; i++) {
  68. output[i] = 0;
  69. }
  70. return unparse(output);
  71. }
  72. if (typeof ns === "string") {
  73. ns = parse(ns, new Buffer(16));
  74. }
  75. var hash = crypto.createHash("sha1");
  76. hash.update(ns || "");
  77. hash.update(data || "");
  78. v = 0x50;
  79. var digest = hash.digest();
  80. if (_.isString(digest)) {
  81. output.write(digest, 0, 16, "binary");
  82. } else if (_.isObject(digest) && digest instanceof Buffer) {
  83. digest.copy(output);
  84. }
  85. /* jshint bitwise: false */
  86. output[8] = output[8] & 0x3f | 0xa0; // set variant
  87. /* jshint bitwise: false */
  88. output[6] = output[6] & 0x0f | v; // set version
  89. return unparse(output);
  90. }
  91. var namespaces = {
  92. DNS: "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
  93. URL: "6ba7b811-9dad-11d1-80b4-00c04fd430c8",
  94. OID: "6ba7b812-9dad-11d1-80b4-00c04fd430c8",
  95. X500: "6ba7b814-9dad-11d1-80b4-00c04fd430c8"
  96. };
  97. module.exports = uuidv5;
  98. uuidv5.ns = namespaces;