49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace BLAIzor.Helpers
|
|
{
|
|
|
|
public static class VectorHashHelper
|
|
{
|
|
/// <summary>
|
|
/// Computes a SHA-256 hash for a float vector and returns it as a Base64 string.
|
|
/// </summary>
|
|
public static string ComputeVectorHash(float[] vector)
|
|
{
|
|
if (vector == null || vector.Length == 0)
|
|
throw new ArgumentException("Vector cannot be null or empty.");
|
|
|
|
// Convert float array to byte array
|
|
byte[] byteArray = new byte[vector.Length * sizeof(float)];
|
|
Buffer.BlockCopy(vector, 0, byteArray, 0, byteArray.Length);
|
|
|
|
using (var sha = SHA256.Create())
|
|
{
|
|
byte[] hashBytes = sha.ComputeHash(byteArray);
|
|
return Convert.ToBase64String(hashBytes);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Compares two float vectors by computing and comparing their SHA-256 hashes.
|
|
/// </summary>
|
|
public static bool AreVectorsEqual(float[] vector1, float[] vector2)
|
|
{
|
|
if (vector1 == null || vector2 == null)
|
|
return false;
|
|
|
|
if (vector1.Length != vector2.Length)
|
|
return false;
|
|
|
|
string hash1 = ComputeVectorHash(vector1);
|
|
string hash2 = ComputeVectorHash(vector2);
|
|
|
|
return hash1 == hash2;
|
|
}
|
|
}
|
|
|
|
}
|