uuid3-by-string
Version:
Generating uuid-hash from string
37 lines (28 loc) • 1.02 kB
JavaScript
var lib = require('./lib');
/** Uin8Array with zero items */
var EMPTY_UINT8_ARRAY = new Uint8Array(0);
/**
* Generates the Name-Based UUID hashes v3 and v5 according to RFC-4122
* https://tools.ietf.org/html/rfc4122#section-4.3
* @param {string} target Hashing target
* @param {string} [namespace] Some name space within which generation occurs
* @returns {string} UUID
*/
function generateUuid(target, namespace) {
if (typeof target !== 'string') {
throw TypeError('Value must be string');
}
// Parsing target chars
var charBuffer = lib.stringToCharBuffer(target);
// TODO: Test namespace for uuid and parse to buffer
var namespaceCharBuffer = typeof namespace === 'string' ? lib.stringToCharBuffer(namespace) : EMPTY_UINT8_ARRAY;
// Concatenation two buffers of strings to one
var buffer = lib.concatBuffers(namespaceCharBuffer, charBuffer);
// Getting hash
var hash = lib.md5Hash(buffer)
return lib.hashToUuid(hash, 3);
}
/**
* Export module
*/
module.exports = generateUuid;