Skip to content

Instantly share code, notes, and snippets.

@huhn511
Last active July 2, 2018 18:53
Show Gist options
  • Save huhn511/e88ba9f213538b238467bd3d9ad3fe98 to your computer and use it in GitHub Desktop.
Save huhn511/e88ba9f213538b238467bd3d9ad3fe98 to your computer and use it in GitHub Desktop.
async function signData(data) {
// deletes old sig attribute, if existing
delete data.sig;
// convert obj to json
const json = JSON.stringify(data);
// load file with private key
const file = await util.promisify(fs.readFile)(path.join(privKeyFile));
// 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 signer = crypto.createSign("RSA-SHA256");
// Updates the Verify content with the given data
signer.update(json);
// Calculates the signature on the json data with the private key and returns the signature in hex as a string
// https://nodejs.org/api/crypto.html#crypto_sign_sign_privatekey_outputformat
return signer.sign(file.toString(), "hex");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment