Create a simple hash value of database columns using C # .net

I would like to keep a simple hash value of n number of columns, for example. address and name, and save the hash in another column. I want to use a hash value as a quick way to synchronize data between two sources by simply comparing the hash value. What is the best way to do something like this. I do not need crypto function to create a hash.

eg.

John Smith, 1 Long Lane Road, Village, City, Zip / Postal Code. Hash: AK67KS38

I would like the hash value to be easy enough to read (not the whole set of Uni code).

Edit Okay, I would still like to do this in C # and LINQ code. Data is entered externally by interrogating other sources. There is no other way to relate this unambiguously to my data, since there is no “key” as such from an external source for reference. Therefore, in this sense, the value of the timestamp will not be an option. I understand that this method is not "accurate", but I can live with it. I could add the ability to manually view possible hash matches and push them further into the database if they are acceptable.

+3
source share
2 answers

- ( ). UTC . , concurrency .., /diff, .

, - "", , . .

[] , , GetHashCode() Equals() . google, , . (2- google) :

http://www.eggheadcafe.com/community/aspnet/2/78458/hashcode.aspx

( , [, resharper: -)]):

public class ContactDetails
{
    public string Name { get; set; }
    public string Address { get; set; }
    public string Village { get; set; }
    public string Town { get; set; }
    public string PostCode { get; set; }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != typeof (ContactDetails)) return false;
        return Equals((ContactDetails) obj);
    }

    public bool Equals(ContactDetails other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Equals(other.Name, Name) && Equals(other.Address, Address);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            return ((Name != null ? Name.GetHashCode() : 0)*397) 
                ^ (Address != null ? Address.GetHashCode() : 0);
        }
    }
}

:

bool isChanged = ContactFromServer1.Equals(ContactFromServer2);
//etc..

,

+2

, ():

DataContractSerializer serializer = new DataContractSerializer(typeof(User));
MemoryStream memoryStream = new MemoryStream();
XmlWriter writer = XmlDictionaryWriter.CreateBinaryWriter(memoryStream);
serializer.WriteObject(memoryStream, user);
byte[] serializedData = memoryStream.ToArray();

// Calculte the serialized data hash value
SHA1CryptoServiceProvider SHA = new SHA1CryptoServiceProvider();
byte[] hash = SHA.ComputeHash(serializedData);

// Convert the hash to a base 64 string
string hashAsText = Convert.ToBase64String(hash);

, 64- MD5,

0

Source: https://habr.com/ru/post/1792766/


All Articles