如何创建确定性的GUID [英] How to Create Deterministic Guids

查看:178
本文介绍了如何创建确定性的GUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用中,我们正在创建XML文件,有一个GUID值的属性。需要此值是文件升级之间是一致的。因此,即使其他一切文件更改,该属性的GUID值应保持不变。

一个显而易见的解决方案是创建一个静态的字典文件名和GUID来使用它们。然后,每当我们生成的文件,我们查字典的文件名,并使用相应的GUID。但是,这是不可行的,因为我们可能会扩展到100的文件,并没有对希望保持GUID的大名单。

所以,另一种方法是使的GUID相同的基于文件的路径。由于我们的文件路径和应用程序的目录结构是唯一的,将GUID应该是唯一的这条道路。所以我们每次运行升级时,该文件获取根据其路径相同的GUID。我发现一个冷静的方式来产生这样的<一个href="http://geekswithblogs.net/EltonStoneman/archive/2008/06/26/generating-deterministic-guids.aspx">Deterministic GUIDS (感谢艾尔顿·斯通曼)。它基本上做到这一点:

 私人的Guid GetDeterministicGuid(字符串输入)

{

//使用MD5哈希来获取字符串的16字节的散列:

MD5CryptoServiceProvider提供商=新MD5CryptoServiceProvider();

byte []的inputBytes = Encoding.Default.GetBytes(输入);

byte []的HASHBYTES = provider.ComputeHash(inputBytes);

//生成的散列GUID:

GUID hashGuid =新的GUID(HASHBYTES);

返回hashGuid;

}
 

于是给定的字符串,所述的Guid将始终是相同的。

还有没有其他办法或建议的方式来这样做呢?什么是该方法的优点和缺点是什么?

解决方案

正如刚才@bacar, RFC 4122 第4.3节定义了一种方式来创建一个基于名称的UUID。这样做(超过只是使用MD5哈希)的优点是,这些都保证不冲突而将非命名为基础的UUID,并有一个非常(非常)小的可能性与其它基于域名的UUID碰撞。

有在.NET Framework创建这些没有原生支持,但是我贴的实现算法> code。它可以用作如下:

 的Guid GUID = GuidUtility.Create(GuidUtility.UrlNamespace,文件路径);
 

要进一步减少冲突与其他的GUID的风险,你可以创建一个专用的GUID命名空间ID(而不是使用在RFC定义的URL命名空间ID)来使用。

In our application we are creating Xml files with an attribute that has a Guid value. This value needed to be consistent between file upgrades. So even if everything else in the file changes, the guid value for the attribute should remain the same.

One obvious solution was to create a static dictionary with the filename and the Guids to be used for them. Then whenever we generate the file, we look up the dictionary for the filename and use the corresponding guid. But this is not feasible because we might scale to 100's of files and didnt want to maintain big list of guids.

So another approach was to make the Guid the same based on the path of the file. Since our file paths and application directory structure are unique, the Guid should be unique for that path. So each time we run an upgrade, the file gets the same guid based on its path. I found one cool way to generate such 'Deterministic Guids' (Thanks Elton Stoneman). It basically does this:

private Guid GetDeterministicGuid(string input) 

{ 

//use MD5 hash to get a 16-byte hash of the string: 

MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider(); 

byte[] inputBytes = Encoding.Default.GetBytes(input); 

byte[] hashBytes = provider.ComputeHash(inputBytes); 

//generate a guid from the hash: 

Guid hashGuid = new Guid(hashBytes); 

return hashGuid; 

} 

So given a string, the Guid will always be the same.

Are there any other approaches or recommended ways to doing this? What are the pros or cons of that method?

解决方案

As mentioned by @bacar, RFC 4122 §4.3 defines a way to create a name-based UUID. The advantage of doing this (over just using a MD5 hash) is that these are guaranteed not to collide with non-named-based UUIDs, and have a very (very) small possibility of collision with other name-based UUIDs.

There's no native support in the .NET Framework for creating these, but I posted code on GitHub that implements the algorithm. It can be used as follows:

Guid guid = GuidUtility.Create(GuidUtility.UrlNamespace, filePath);

To reduce the risk of collisions with other GUIDs even further, you could create a private GUID to use as the namespace ID (instead of using the URL namespace ID defined in the RFC).

这篇关于如何创建确定性的GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