Sign text in C# with SHA256

3055 views c#
4

I have some code in nodejs and I am trying to replicate in in C# but I can't seem to find a simple solution like the nodejs one:

var crypto = require('crypto');

var privateKey = '-----BEGIN RSA PRIVATE KEY-----\n'+
      /// key removed
'-----END RSA PRIVATE KEY-----';

var publicKey = '-----BEGIN PUBLIC KEY-----\n'+
      /// key removed
'-----END PUBLIC KEY-----';

// sign string
var signer = crypto.createSign('sha256');
signer.update('ugus-dev/logo.png');
var sign = signer.sign(privateKey,'base64');
console.log(sign);

// verify signature 
var verifier = crypto.createVerify('sha256');
verifier.update('ugus-dev/logo.png');
var ver = verifier.verify(publicKey, sign,'base64'); 

console.log(ver); // true if signature matches.

Basically i will be singing a string in C# and then verifying the signature of the string in nodeJS with the obove code for verify.

So I am looking for an equvalent of:

// sign string
var signer = crypto.createSign('sha256');
signer.update('ugus-dev/logo.png');
var sign = signer.sign(privateKey,'base64');

in C#.

answered question

1 Answer

7

A head start:

using System.Security.Cryptography;
using System.Text;

SHA256 mySHA=SHA256.Create();
string myText="yourText";
byte[] byteArr=Encoding.UTF8.GetBytes(myText);
byte[] hash=mySHA.ComputeHash(byteArr);

string res=BitConverter.ToString(hash);

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories