1

I'm not sure if I have the terminology correct but I am looking at creating a Checksum / Hash Value from a List of custom objects based on a single Integer value which is technically the primary key in the database.

The use case is the List of custom objects holds is essentially a monitor of various events which are in progress. These events are sent to the GUI program which displays them nicely on the screen. Best way to think of it is as a list of events in progress.

Occasionally I have the issue where the message does not make it to the GUI due to network issues so I'm looking at creating some sort of identifying value which represents the state of the system. This can then be sent down to the GUI with each message and if this value does not match what the GUI has stored, then the GUI can request a refresh list of events.

Any guidance would be appreciated.

UPDATE

Further to this I thought Id provide some samples of what Im trying to achieve to clarify.

My Base Class might be as follows:-

public class BaseEvent {

    public DateTime EventDate { get;set;}
    public EventName EventName { get;set;}
    public int EventID { get;set;}

}

The underlying service and GUI would have a collection with the service being the "master"

List<BaseEvent> _eventList = new List<BaseEvent>();

One the service the events would be generated and added to the list (sample below - not how it would happen in real world scenario)

_eventList.Add(new BaseEvent() { EventID=1, EventName="Event 1", EventDate=DateTime.Now };

_eventList.Add(new BaseEvent() { EventID=2, EventName="Event 2", EventDate=DateTime.Now };

_eventList.Add(new BaseEvent() { EventID=3, EventName="Event 3", EventDate=DateTime.Now };

As each of the events is being received / processed by the server, they would be sent out the socket to all GUI clients (all classes are serializable)

Every time an Event is Added to or removed from the list, the server will store the current hash in a variable.

public class SocketData {

    public BaseEvent CurrentEvent { get;set;}

    public int CurrentHash { get;set;}

}

So based on the above and a very basic hash of adding the EventIDs together, the following would happen

When Event 1 is sent the Current Hash would be 1
When Event 2 is sent the Current Hash would be 3 ( 1 + 2 )
When Event 3 is sent the Current Hash would be 6 ( 1 + 2 + 3 )

So the goal would be to create a mechanism that would guarantee the "hash" of the System Event ID for all items in the service list would be unique.

Then at the GUI end the goal is when a message is received from the service, it checks the hash value received in the message and compares with its own hash and if they dont match, it will request the events from the service.

Hopefully this clarifies what I am trying to achieve.

THanks.

  • You could simply sum the integers and mod by a suitably high number to get a "hash" you could check both ends of the pipe. – Chris Pickford Jul 09 '19 at 09:29
  • Perhaps override gethash code https://learn.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=netframework-4.8 or implement the iequatable interface https://learn.microsoft.com/en-us/dotnet/api/system.iequatable-1?view=netframework-4.8 – B. Lec Jul 09 '19 at 09:34
  • What is the goal of this hash? I ask because if you need a unique value that identifies a row, and you already have a primary key value that is a number, can't you simply use that value, unmodified? Or did you mean 1 hash from the list of integers? So that if you add, or remove, one of those integers the hash changes? Does order matter? – Lasse V. Karlsen Jul 09 '19 at 09:39

1 Answers1

0

You should take a look at MD5 checksum. You can ComputeHash for any stream, so if you serialize your list into a stream you'll be able to compute the hash.

byte[] GetMD5Checksum(List<int> list)
{
    var binaryFormatter = new BinaryFormatter();
    using(var stream = new MemoryStream())
    using (var md5 = MD5.Create())
    {
        binaryFormatter.Serialize(memoryStream, list);
        return md5.ComputeHash(stream);
    }
}
V0ldek
  • 9,623
  • 1
  • 26
  • 57