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
*/