开发者

Generating a non-guid unique key outside of a database

I have a situation where I need to create some kind of uniqueness between 'entities', but it is not a GUID, and it is not saved in a database (It is saved, however. Just no开发者_开发问答t by a database).

The basic use of the key is a mere redundancy check. It does not have to be as scalable as a real 'primary key', but in the simplest terms I can think of , this is how it works.

[receiver] has List<possibilities>.

possibilities exist independently, but many will have the same values (impossible to predict. This is by design)

Frequently, the receivers list of possibilities will have to be emptied and then refilled (this is a business requirement).

The key is basically used to add a very lightweight redundancy check. In other words, sometimes the same possibility will be repeated, sometimes it should only appear once in the receiver's list.

I basically want to use something very light and simple. A string is sufficient. I was just wanting to figure out a modest algorithm to accomplish this. I thought about using the GetHashCode() method, but I am not certain about how reliable that is. Can I get some thoughts?


If you can use GetHashCode() at a first glance, you can probably use an MD5 hash as well, obtaining less collision probability. The resulting MD5 can be stored as a 24 charachter string by encoding it base 64, let see this example:

    public static class MD5Gen
    {
        static MD5 hash = MD5.Create();
        public static string Encode(string toEncode)
        { 
            return Convert.ToBase64String(
            hash.ComputeHash(Encoding.UTF8.GetBytes(toEncode)));
        }
    }

with this you encode a source string in an md5 hash in string format too. You just have to write the "possibility" class in term of string.


Try this for generating Guid.

VBScript Function to Generate a UUID/GUID

If you are on Windows, you can use the simple VBScript below to generate a UUID. Just save the code to a file called createguid.vbs, and then run cscript createguid.vbs at a command prompt.

Set TypeLib = CreateObject("Scriptlet.TypeLib")
NewGUID = TypeLib.Guid
WScript.Echo(left(NewGUID, len(NewGUID)-2))
Set TypeLib = Nothing

Create a UUID/GUID via the Windows Command Line

If you have the Microsoft SDK installed on your system, you can use the utility uuidgen.exe, which is located in the "C:\Program Files\Microsoft SDK\Bin" directory

or try the same for more info.

Link

I would say go for the Windows command line as it is more reliable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