diff --git a/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs b/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs index ce4e010bf..d01c30199 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Options; +using System.Security.Cryptography; using System.Text.Json; using System.Text.RegularExpressions; @@ -186,4 +187,25 @@ public static string ConvertToString(this T? value, JsonSerializerOptions? js var str = JsonSerializer.Serialize(value, jsonOptions); return str; } + + /// + /// Get MD5 hash of a string + /// + /// + /// + public static string GetMd5Hash(this string text) + { + using MD5 md5 = MD5.Create(); + byte[] inputBytes = Encoding.UTF8.GetBytes(text); + byte[] hashBytes = md5.ComputeHash(inputBytes); + + // Convert byte array to a 32-character hex string + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < hashBytes.Length; i++) + { + sb.Append(hashBytes[i].ToString("x2")); // "x2" for lowercase hex + } + + return sb.ToString(); + } }