Okay. I know what you're thinking. I know this is dumb, but a friend of mine wanted to port their
createHmac
code to work on Cloudflare Workers, and I thought it would be a good idea to make
a polyfill for it.
I highly recommend you use the WebCrypto API directly instead, but this is for the tired souls who are too tired to figure that out. You're free to copy paste this code into your project if you want.
Note
crypto.subtle
is a fairly new global, so if you want to use this package, you'll need to be on Node 19 or above.
Warning This polyfill only supports
UTF-8
strings andhex
digests. If you need anything else, you're on your own.
npm i createhmac-polyfill
import { createHmac } from 'createhmac-polyfill';
const hmac = createHmac('sha512', process.env.SECRET);
// acquire data
const data = [...];
hmac.update(data);
const signature = await hmac.digest('hex');