Skip to content

Instantly share code, notes, and snippets.

@cilliemalan
Created May 15, 2017 11:36
Show Gist options
  • Save cilliemalan/1d69ff1371f932a44d921ac73310b411 to your computer and use it in GitHub Desktop.
Save cilliemalan/1d69ff1371f932a44d921ac73310b411 to your computer and use it in GitHub Desktop.
how to digest a PEM key
/**
* returns a base64url based digest of a PEM file
* @param {*} key
*/
function digestKey(key) {
let meat = key.match(/^-{5}[A-Z ]+-{5}[\r\n]{1,2}([a-zA-Z0-9/+\r\n]+={0,3})[\r\n]{1,2}-{5}[A-Z ]+-{5}/)[1];
if (!meat) throw "unrecognized format";
meat = meat.replace(/[\r\n]{1,2}/g, '');
var buf = Buffer.from(meat, 'base64');
let sha = crypto.createHash('sha256');
sha.update(buf);
return sha.digest('base64').replace(/[+\/=]/g, s => s == '+' ? '-' : s == '/' ? '_' : '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment