Namit Gupta
Namit Gupta

Reputation: 826

Is there any Checksum like mechanism for JSON?

I need to upload large amount of JSON data to a webservice. Whats the best way to analyse that the server received the data correctly and all data is uploaded? Please let me know if anyone has any experience in this regards. Thanks.

Upvotes: 4

Views: 10486

Answers (3)

user13601505
user13601505

Reputation:

You can calculate md5 to compare two json.

public static string CreateMD5(string json)
{
    // Use json string to calculate MD5 hash
    using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
    {
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(json);
        byte[] hashBytes = md5.ComputeHash(inputBytes);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hashBytes.Length; i++)
        {
            sb.Append(hashBytes[i].ToString("X2"));
        }
        return sb.ToString();
    }
}

Upvotes: 1

user13599419
user13599419

Reputation:

You can calculate json's hash value like this:

var sha1 = System.Security.Cryptography.SHA1.Create();
byte[] buf = System.Text.Encoding.UTF8.GetBytes(jsonString);
byte[] hash= sha1.ComputeHash(buf, 0, buf.Length); 
var hashstr = System.BitConverter.ToString(hash).Replace("-", "");

Upvotes: 1

Hidayet R. Colkusu
Hidayet R. Colkusu

Reputation: 384

You can check out my project : https://github.com/hidayetcolkusu?tab=repositories

Calculation:

ChecksumCalculator checksumCalculator = new ChecksumCalculator();

string json = @"{""Name"":""Hidayet Raşit"",""Surname"":""ÇÖLKUŞU""}";

ushort checksum = checksumCalculator.Calculate(json);
Result: 43460

Comparing:

ChecksumCalculator checksumCalculator = new ChecksumCalculator();

string json = @"{""Name"":""Hidayet Raşit"",""Surname"":""ÇÖLKUŞU""}";

bool result = checksumCalculator.Compare(json, 43460);
Resut:true

Or

ChecksumCalculator checksumCalculator = new ChecksumCalculator();

string json = @"{""Name"":""Hidayet Raşit"",""Surname"":""ÇÖLKUŞU"",""Checksum"":43460}"; 

bool result = checksumCalculator.Compare(json);
Result:true

Upvotes: 3

Related Questions