using System.Text.RegularExpressions;
using System.Globalization;
using System.Security.Cryptography;
private static SHA256 shaEncryptor = SHA256.Create();
private static string CalculateSha2Hash(string input)
var utf8Encoder = new UTF8Encoding(true);
var hashedBytes = shaEncryptor.ComputeHash(utf8Encoder.GetBytes(input));
return hashedBytes.Aggregate(string.Empty, (current, item) => current + item.ToString("X2", CultureInfo.InvariantCulture)).ToLowerInvariant();
public static void Main()
string message = "hello 8:narinderberi this is a message";
string pattern = @"(8:[^: ]+)( |$)";
MatchCollection matches = Regex.Matches(message, pattern);
foreach (Match match in matches)
GroupCollection groups = match.Groups;
Console.WriteLine("User Id Captured: <" + groups[1] + ">");
message = message.Substring(0, groups[1].Index) + CalculateSha2Hash(groups[1].ToString()) + message.Substring(groups[1].Index + groups[1].Length);
Console.WriteLine(message);