Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,700 questions

57,013 answers

573 users

How to uniquely identify an array using SHA-256 hash and verify that data has not changed in C#

1 Answer

0 votes
using System;
using System.Text;
using System.Security.Cryptography;

public static class ArrayHashHelper
{
    /// <summary>
    /// Computes a SHA-256 hash for an integer array and returns
    /// it as a hexadecimal string.
    /// </summary>
    public static string GetArraySha256(int[] values)
    {
        if (values == null) {
            return String.Empty;
        }

        // Convert array to a string representation.
        string input = string.Join(",", values);

        // Convert string to bytes.
        byte[] data = Encoding.UTF8.GetBytes(input);

        // Compute SHA-256 hash.
        using (SHA256 sha256 = SHA256.Create())
        {
            byte[] hashBytes = sha256.ComputeHash(data);

            // Convert bytes to a hexadecimal string.
            StringBuilder sb = new StringBuilder();

            foreach (byte b in hashBytes) {
                sb.Append(b.ToString("x2"));
            }

            return sb.ToString();
        }
    }
    public static void Main(string[] args) {
        int[] originalData = { 1, 2, 3, 4, 5 };
        string originalHash = GetArraySha256(originalData);
        Console.WriteLine(originalHash);

        int[] currentData = { 1, 2, 3, 4, 5 };
        string currentHash = GetArraySha256(currentData);
        Console.WriteLine(currentHash);
        
        bool isSame = originalHash == currentHash;

        Console.WriteLine(isSame);
    }
}


/*
run:

b726eed184cea30d5a57fb645f77527e4232bec085e24e8d29a6e3b2c84c2db1
b726eed184cea30d5a57fb645f77527e4232bec085e24e8d29a6e3b2c84c2db1
True

*/

 



answered 3 days ago by avibootz
...