制作一个UUID(rfc4122)用于哈希完整内容的命名空间?

我正在学习编写博客软件,所以我阅读了需要唯一ID的Atom。 似乎你应该做的是一个urn:uuid:type IRI。

得到一个全球唯一的标识符对我来说是有意义的,就是对这个post的内容进行散列。

我已经编写了代码(见下面)来生成符合rfc-4122标准的UUID(版本5),除了我不确定要为命名空间添加什么内容。

RFC表示说我们应该使用什么名字空间,而不是说它的范围,并且显示了一些例子,其中没有一个是正确的。 我GOOGLE了一下,没有看到任何超出RFC的build议。

命名空间本身应该是一个UUID,所以我不应该把“整个post的散列”当作命名空间。

要生成一个版本5 uuid的[除6位以外的所有内容],可以将名称空间UUID(原始格式)与您的“名称”连接起来。

所以…这是我的问题:是否有一个名称空间UUID使用文档的全部内容作为“名称”?

或者我应该随机(v4)UUID,并使用它作为我自己的“整个职位”命名空间?

或者是其他东西?

谢谢,杰森

PS我写了一个UUID生成器的节点,现在使用ns:URL命名空间。 如果您有兴趣,请input以下代码:

// Copyright 2011 Jason Woofenden -- CC0 // // An almost correct rfc-4122 v5 UUID generator for node (see http://node.js) // // To test, run this with node, then compare the out put of these: // // curl http://localhost:8129/foo // uuid -v 5 ns:URL foo // // Replace "foo" with any string and they should still be the same. var http = require('http'), crypto = require('crypto'), url = require('url'); hex_high_10 = { // set the highest bit and clear the next highest '0': '8', '1': '9', '2': 'a', '3': 'b', '4': '8', '5': '9', '6': 'a', '7': 'b', '8': '8', '9': '9', 'a': 'a', 'b': 'b', 'c': '8', 'd': '9', 'e': 'a', 'f': 'b' } http.createServer(function (req, res) { var sum = crypto.createHash('sha1'); // namespace in raw form. FIXME using ns:URL for now, what should it be? sum.update(new Buffer('a6e4EZ2tEdGAtADAT9QwyA==', 'base64')); // add HTTP path sum.update(url.parse(req.url).pathname.substr(1)); // get sha1 hash in hex form var uuid = sum.digest('hex'); // format as UUID (add dashes, version bits and reserved bits) uuid = uuid.substr(0, 8) + '-' + // time_low uuid.substr(8, 4) + '-' + // time_mid '5' + // time_hi_and_version high 4 bits (version) uuid.substr(13, 3) + '-' + // time_hi_and_version low 4 bits (time high) hex_high_10[uuid.substr(16, 1)] + uuid.substr(17, 1) + // cloc_seq_hi_and_reserved uuid.substr(18, 2) + '-' + // clock_seq_low uuid.substr(20, 12); // node // spit it out res.writeHead(200, {'Content-Type': 'text/plain'}); res.end(uuid + '\n'); }).listen(8129, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8129/'); 

前一段时间我遇到过同样的问题,得出的结论是使用v4 UUID作为命名空间是正确的。 基本上我想从一个string生成一个UUID,这就是我所做的(在Java中,但它很简单,可以翻译成JS):

 public final class FooIdGen { /** * The namespace id for generating Foo - UUIDs from the foo - id * strings. */ public final static String NAMESPACE = "0416141a-5229-4d16-94cc-43d546ef1118"; //NOI18N private final static byte[] NS_BYTES = uuidToBytes(UUID.fromString(NAMESPACE)); /** * Generates a UUID for a given foo - id. * * @param fooId the reporter ID to get the UUID for * @return the UUID for the specified foo ID */ public static UUID uuidForFooId(String fooId) { final byte[] idBytes; try { idBytes = fooId.getBytes("US-ASCII"); //NOI18N } catch (UnsupportedEncodingException ex) { /* pretty sure US-ASCII is ok, so this can't happen */ throw new AssertionError(ex.toString()); } final byte[] tmp = Arrays.copyOf( NS_BYTES, idBytes.length + NS_BYTES.length); System.arraycopy(idBytes, 0, tmp, NS_BYTES.length, idBytes.length); return UUID.nameUUIDFromBytes(tmp); } /* want it to align, so that's ok */ @SuppressWarnings("PointlessBitwiseExpression") private static byte[] uuidToBytes(UUID id) { final long h = id.getMostSignificantBits(); final long l = id.getLeastSignificantBits(); final byte[] result = new byte[16]; int i=0; result[i++] = (byte) ((h >> 56) & 0xff); result[i++] = (byte) ((h >> 48) & 0xff); result[i++] = (byte) ((h >> 40) & 0xff); result[i++] = (byte) ((h >> 32) & 0xff); result[i++] = (byte) ((h >> 24) & 0xff); result[i++] = (byte) ((h >> 16) & 0xff); result[i++] = (byte) ((h >> 8) & 0xff); result[i++] = (byte) ((h >> 0) & 0xff); result[i++] = (byte) ((l >> 56) & 0xff); result[i++] = (byte) ((l >> 48) & 0xff); result[i++] = (byte) ((l >> 40) & 0xff); result[i++] = (byte) ((l >> 32) & 0xff); result[i++] = (byte) ((l >> 24) & 0xff); result[i++] = (byte) ((l >> 16) & 0xff); result[i++] = (byte) ((l >> 8) & 0xff); result[i++] = (byte) ((l >> 0) & 0xff); return result; } private FooIdGen() { /* no instances */ } } 

下面是一些CoffeeScript代码,用于使用Sha-1哈希值生成v4 UUID。 (使用http://js2coffee.org转换成Javascript。)

 # Compute a uuid v4 from the Sha-1 hash of data. crypto = require('crypto') exports.uuidsha1 = (data) -> sha1sum = crypto.createHash('sha1') sha1sum.update(data) s = sha1sum.digest('hex') i = -1 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) -> i += 1 switch c when 'x' return s[i] when 'y' # take 8 + low order 3 bits of hex number. return ((parseInt('0x'+s[i],16)&0x3)|0x8).toString(16) ) 

你可能想看看node-uuid。 我还没有使用它,但是当我需要这种types的function时,我打算仔细观察它。 这可能不适合你的需求,但我认为你应该知道这一点。 https://github.com/broofa/node-uuid