Using crypto to sign and verify strings












1














I'm playing around with Node's Crypto's generateKeyPairSync, Sign and Verify but I can't get this ridiculously simple code to work: verify.verify always output false, eventhough it should output true. What am I missing?



const crypto = require('crypto');

const txt = 'Some text to sign';

// generates asymmetric key pair
const keys = crypto.generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs1',
format: 'pem',
}
});

// generates a signature object
const sign = crypto.createSign('sha256');
sign.update(txt);

// generates a verify object
const verify = crypto.createVerify('sha256');
verify.update(txt);

// should logs true, but logs false
console.log(
verify.verify(
keys.publicKey,
sign.sign(keys.privateKey, 'base64')
)
);









share|improve this question






















  • It is always easier to understand and debug without compound statements. Use a variable for the signed data so you can see it. It looks like signing Base64 encodes the output but the Base64 signing is not Base64 decoded.
    – zaph
    Dec 27 '18 at 22:24










  • Yup, that's what Nino Filiu noticed (see answer below). But thanks for the tip on compound statements @zaph!
    – Phillip Zuckerberg
    Dec 27 '18 at 22:29
















1














I'm playing around with Node's Crypto's generateKeyPairSync, Sign and Verify but I can't get this ridiculously simple code to work: verify.verify always output false, eventhough it should output true. What am I missing?



const crypto = require('crypto');

const txt = 'Some text to sign';

// generates asymmetric key pair
const keys = crypto.generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs1',
format: 'pem',
}
});

// generates a signature object
const sign = crypto.createSign('sha256');
sign.update(txt);

// generates a verify object
const verify = crypto.createVerify('sha256');
verify.update(txt);

// should logs true, but logs false
console.log(
verify.verify(
keys.publicKey,
sign.sign(keys.privateKey, 'base64')
)
);









share|improve this question






















  • It is always easier to understand and debug without compound statements. Use a variable for the signed data so you can see it. It looks like signing Base64 encodes the output but the Base64 signing is not Base64 decoded.
    – zaph
    Dec 27 '18 at 22:24










  • Yup, that's what Nino Filiu noticed (see answer below). But thanks for the tip on compound statements @zaph!
    – Phillip Zuckerberg
    Dec 27 '18 at 22:29














1












1








1







I'm playing around with Node's Crypto's generateKeyPairSync, Sign and Verify but I can't get this ridiculously simple code to work: verify.verify always output false, eventhough it should output true. What am I missing?



const crypto = require('crypto');

const txt = 'Some text to sign';

// generates asymmetric key pair
const keys = crypto.generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs1',
format: 'pem',
}
});

// generates a signature object
const sign = crypto.createSign('sha256');
sign.update(txt);

// generates a verify object
const verify = crypto.createVerify('sha256');
verify.update(txt);

// should logs true, but logs false
console.log(
verify.verify(
keys.publicKey,
sign.sign(keys.privateKey, 'base64')
)
);









share|improve this question













I'm playing around with Node's Crypto's generateKeyPairSync, Sign and Verify but I can't get this ridiculously simple code to work: verify.verify always output false, eventhough it should output true. What am I missing?



const crypto = require('crypto');

const txt = 'Some text to sign';

// generates asymmetric key pair
const keys = crypto.generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs1',
format: 'pem',
}
});

// generates a signature object
const sign = crypto.createSign('sha256');
sign.update(txt);

// generates a verify object
const verify = crypto.createVerify('sha256');
verify.update(txt);

// should logs true, but logs false
console.log(
verify.verify(
keys.publicKey,
sign.sign(keys.privateKey, 'base64')
)
);






node.js cryptojs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 27 '18 at 22:10









Phillip Zuckerberg

428




428












  • It is always easier to understand and debug without compound statements. Use a variable for the signed data so you can see it. It looks like signing Base64 encodes the output but the Base64 signing is not Base64 decoded.
    – zaph
    Dec 27 '18 at 22:24










  • Yup, that's what Nino Filiu noticed (see answer below). But thanks for the tip on compound statements @zaph!
    – Phillip Zuckerberg
    Dec 27 '18 at 22:29


















  • It is always easier to understand and debug without compound statements. Use a variable for the signed data so you can see it. It looks like signing Base64 encodes the output but the Base64 signing is not Base64 decoded.
    – zaph
    Dec 27 '18 at 22:24










  • Yup, that's what Nino Filiu noticed (see answer below). But thanks for the tip on compound statements @zaph!
    – Phillip Zuckerberg
    Dec 27 '18 at 22:29
















