Created
July 3, 2018 21:20
-
-
Save huhn511/e2e30af112aaea270659f600ab4bb2f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function verifyData(data) { | |
// return false if the object has no signature | |
if (!data.sig) { | |
return false; | |
} | |
else { | |
// save the signature and delite it from the object | |
const signature = data.sig; | |
delete data.sig; | |
// converts the object to a string | |
const json = JSON.stringify(data); | |
// loads to public key as a file | |
const publicKey = await util.promisify(fs.readFile)(path.join(pubKeyFile)); | |
// Creates and returns a Verify object that uses RSA-SHA256 | |
//https://en.wikipedia.org/wiki/RSA_(cryptosystem) | |
// https://en.wikipedia.org/wiki/RSA_(cryptosystem) | |
// https://en.wikipedia.org/wiki/Secure_Hash_Algorithms | |
const verifier = crypto.createVerify("RSA-SHA256"); | |
// Updates the Verify content with the given data as json string | |
verifier.update(json); | |
// Verifies the provided json with the public key as a string and the saved signature | |
// Returns true or false depending on the validity of the signature for the data and public key. | |
// https://nodejs.org/api/crypto.html#crypto_verify_verify_object_signature_signatureformat | |
return verifier.verify(publicKey.toString(), signature, "hex"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment