Sign text in C# with SHA256











up vote
0
down vote

favorite












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#.










share|improve this question






















  • stackoverflow.com/questions/14644926/…
    – bartonjs
    Nov 9 at 15:46










  • this did it for me, specifically the answer from Chris.J, Thanks bartonjs!
    – Chester0
    Nov 12 at 3:41















up vote
0
down vote

favorite












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#.










share|improve this question






















  • stackoverflow.com/questions/14644926/…
    – bartonjs
    Nov 9 at 15:46










  • this did it for me, specifically the answer from Chris.J, Thanks bartonjs!
    – Chester0
    Nov 12 at 3:41













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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#.










share|improve this question













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#.







c# node.js cryptography sign






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 4:30









Chester0

134




134












  • stackoverflow.com/questions/14644926/…
    – bartonjs
    Nov 9 at 15:46










  • this did it for me, specifically the answer from Chris.J, Thanks bartonjs!
    – Chester0
    Nov 12 at 3:41


















  • stackoverflow.com/questions/14644926/…
    – bartonjs
    Nov 9 at 15:46










  • this did it for me, specifically the answer from Chris.J, Thanks bartonjs!
    – Chester0
    Nov 12 at 3:41
















stackoverflow.com/questions/14644926/…
– bartonjs
Nov 9 at 15:46




stackoverflow.com/questions/14644926/…
– bartonjs
Nov 9 at 15:46












this did it for me, specifically the answer from Chris.J, Thanks bartonjs!
– Chester0
Nov 12 at 3:41




this did it for me, specifically the answer from Chris.J, Thanks bartonjs!
– Chester0
Nov 12 at 3:41












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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);
//Or
string res=Convert.ToBase64String(hash);





share|improve this answer























  • how do I read my private key with this?
    – Chester0
    Nov 9 at 5:29










  • answering my own comment: mySHA.LoadPrivateFromXml(@"C:privateKey.xml"); mySHA.LoadPublicFromXml(@"C:publicKey.xml");
    – Chester0
    Nov 9 at 5:37










  • Good to know you've solved the problem.
    – bolkay
    Nov 9 at 5:39










  • I was wrong, : still not sure how to load the key
    – Chester0
    Nov 9 at 6:08










  • This should help you.link
    – bolkay
    Nov 9 at 6:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219903%2fsign-text-in-c-sharp-with-sha256%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













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);
//Or
string res=Convert.ToBase64String(hash);





share|improve this answer























  • how do I read my private key with this?
    – Chester0
    Nov 9 at 5:29










  • answering my own comment: mySHA.LoadPrivateFromXml(@"C:privateKey.xml"); mySHA.LoadPublicFromXml(@"C:publicKey.xml");
    – Chester0
    Nov 9 at 5:37










  • Good to know you've solved the problem.
    – bolkay
    Nov 9 at 5:39










  • I was wrong, : still not sure how to load the key
    – Chester0
    Nov 9 at 6:08










  • This should help you.link
    – bolkay
    Nov 9 at 6:17















up vote
0
down vote













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);
//Or
string res=Convert.ToBase64String(hash);





share|improve this answer























  • how do I read my private key with this?
    – Chester0
    Nov 9 at 5:29










  • answering my own comment: mySHA.LoadPrivateFromXml(@"C:privateKey.xml"); mySHA.LoadPublicFromXml(@"C:publicKey.xml");
    – Chester0
    Nov 9 at 5:37










  • Good to know you've solved the problem.
    – bolkay
    Nov 9 at 5:39










  • I was wrong, : still not sure how to load the key
    – Chester0
    Nov 9 at 6:08










  • This should help you.link
    – bolkay
    Nov 9 at 6:17













up vote
0
down vote










up vote
0
down vote









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);
//Or
string res=Convert.ToBase64String(hash);





share|improve this answer














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);
//Or
string res=Convert.ToBase64String(hash);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 5:01

























answered Nov 9 at 4:52









bolkay

4228




4228












  • how do I read my private key with this?
    – Chester0
    Nov 9 at 5:29










  • answering my own comment: mySHA.LoadPrivateFromXml(@"C:privateKey.xml"); mySHA.LoadPublicFromXml(@"C:publicKey.xml");
    – Chester0
    Nov 9 at 5:37










  • Good to know you've solved the problem.
    – bolkay
    Nov 9 at 5:39










  • I was wrong, : still not sure how to load the key
    – Chester0
    Nov 9 at 6:08










  • This should help you.link
    – bolkay
    Nov 9 at 6:17


















  • how do I read my private key with this?
    – Chester0
    Nov 9 at 5:29










  • answering my own comment: mySHA.LoadPrivateFromXml(@"C:privateKey.xml"); mySHA.LoadPublicFromXml(@"C:publicKey.xml");
    – Chester0
    Nov 9 at 5:37










  • Good to know you've solved the problem.
    – bolkay
    Nov 9 at 5:39










  • I was wrong, : still not sure how to load the key
    – Chester0
    Nov 9 at 6:08










  • This should help you.link
    – bolkay
    Nov 9 at 6:17
















how do I read my private key with this?
– Chester0
Nov 9 at 5:29




how do I read my private key with this?
– Chester0
Nov 9 at 5:29












answering my own comment: mySHA.LoadPrivateFromXml(@"C:privateKey.xml"); mySHA.LoadPublicFromXml(@"C:publicKey.xml");
– Chester0
Nov 9 at 5:37




answering my own comment: mySHA.LoadPrivateFromXml(@"C:privateKey.xml"); mySHA.LoadPublicFromXml(@"C:publicKey.xml");
– Chester0
Nov 9 at 5:37












Good to know you've solved the problem.
– bolkay
Nov 9 at 5:39




Good to know you've solved the problem.
– bolkay
Nov 9 at 5:39












I was wrong, : still not sure how to load the key
– Chester0
Nov 9 at 6:08




I was wrong, : still not sure how to load the key
– Chester0
Nov 9 at 6:08












This should help you.link
– bolkay
Nov 9 at 6:17




This should help you.link
– bolkay
Nov 9 at 6:17


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219903%2fsign-text-in-c-sharp-with-sha256%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()