Generating JWK Thumbprints with Node.JS
As mentioned in How are Open Banking Key Ids (kid
) Generated?, Open Banking use the JWK thumbprints as defined by RFC7638: JSON Web Key (JWK) Thumbprint.
But these may be used in other circumstances, so it's worth knowing how to generate them. Instead of hand-rolling the generation process, we can re-use the excellent node-jose:
const fs = require('fs');
const jose = require('node-jose');
const args = process.argv.slice(2);
const publicKey = fs.readFileSync(args[0]);
const hash = args[1] || 'SHA-256';
(async () => {
const key = await jose.JWK.asKey(publicKey, 'pem');
key.thumbprint(hash).
then(function(print) {
console.log(jose.util.base64url.encode(print));
});
})();
This allows us to run the following:
node thumb.js path/to/public.cer # works with certificates
node thumb.js path/to/public.pem # to use default hash algorithm
node thumb.js path/to/public.pem SHA-1 # to specify our own