Sign / Verify with nodejs crypto always false












2















I need to sign and verify messages in node.js. I followed the crypto package document but I always get a false on the verify method, which indicates that the signature is incorrect.



The RSA public key and private key I'm using are working well for encrypt / decrypt messages with the same crypto package.



Here's the code:



var crypto = require("crypto");
var eol = require('os').EOL;


The sign and verify methods:



function RSASign(privateKey, data) {
const sign = crypto.createSign('RSA-SHA256');
sign.update(data);
var sig = sign.sign(privateKey, 'hex')
console.log(sig);
return sig;
}

function RSAVerify(publicKey, signature, data) {
const verify = crypto.createVerify('RSA-SHA256');
verify.update(data);
console.log(verify.verify(publicKey, signature));
}


I'm calling the methods using



var dataToSign = "some data";
var sig = RSASign(privateKey, dataToSign);
RSAVerify(publicKey, sig, dataToSign);


The public key:



var pubStr = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbbBSVpWzSmCGVeezhuVFgUEYowUxgX/SnFdymGRCHGc77d5I0xkMAnIOWbI2MmP8j/7sdfPuUF0V5zw+Hd/7iZ6vs2k4JRKdprrB/zSC4GGqCDpDkbRYydcw3kwDgKkHhDp6NwIKvvl87WsnFozi487tGPQ8NO15hngwsV7DrawIDAQAB';
var publickKey = '-----BEGIN PUBLIC KEY-----' + eol + pubStr + eol + '-----END PUBLIC KEY-----';


The private key:



var p = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJtsFJWlbNKYIZV57OG5UWBQRijBTGBf9KcV3KYZEIcZzvt3kjTGQwCcg5ZsjYyY/yP/ux18+5QXRXnPD4d3/uJnq+zaTglEp2musH/NILgYaoIOkORtFjJ1zDeTAOAqQeEOno3Agq++XztaycWjOLjzu0Y9Dw07XmGeDCxXsOtrAgMBAAECgYAV13iFFzxV1B9UHFBX4G05Nc7GR3PuT03YdVAO35LdCZl26XTYicw8t8IeT58M1St16ahoGnpYc3TGC31JMmnVOr58At0jbd4JQgvUaE+2jVvgp0Lc6n/jN+7NYBGlEy44ZpIRbB1Biu7khCZ0D+8PZsDMi6WJK4jgI5Gf/aXvkQJBAOe6809U/1wEVDJFHZ6A++WI/8iebXSbw9hDa4a9qoXv8bsMjYkDiblD3UPRlTEdFsAOA/YuGdah+fKE7jKdKkcCQQCrszD8Z1MYWYE4dMQTRPxEKHGQZd5HHkTQu9l6FV7Bv2ga9wLhT4QTb/5U7WYGgbfxhFzklxoqsmhTJNuLlyO9AkBrA1nDZBQ9MT6UrHheL2Ckgpzkz8zqUdiicZghdEtgaQtv/v8JrBmY9e8jl5DXSoCsFozbzjReexTLW3oI462XAkEAnTQ/kZl4tz6b1XjzXUE4R59P+wmJ7kuEbijQAbs3OuVpB+dJN8l5/+H2VwPU+fgi1np+Ir1GM/mNEzMX4ELNcQJBAIk1s3Y7ep2gK0p4js4f9HU0u2vH25+6bu+y6JFfvIBd8OR1bCFEe3FIui1H/ohh0Eoo3ZgJZ/5JjwfsqJzOoBs=';
var privateKey = '-----BEGIN PRIVATE KEY-----' + eol + p + eol + '-----END PRIVATE KEY-----'


What am I missing?










