2016-03-09 05:30:04 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2017-01-22 05:09:29 +00:00
|
|
|
const createHash = require('crypto').createHash;
|
2016-03-09 05:30:04 +00:00
|
|
|
|
|
|
|
exports.createNamedUUID = createNamedUUID;
|
|
|
|
|
|
|
|
function createNamedUUID(namespaceUuid, key) {
|
|
|
|
//
|
|
|
|
// v5 UUID generation code based on the work here:
|
|
|
|
// https://github.com/download13/uuidv5/blob/master/uuid.js
|
2018-01-15 19:22:11 +00:00
|
|
|
//
|
2017-01-22 05:09:29 +00:00
|
|
|
if(!Buffer.isBuffer(namespaceUuid)) {
|
2018-04-28 12:59:07 +00:00
|
|
|
namespaceUuid = Buffer.from(namespaceUuid);
|
2017-01-22 05:09:29 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
if(!Buffer.isBuffer(key)) {
|
2018-04-28 12:59:07 +00:00
|
|
|
key = Buffer.from(key);
|
2016-03-09 05:30:04 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
let digest = createHash('sha1').update(
|
|
|
|
Buffer.concat( [ namespaceUuid, key ] )).digest();
|
|
|
|
|
2018-04-28 12:59:07 +00:00
|
|
|
let u = Buffer.alloc(16);
|
2016-03-09 05:30:04 +00:00
|
|
|
|
|
|
|
// bbbb - bb - bb - bb - bbbbbb
|
|
|
|
digest.copy(u, 0, 0, 4); // time_low
|
|
|
|
digest.copy(u, 4, 4, 6); // time_mid
|
|
|
|
digest.copy(u, 6, 6, 8); // time_hi_and_version
|
|
|
|
|
|
|
|
u[6] = (u[6] & 0x0f) | 0x50; // version, 4 most significant bits are set to version 5 (0101)
|
|
|
|
u[8] = (digest[8] & 0x3f) | 0x80; // clock_seq_hi_and_reserved, 2msb are set to 10
|
|
|
|
u[9] = digest[9];
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
digest.copy(u, 10, 10, 16);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
return u;
|
|
|
|
}
|