From d46406c3fd9975091b1ecf82b68ad2d063f99cba Mon Sep 17 00:00:00 2001 From: Ionite Date: Mon, 13 Nov 2023 14:37:13 -0500 Subject: [PATCH] Made ObjectHash static, add GetStringSignature --- StabilityMatrix.Core/Helper/ObjectHash.cs | 26 +++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/StabilityMatrix.Core/Helper/ObjectHash.cs b/StabilityMatrix.Core/Helper/ObjectHash.cs index f29bc35a..53db6a37 100644 --- a/StabilityMatrix.Core/Helper/ObjectHash.cs +++ b/StabilityMatrix.Core/Helper/ObjectHash.cs @@ -1,9 +1,10 @@ -using System.Text; +using System.Security.Cryptography; +using System.Text; using System.Text.Json; namespace StabilityMatrix.Core.Helper; -public class ObjectHash +public static class ObjectHash { /// /// Return a GUID based on the MD5 hash of the JSON representation of the object. @@ -16,4 +17,25 @@ public class ObjectHash var hash = md5.ComputeHash(bytes); return new Guid(hash); } + + /// + /// Return a short Sha256 signature of a string + /// + public static string GetStringSignature(string? str) + { + if (str is null) + { + return "null"; + } + + if (string.IsNullOrEmpty(str)) + { + return ""; + } + + var bytes = Encoding.UTF8.GetBytes(str); + var hash = Convert.ToBase64String(SHA256.HashData(bytes)); + + return $"[..{str.Length}, {hash[..7]}]"; + } }