How can I generate a GUID for a string?

后端 未结 9 1726
Happy的楠姐
Happy的楠姐 2020-12-02 14:14

I am having a problem generating a GUID for a string - for example:

Guid g = New Guid(\"Mehar\");

How can I compute a GUID for \"Meha

相关标签:
9条回答
  • 2020-12-02 14:54

    If op's intent is to create a UUID (Guid) from a string hash of some sort (MD5, SHA-1, et.c.), I found this very similar question with this great answer:

    https://stackoverflow.com/a/5657517/430885

    It has a link to a github-snippet based on RFC 4122 §4.3, that will create a Guid from a string and a namespace (which you can choose for yourself to guarantee against collisions from outside environments).

    Direct link to the snippet: https://github.com/LogosBible/Logos.Utility/blob/master/src/Logos.Utility/GuidUtility.cs

    0 讨论(0)
  • 2020-12-02 14:56

    Quite old this thread but this is how we solved this problem:

    Since Guid's from the .NET framework are arbitrary 16bytes, or respectively 128bits, you can calculate a Guid from arbitrary strings by applying any hash function to the string that generates a 16 byte hash and subsequently pass the result into the Guid constructor.

    We decided to use the MD5 hash function and an example code could look like this:

    string input = "asdfasdf";
    using (MD5 md5 = MD5.Create())
    {
        byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(input));
        Guid result = new Guid(hash);
    }
    

    Please note that this Guid generation has a few flaws by itself as it depends on the quality of the hash function! If your hash function generates equal hashes for lots of string you use, it's going to impact the behaviour of your software.

    Here is a list of the most popular hash functions that produce a digest of 128bit:

    • RIPEMD (probability of collision: 2^18)
    • MD4 (probability of collision: for sure)
    • MD5 (probability of collision: 2^20.96)

    Please note that one can use also other hash functions that produce larger digests and simply truncate those. Therefore it may be smart to use a newer hash function. To list some:

    • SHA-1
    • SHA-2
    • SHA-3

    Today (Aug 2013) the 160bit SHA1 hash can be considered being a good choice.

    0 讨论(0)
  • 2020-12-02 14:59

    In general there are few ways to make an universally unique ID (UUID RFC 4122, a.k.a. GUID). We could borrow these four from Python, and make in C# something alike:

    uuid.uuid1([node[, clock_seq]])

    Generate a UUID from a host ID, sequence number, and the current time. If node is not given, getnode() is used to obtain the hardware address. If clock_seq is given, it is used as the sequence number; otherwise a random 14-bit sequence number is chosen.

    uuid.uuid3(namespace, name)

    Generate a UUID based on the MD5 hash of a namespace identifier (which is a UUID) and a name (which is a string).

    uuid.uuid4()

    Generate a random UUID.

    uuid.uuid5(namespace, name)

    Generate a UUID based on the SHA-1 hash of a namespace identifier (which is a UUID) and a name (which is a string).

    So if you need ID of a string as an object, not ID of a value, you should mangle your private UUID with given string, Your private UUID generate once using uuid1, and then use it as namespace for uuid3 or uuid5.

    These variants and versions described on Wikipedia Universally_unique_identifier#Variants_and_versions

    0 讨论(0)
提交回复
热议问题