UNPKG

@translated/lara

Version:

Official Lara SDK for JavaScript and Node.js

29 lines (28 loc) 1.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BrowserCrypto = void 0; /** @internal */ class BrowserCrypto { constructor() { this.subtle = window.crypto.subtle; } /** * MD5 in browser is not supported, so we use SHA-256 instead and return the first 16 bytes */ async digest(data) { const encoder = new TextEncoder(); const buffer = (await this.subtle.digest("sha-256", encoder.encode(data))).slice(0, 16); return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join(""); } async hmac(key, data) { const encoder = new TextEncoder(); encoder.encode(data); const cKey = await this.subtle.importKey("raw", encoder.encode(key), { name: "hmac", hash: { name: "sha-256" } }, false, ["sign"]); const buffer = await this.subtle.sign("hmac", cKey, encoder.encode(data)); return btoa(String.fromCharCode(...new Uint8Array(buffer))); } } exports.BrowserCrypto = BrowserCrypto;