magento2-api-wrapper
Version:
Minimal Magento 2 API library. Both node and browser compatible
13 lines (12 loc) • 558 B
JavaScript
export function createHmacHasher(hash) {
return async function hmacHasher(key, content) {
// encoder to convert string to Uint8Array
const enc = new TextEncoder();
const algo = { name: "HMAC", hash };
const cryptKey = await crypto.subtle.importKey("raw", enc.encode(key), algo, false, ["sign", "verify"]);
const signature = await crypto.subtle.sign(algo, cryptKey, enc.encode(content));
const b = new Uint8Array(signature);
// base64 digest
return btoa(String.fromCharCode(...b));
};
}