Is there a way to generate a Guid from a list of Guids?

1k views Asked by At

I have a list of objects they have guids for ids. I want to use the ids in this list of objects to create a guid that I can use ensure that the list has not changed. If an object was removed/added the guid would be different. The thing that comes to mind is using a hash. Maybe I should just use a hash, but I am wondering if it is possible that this could be faster than generating a hash?

EDIT: I am getting this list from a stored procedure. Then storing the list of objects into memory cache. Each user is going to validate their local value in a cookie against this generated value to ensure that the list is still the same.

2

There are 2 answers

0
Alexei Levenkov On BEST ANSWER

When you need to know if object is exactly the same as before you can't rely on hashing alone - hash of an object allows you to definitely tell that objects are different, but it does not guarantee that objects are the same.

Generally there are several approaches to solve that:

  • frequently small rate of potential collisions on hash values is acceptable and you can just use hash that long enough to satisfy your requirement. Usually crypto-hash functions like SHA256 provide low enough collision rate to just live with it. Such value easily will fit in HTTP cookie if needed.
  • if objects are small enough (i.e. several K) sending whole object to client may be an option (will give you chance to perform exact comparison on postbacks).
  • if you can clearly version objects in some way that is guaranteed to be unique (modified date, current sequential update with auto-increment) for particular object you can keep just that piece of information. Usually such versioning information is small and easy to send in cookie.

Note: depending on your requirement you may need to encrypt/sign cookie values (possibly with salt) to prevent client code from tampering with your cookie values.

6
Daniel A. White On

You can write your own class that accesses the list or hashset or any other collection backing this:

class GuidList // You can implement IReadOnlyList
{
    public bool Dirty { get; private set;}
    // Omit if implementing `IReadOnlyList`
    public Guid[] Guids { get { return guidCollection.ToArray() }
    public void Add(Guid guid) // Do similarly for remove
    {
        Dirty = true;
        // implement logic
    }

}

This is a form of information hiding.