How can I generate a GUID for a string?

后端 未结 9 1725
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:37

    Here is my own approach, I'm intentionally using String to hex dump if possible - visually it can be seen at least how big string is, and if needed - decoded using some online hex converter. But if string is too long (more than 16 bytes) - then using sha-1 to compute hash and generate guid from it.

    /// <summary>
    /// Generates Guid based on String. Key assumption for this algorithm is that name is unique (across where it it's being used)
    /// and if name byte length is less than 16 - it will be fetched directly into guid, if over 16 bytes - then we compute sha-1
    /// hash from string and then pass it to guid.
    /// </summary>
    /// <param name="name">Unique name which is unique across where this guid will be used.</param>
    /// <returns>For example "{706C7567-696E-7300-0000-000000000000}" for "plugins"</returns>
    static public String GenerateGuid(String name)
    {
        byte[] buf = Encoding.UTF8.GetBytes(name);
        byte[] guid = new byte[16];
        if (buf.Length < 16)
        {
            Array.Copy(buf, guid, buf.Length);
        }
        else
        {
            using (SHA1 sha1 = SHA1.Create())
            {
                byte[] hash = sha1.ComputeHash(buf);
                // Hash is 20 bytes, but we need 16. We loose some of "uniqueness", but I doubt it will be fatal
                Array.Copy(hash, guid, 16);
            }
        }
    
        // Don't use Guid constructor, it tends to swap bytes. We want to preserve original string as hex dump.
        String guidS = "{" + String.Format("{0:X2}{1:X2}{2:X2}{3:X2}-{4:X2}{5:X2}-{6:X2}{7:X2}-{8:X2}{9:X2}-{10:X2}{11:X2}{12:X2}{13:X2}{14:X2}{15:X2}", 
            guid[0], guid[1], guid[2], guid[3], guid[4], guid[5], guid[6], guid[7], guid[8], guid[9], guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]) + "}";
    
        return guidS;
    }
    
    0 讨论(0)
  • 2020-12-02 14:38

    Guids are random, they are not intrinsically assigned to any string or other value.

    If you need such linking, store the guids in a Dictionary and check for an existing guid first before creating a new one.

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

    What you are looking for is probably generating version 3 or version 5 UUIDs, which are name based UUIDs. (version 5 is the recommended). I don't think that the .NET framework has build in support for it. See http://en.wikipedia.org/wiki/Universally_Unique_Identifier

    I did a few google searches to see if I could find something in the Win32 API, but nothing came up. However, I am sure that the .NET framework has some implementation hidden somewhere, because as far as I know, when generating a COM object in .NET, and you don't supply an explicit GUID, then the .NET framework generates a name based UUID to create a well-defined ClassID and InterfaceID, i.e. UUIDs that don't change every time you recompile (like VB6). But this is probably hidden, so I guess you need to implement the algorithm yourself. Luckily, .NET provides both an MD5 and SHA1 algorithm so I don't think implementing a version3 and version5 UUID should be too difficult.

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

    I'm fairly sure you've confused System.Guid with wanting a hash (say, SHA-256) of a given string.

    Note that, when selecting a cryptographically-secure hashing algorithm, MD5, SHA0 and SHA1 are all generally considered dead. SHA2 and up are still usable.

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

    You cannot use GUID that way. The constructor of Guid expects a valid, string representation of a Guid.

    What you're looking for is called a Hash function. (for example: MD5)

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

    I think you have a misunderstanding of what a Guid actually is. There is no Guid representation of a string such as "Mehar".

    The reason there is a new Guid(String s) overload is so that you can create a guid from a typical string representation of one, such as "00000000-0000-0000-0000-000000000000".

    See the wiki article for more information on what a Guid actually is.

    http://en.wikipedia.org/wiki/Globally_Unique_Identifier

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