cryptography - How to create a digital signature using SHA256 with ECDSA algorithm in C# -


i have requirement create signature url-safe-base-64-encoded sha256+ecdsa signature of message. used consume remote rest service.

i have been given hal browser implementation connects them expected , test implementation done in scala.

    val token = generatetoken() // generates random numeric token, different each request     val lines = line1 + "\n" + line2 + "\n" + line3 + "\n"     val linebytes = lines.getbytes()     try {         var sig = signature.getinstance("sha256withecdsa")         sig.initsign(privatekey)         sig.update(linebytes)         body.foreach { input => // if there body, sign             input.reset()             var bytes = new array[byte](1024)             while (input.available() > 0) {                 val alloc = input.read(bytes)                 sig.update(bytes, 0, alloc)             }         }         val encoder = new base64(true)         val sigstring = encoder.encodeasstring(sig.sign()).replace("\r\n", "")         val headerval = "authentication.scheme.signed" + " username=" + username + "&token=" + token + "&signature=" + sigstring          request.addheader("authorization", headerval)     } catch {         case e : nosuchalgorithmexception =>             throw new error("no support sha256withecdsa! check java installation.")     } 

i trying generate same signature using c#.

so far signing method looks like

private byte[] signdata(byte[] hashedmessagetosign) {     cngkey pkey2 = cngkey.open(@"c:\openssl-win64\bin\myprivateikeyinpkcs8format.pem");      using (ecdsacng dsa = new ecdsacng(pkey2))     {         //dsa.hashalgorithm = cngalgorithm.ecdsap256;         //bob.key = dsa.key.export(cngkeyblobformat.eccpublicblob);          byte[] data = hashedmessagetosign;          return dsa.signdata(data);     } } 

i getting code building creating invalid signature. here calling method

        protected void btnldiscover_click(object sender, eventargs e) {     httpwebrequest request = webrequest.create("https://service.provider/path/") httpwebrequest;     request.method = "get";     request.contenttype = "application/bespoke.format+json; version=1";     //request.date = new datetime(2015, 9, 3, 10, 40, 48);     request.date = new datetime(2015, 9, 21, datetime.now.hour, datetime.now.minute, datetime.now.second);     request.cachepolicy = new httprequestcachepolicy(httprequestcachelevel.nocachenostore);     request.accept = "application/bespoke.format+json; version=1";     request.keepalive = true;     request.maximumautomaticredirections = 99;     //request.preauthenticate = true;      string token = datetime.now.ticks.tostring();     string messagetosign = "get /path/\n1\n" + token + "\n";      string signaturestring = convert.tobase64string(signdata(encoding.ascii.getbytes(messagetosign)));     //signaturestring = removecontrolcharacters(signaturestring);     //signaturestring = httputility.urlencode(signaturestring);     signaturestring = signaturestring                         .replace('+', '-')                         .replace('/', '_')                         .replace("=", string.empty);      request.headers.add("authorization", "authentication.shceme.signed username=someuser&token=" + token + "&signature=" + signaturestring);      httpwebresponse response = request.getresponse() httpwebresponse;      encoding enc = system.text.encoding.getencoding(65001);     streamreader loresponsestream =     new streamreader(response.getresponsestream(), enc);      string responsestring = loresponsestream.readtoend();      loresponsestream.close();     response.close();      resulttextbox.text = responsestring; } 

if don't mind i'm going skip part perform base 64 encoding in 1 code fragment not in other.

unlike rsa pkcs#1 v1.5 signatures, ecdsa signatures not deterministic. in other words, depend on random number generator generate signatures. signatures have different value after each signing operation. correctness of value of these signatures can tested verifying public key.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -