TheProgrammer
TheProgrammer

Reputation: 1344

Convert string to Guid using SHA256 without truncating

Is there a way to convert a string to a GUID using SHA256 without truncating the 16 bytes?

Currently I have this:

using SHA256 sha2 = SHA256.Create())
{
     var hash = sha2.ComputeHash(Encoding.Default.GetBytes(string));
     return new Guid(hash.Take(16).ToArray());
}

Upvotes: 3

Views: 3810

Answers (1)

McAden
McAden

Reputation: 13970

A hash is not the same as a Guid. Trying to equate the 2 is incorrect.

If you want a unique identifier:

return Guid.NewGuid();

That'll give you one.

If you want the hash, store is as bytes or a string, not a Guid

Upvotes: 0

Related Questions