What we want to convert is the Base64 string in the Modulus node, so the first step is to extract it and then convert it.
static void Main(string[] args)
{
using (var provider = new RSACryptoServiceProvider(2048))
{
//
var publicKey = provider.ToXmlString(false);
XmlDocument xd = new XmlDocument();
xd.LoadXml(publicKey);
string base64PK = xd.SelectSingleNode("descendant::Modulus").InnerText;
byte[] bytes = Convert.FromBase64String(base64PK);
string hex = BitConverter.ToString(bytes);
string hash = ComputeSha256Hash(base64PK);
Console.WriteLine();
}
}
static string ComputeSha256Hash(string rawData)
{
// Create a SHA256
using (SHA256 sha256Hash = SHA256.Create())
{
// ComputeHash - returns byte array
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
// Convert byte array to a string
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("X2"));
}
return builder.ToString();
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.