B2B-88: add starter kit structure and elements
This commit is contained in:
28
packages/billing/lemon-squeezy/src/services/verify-hmac.ts
Normal file
28
packages/billing/lemon-squeezy/src/services/verify-hmac.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
function bufferToHex(buffer: ArrayBuffer) {
|
||||
return Array.from(new Uint8Array(buffer))
|
||||
.map((b) => b.toString(16).padStart(2, '0'))
|
||||
.join('');
|
||||
}
|
||||
|
||||
export async function createHmac({ key, data }: { key: string; data: string }) {
|
||||
const encoder = new TextEncoder();
|
||||
const encodedKey = encoder.encode(key);
|
||||
const encodedData = encoder.encode(data);
|
||||
|
||||
const hmacKey = await crypto.subtle.importKey(
|
||||
'raw',
|
||||
encodedKey,
|
||||
{
|
||||
name: 'HMAC',
|
||||
hash: 'SHA-256',
|
||||
},
|
||||
true,
|
||||
['sign', 'verify'],
|
||||
);
|
||||
|
||||
const signature = await crypto.subtle.sign('HMAC', hmacKey, encodedData);
|
||||
|
||||
const hex = bufferToHex(signature);
|
||||
|
||||
return { hex };
|
||||
}
|
||||
Reference in New Issue
Block a user