It is always easier to understand and debug without compound statements. Use a variable for the signed data so you can see it. It looks like signing Base64 encodes the output but the Base64 signing is not Base64 decoded.
– zaph
Dec 27 '18 at 22:24




It is always easier to understand and debug without compound statements. Use a variable for the signed data so you can see it. It looks like signing Base64 encodes the output but the Base64 signing is not Base64 decoded.
– zaph
Dec 27 '18 at 22:24












Yup, that's what Nino Filiu noticed (see answer below). But thanks for the tip on compound statements @zaph!
– Phillip Zuckerberg
Dec 27 '18 at 22:29




Yup, that's what Nino Filiu noticed (see answer below). But thanks for the tip on compound statements @zaph!
– Phillip Zuckerberg
Dec 27 '18 at 22:29












1 Answer
1






active

oldest

votes


















2














You can precise signature_format: "latin1" | "hex" | "base64" as the third argument of verify.verify. This returns true, as expected:



verify.verify(
keys.publicKey,
sign.sign(keys.privateKey, 'base64'),
'base64'
)





share|improve this answer

















  • 1




    Correct and it would be more clear as two statements: signed = sign.sign(keys.privateKey, 'base64') and match = verify.verify( keys.publicKey, signed, 'base64'). It is clearer that each has a Base64 argument.
    – zaph
    Dec 27 '18 at 22:28













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%2f53951376%2fusing-crypto-to-sign-and-verify-strings%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









2














You can precise signature_format: "latin1" | "hex" | "base64" as the third argument of verify.verify. This returns true, as expected:



verify.verify(
keys.publicKey,
sign.sign(keys.privateKey, 'base64'),
'base64'
)





share|improve this answer

















  • 1




    Correct and it would be more clear as two statements: signed = sign.sign(keys.privateKey, 'base64') and match = verify.verify( keys.publicKey, signed, 'base64'). It is clearer that each has a Base64 argument.
    – zaph
    Dec 27 '18 at 22:28


















2














You can precise signature_format: "latin1" | "hex" | "base64" as the third argument of verify.verify. This returns true, as expected:



verify.verify(
keys.publicKey,
sign.sign(keys.privateKey, 'base64'),
'base64'
)





share|improve this answer

















  • 1




    Correct and it would be more clear as two statements: signed = sign.sign(keys.privateKey, 'base64') and match = verify.verify( keys.publicKey, signed, 'base64'). It is clearer that each has a Base64 argument.
    – zaph
    Dec 27 '18 at 22:28
















2












2








2






You can precise signature_format: "latin1" | "hex" | "base64" as the third argument of verify.verify. This returns true, as expected:



verify.verify(
keys.publicKey,
sign.sign(keys.privateKey, 'base64'),
'base64'
)





share|improve this answer












You can precise signature_format: "latin1" | "hex" | "base64" as the third argument of verify.verify. This returns true, as expected:



verify.verify(
keys.publicKey,
sign.sign(keys.privateKey, 'base64'),
'base64'
)






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 27 '18 at 22:21









Nino Filiu

924516




924516








  • 1




    Correct and it would be more clear as two statements: signed = sign.sign(keys.privateKey, 'base64') and match = verify.verify( keys.publicKey, signed, 'base64'). It is clearer that each has a Base64 argument.
    – zaph
    Dec 27 '18 at 22:28
















  • 1




    Correct and it would be more clear as two statements: signed = sign.sign(keys.privateKey, 'base64') and match = verify.verify( keys.publicKey, signed, 'base64'). It is clearer that each has a Base64 argument.
    – zaph
    Dec 27 '18 at 22:28










1




1




Correct and it would be more clear as two statements: signed = sign.sign(keys.privateKey, 'base64') and match = verify.verify( keys.publicKey, signed, 'base64'). It is clearer that each has a Base64 argument.
– zaph
Dec 27 '18 at 22:28






Correct and it would be more clear as two statements: signed = sign.sign(keys.privateKey, 'base64') and match = verify.verify( keys.publicKey, signed, 'base64'). It is clearer that each has a Base64 argument.
– zaph
Dec 27 '18 at 22:28




















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%2f53951376%2fusing-crypto-to-sign-and-verify-strings%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