Skip to content

Commit 712bfd7

Browse files
authored
Merge pull request #1314 from hchen2020/master
Add GetMd5Hash to StringExtensions
2 parents 2d9d0c9 + f27539f commit 712bfd7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Options;
2+
using System.Security.Cryptography;
23
using System.Text.Json;
34
using System.Text.RegularExpressions;
45

@@ -186,4 +187,25 @@ public static string ConvertToString<T>(this T? value, JsonSerializerOptions? js
186187
var str = JsonSerializer.Serialize(value, jsonOptions);
187188
return str;
188189
}
190+
191+
/// <summary>
192+
/// Get MD5 hash of a string
193+
/// </summary>
194+
/// <param name="text"></param>
195+
/// <returns></returns>
196+
public static string GetMd5Hash(this string text)
197+
{
198+
using MD5 md5 = MD5.Create();
199+
byte[] inputBytes = Encoding.UTF8.GetBytes(text);
200+
byte[] hashBytes = md5.ComputeHash(inputBytes);
201+
202+
// Convert byte array to a 32-character hex string
203+
StringBuilder sb = new StringBuilder();
204+
for (int i = 0; i < hashBytes.Length; i++)
205+
{
206+
sb.Append(hashBytes[i].ToString("x2")); // "x2" for lowercase hex
207+
}
208+
209+
return sb.ToString();
210+
}
189211
}

0 commit comments

Comments
 (0)