How can I generate a GUID for a string?

后端 未结 9 1735
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条回答
  •  Happy的楠姐
    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

提交回复
热议问题