share|improve this question





























    2















    I need to sign and verify messages in node.js. I followed the crypto package document but I always get a false on the verify method, which indicates that the signature is incorrect.



    The RSA public key and private key I'm using are working well for encrypt / decrypt messages with the same crypto package.



    Here's the code:



    var crypto = require("crypto");
    var eol = require('os').EOL;


    The sign and verify methods:



    function RSASign(privateKey, data) {
    const sign = crypto.createSign('RSA-SHA256');
    sign.update(data);
    var sig = sign.sign(privateKey, 'hex')
    console.log(sig);
    return sig;
    }

    function RSAVerify(publicKey, signature, data) {
    const verify = crypto.createVerify('RSA-SHA256');
    verify.update(data);
    console.log(verify.verify(publicKey, signature));
    }


    I'm calling the methods using



    var dataToSign = "some data";
    var sig = RSASign(privateKey, dataToSign);
    RSAVerify(publicKey, sig, dataToSign);


    The public key:



    var pubStr = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbbBSVpWzSmCGVeezhuVFgUEYowUxgX/SnFdymGRCHGc77d5I0xkMAnIOWbI2MmP8j/7sdfPuUF0V5zw+Hd/7iZ6vs2k4JRKdprrB/zSC4GGqCDpDkbRYydcw3kwDgKkHhDp6NwIKvvl87WsnFozi487tGPQ8NO15hngwsV7DrawIDAQAB';
    var publickKey = '-----BEGIN PUBLIC KEY-----' + eol + pubStr + eol + '-----END PUBLIC KEY-----';


    The private key:



    var p = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJtsFJWlbNKYIZV57OG5UWBQRijBTGBf9KcV3KYZEIcZzvt3kjTGQwCcg5ZsjYyY/yP/ux18+5QXRXnPD4d3/uJnq+zaTglEp2musH/NILgYaoIOkORtFjJ1zDeTAOAqQeEOno3Agq++XztaycWjOLjzu0Y9Dw07XmGeDCxXsOtrAgMBAAECgYAV13iFFzxV1B9UHFBX4G05Nc7GR3PuT03YdVAO35LdCZl26XTYicw8t8IeT58M1St16ahoGnpYc3TGC31JMmnVOr58At0jbd4JQgvUaE+2jVvgp0Lc6n/jN+7NYBGlEy44ZpIRbB1Biu7khCZ0D+8PZsDMi6WJK4jgI5Gf/aXvkQJBAOe6809U/1wEVDJFHZ6A++WI/8iebXSbw9hDa4a9qoXv8bsMjYkDiblD3UPRlTEdFsAOA/YuGdah+fKE7jKdKkcCQQCrszD8Z1MYWYE4dMQTRPxEKHGQZd5HHkTQu9l6FV7Bv2ga9wLhT4QTb/5U7WYGgbfxhFzklxoqsmhTJNuLlyO9AkBrA1nDZBQ9MT6UrHheL2Ckgpzkz8zqUdiicZghdEtgaQtv/v8JrBmY9e8jl5DXSoCsFozbzjReexTLW3oI462XAkEAnTQ/kZl4tz6b1XjzXUE4R59P+wmJ7kuEbijQAbs3OuVpB+dJN8l5/+H2VwPU+fgi1np+Ir1GM/mNEzMX4ELNcQJBAIk1s3Y7ep2gK0p4js4f9HU0u2vH25+6bu+y6JFfvIBd8OR1bCFEe3FIui1H/ohh0Eoo3ZgJZ/5JjwfsqJzOoBs=';
    var privateKey = '-----BEGIN PRIVATE KEY-----' + eol + p + eol + '-----END PRIVATE KEY-----'


    What am I missing?










    share|improve this question



























      2












      2








      2








      I need to sign and verify messages in node.js. I followed the crypto package document but I always get a false on the verify method, which indicates that the signature is incorrect.



      The RSA public key and private key I'm using are working well for encrypt / decrypt messages with the same crypto package.



      Here's the code:



      var crypto = require("crypto");
      var eol = require('os').EOL;


      The sign and verify methods:



      function RSASign(privateKey, data) {
      const sign = crypto.createSign('RSA-SHA256');
      sign.update(data);
      var sig = sign.sign(privateKey, 'hex')
      console.log(sig);
      return sig;
      }

      function RSAVerify(publicKey, signature, data) {
      const verify = crypto.createVerify('RSA-SHA256');
      verify.update(data);
      console.log(verify.verify(publicKey, signature));
      }


      I'm calling the methods using



      var dataToSign = "some data";
      var sig = RSASign(privateKey, dataToSign);
      RSAVerify(publicKey, sig, dataToSign);


      The public key:



      var pubStr = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbbBSVpWzSmCGVeezhuVFgUEYowUxgX/SnFdymGRCHGc77d5I0xkMAnIOWbI2MmP8j/7sdfPuUF0V5zw+Hd/7iZ6vs2k4JRKdprrB/zSC4GGqCDpDkbRYydcw3kwDgKkHhDp6NwIKvvl87WsnFozi487tGPQ8NO15hngwsV7DrawIDAQAB';
      var publickKey = '-----BEGIN PUBLIC KEY-----' + eol + pubStr + eol + '-----END PUBLIC KEY-----';


      The private key:



      var p = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJtsFJWlbNKYIZV57OG5UWBQRijBTGBf9KcV3KYZEIcZzvt3kjTGQwCcg5ZsjYyY/yP/ux18+5QXRXnPD4d3/uJnq+zaTglEp2musH/NILgYaoIOkORtFjJ1zDeTAOAqQeEOno3Agq++XztaycWjOLjzu0Y9Dw07XmGeDCxXsOtrAgMBAAECgYAV13iFFzxV1B9UHFBX4G05Nc7GR3PuT03YdVAO35LdCZl26XTYicw8t8IeT58M1St16ahoGnpYc3TGC31JMmnVOr58At0jbd4JQgvUaE+2jVvgp0Lc6n/jN+7NYBGlEy44ZpIRbB1Biu7khCZ0D+8PZsDMi6WJK4jgI5Gf/aXvkQJBAOe6809U/1wEVDJFHZ6A++WI/8iebXSbw9hDa4a9qoXv8bsMjYkDiblD3UPRlTEdFsAOA/YuGdah+fKE7jKdKkcCQQCrszD8Z1MYWYE4dMQTRPxEKHGQZd5HHkTQu9l6FV7Bv2ga9wLhT4QTb/5U7WYGgbfxhFzklxoqsmhTJNuLlyO9AkBrA1nDZBQ9MT6UrHheL2Ckgpzkz8zqUdiicZghdEtgaQtv/v8JrBmY9e8jl5DXSoCsFozbzjReexTLW3oI462XAkEAnTQ/kZl4tz6b1XjzXUE4R59P+wmJ7kuEbijQAbs3OuVpB+dJN8l5/+H2VwPU+fgi1np+Ir1GM/mNEzMX4ELNcQJBAIk1s3Y7ep2gK0p4js4f9HU0u2vH25+6bu+y6JFfvIBd8OR1bCFEe3FIui1H/ohh0Eoo3ZgJZ/5JjwfsqJzOoBs=';
      var privateKey = '-----BEGIN PRIVATE KEY-----' + eol + p + eol + '-----END PRIVATE KEY-----'


      What am I missing?










      share|improve this question
















      I need to sign and verify messages in node.js. I followed the crypto package document but I always get a false on the verify method, which indicates that the signature is incorrect.



      The RSA public key and private key I'm using are working well for encrypt / decrypt messages with the same crypto package.



      Here's the code:



      var crypto = require("crypto");
      var eol = require('os').EOL;


      The sign and verify methods:



      function RSASign(privateKey, data) {
      const sign = crypto.createSign('RSA-SHA256');
      sign.update(data);
      var sig = sign.sign(privateKey, 'hex')
      console.log(sig);
      return sig;
      }

      function RSAVerify(publicKey, signature, data) {
      const verify = crypto.createVerify('RSA-SHA256');
      verify.update(data);
      console.log(verify.verify(publicKey, signature));
      }


      I'm calling the methods using



      var dataToSign = "some data";
      var sig = RSASign(privateKey, dataToSign);
      RSAVerify(publicKey, sig, dataToSign);


      The public key:



      var pubStr = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbbBSVpWzSmCGVeezhuVFgUEYowUxgX/SnFdymGRCHGc77d5I0xkMAnIOWbI2MmP8j/7sdfPuUF0V5zw+Hd/7iZ6vs2k4JRKdprrB/zSC4GGqCDpDkbRYydcw3kwDgKkHhDp6NwIKvvl87WsnFozi487tGPQ8NO15hngwsV7DrawIDAQAB';
      var publickKey = '-----BEGIN PUBLIC KEY-----' + eol + pubStr + eol + '-----END PUBLIC KEY-----';


      The private key:



      var p = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJtsFJWlbNKYIZV57OG5UWBQRijBTGBf9KcV3KYZEIcZzvt3kjTGQwCcg5ZsjYyY/yP/ux18+5QXRXnPD4d3/uJnq+zaTglEp2musH/NILgYaoIOkORtFjJ1zDeTAOAqQeEOno3Agq++XztaycWjOLjzu0Y9Dw07XmGeDCxXsOtrAgMBAAECgYAV13iFFzxV1B9UHFBX4G05Nc7GR3PuT03YdVAO35LdCZl26XTYicw8t8IeT58M1St16ahoGnpYc3TGC31JMmnVOr58At0jbd4JQgvUaE+2jVvgp0Lc6n/jN+7NYBGlEy44ZpIRbB1Biu7khCZ0D+8PZsDMi6WJK4jgI5Gf/aXvkQJBAOe6809U/1wEVDJFHZ6A++WI/8iebXSbw9hDa4a9qoXv8bsMjYkDiblD3UPRlTEdFsAOA/YuGdah+fKE7jKdKkcCQQCrszD8Z1MYWYE4dMQTRPxEKHGQZd5HHkTQu9l6FV7Bv2ga9wLhT4QTb/5U7WYGgbfxhFzklxoqsmhTJNuLlyO9AkBrA1nDZBQ9MT6UrHheL2Ckgpzkz8zqUdiicZghdEtgaQtv/v8JrBmY9e8jl5DXSoCsFozbzjReexTLW3oI462XAkEAnTQ/kZl4tz6b1XjzXUE4R59P+wmJ7kuEbijQAbs3OuVpB+dJN8l5/+H2VwPU+fgi1np+Ir1GM/mNEzMX4ELNcQJBAIk1s3Y7ep2gK0p4js4f9HU0u2vH25+6bu+y6JFfvIBd8OR1bCFEe3FIui1H/ohh0Eoo3ZgJZ/5JjwfsqJzOoBs=';
      var privateKey = '-----BEGIN PRIVATE KEY-----' + eol + p + eol + '-----END PRIVATE KEY-----'


      What am I missing?







      node.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 31 '18 at 8:58







      Eddy

















      asked Dec 31 '18 at 8:36









      EddyEddy

      1,35843757




      1,35843757
























          1 Answer
          1






          active

          oldest

          votes


















          1














          While verifying verify.verify(publicKey, signature,'hex'),you have to include the signatureEncoding that you have used during encoding



          verify signature



           function RSAVerify(publicKey, signature, data) {
          const verify = crypto.createVerify('RSA-SHA256');
          verify.update(data);
          console.log(verify.verify(publicKey, signature,'hex'));
          }





          share|improve this answer





















          • 1





            Or remove the output encoding from sign.sign().

            – Tarabass
            Dec 31 '18 at 9:12











          • yes..if signatureEncoding is not provided,it will be utf8 by default

            – BittuS
            Dec 31 '18 at 9:13











          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',
          autoActivateHeartbeat: false,
          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%2f53985272%2fsign-verify-with-nodejs-crypto-always-false%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









          1














          While verifying verify.verify(publicKey, signature,'hex'),you have to include the signatureEncoding that you have used during encoding



          verify signature



           function RSAVerify(publicKey, signature, data) {
          const verify = crypto.createVerify('RSA-SHA256');
          verify.update(data);
          console.log(verify.verify(publicKey, signature,'hex'));
          }





          share|improve this answer





















          • 1





            Or remove the output encoding from sign.sign().

            – Tarabass
            Dec 31 '18 at 9:12











          • yes..if signatureEncoding is not provided,it will be utf8 by default

            – BittuS
            Dec 31 '18 at 9:13
















          1














          While verifying verify.verify(publicKey, signature,'hex'),you have to include the signatureEncoding that you have used during encoding



          verify signature



           function RSAVerify(publicKey, signature, data) {
          const verify = crypto.createVerify('RSA-SHA256');
          verify.update(data);
          console.log(verify.verify(publicKey, signature,'hex'));
          }





          share|improve this answer





















          • 1





            Or remove the output encoding from sign.sign().

            – Tarabass
            Dec 31 '18 at 9:12











          • yes..if signatureEncoding is not provided,it will be utf8 by default

            – BittuS
            Dec 31 '18 at 9:13














          1












          1








          1







          While verifying verify.verify(publicKey, signature,'hex'),you have to include the signatureEncoding that you have used during encoding



          verify signature



           function RSAVerify(publicKey, signature, data) {
          const verify = crypto.createVerify('RSA-SHA256');
          verify.update(data);
          console.log(verify.verify(publicKey, signature,'hex'));
          }





          share|improve this answer















          While verifying verify.verify(publicKey, signature,'hex'),you have to include the signatureEncoding that you have used during encoding



          verify signature



           function RSAVerify(publicKey, signature, data) {
          const verify = crypto.createVerify('RSA-SHA256');
          verify.update(data);
          console.log(verify.verify(publicKey, signature,'hex'));
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 31 '18 at 9:12

























          answered Dec 31 '18 at 8:58









          BittuSBittuS

          587216




          587216








          • 1





            Or remove the output encoding from sign.sign().

            – Tarabass
            Dec 31 '18 at 9:12











          • yes..if signatureEncoding is not provided,it will be utf8 by default

            – BittuS
            Dec 31 '18 at 9:13














          • 1





            Or remove the output encoding from sign.sign().

            – Tarabass
            Dec 31 '18 at 9:12











          • yes..if signatureEncoding is not provided,it will be utf8 by default

            – BittuS
            Dec 31 '18 at 9:13








          1




          1





          Or remove the output encoding from sign.sign().

          – Tarabass
          Dec 31 '18 at 9:12





          Or remove the output encoding from sign.sign().

          – Tarabass
          Dec 31 '18 at 9:12













          yes..if signatureEncoding is not provided,it will be utf8 by default

          – BittuS
          Dec 31 '18 at 9:13





          yes..if signatureEncoding is not provided,it will be utf8 by default

          – BittuS
          Dec 31 '18 at 9:13


















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985272%2fsign-verify-with-nodejs-crypto-always-false%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







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas