Created
May 15, 2017 11:36
-
-
Save cilliemalan/1d69ff1371f932a44d921ac73310b411 to your computer and use it in GitHub Desktop.
how to digest a PEM key
This file contains hidden or 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
/** | |
* 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