@translated/lara
Version:
Official Lara SDK for JavaScript and Node.js
26 lines (25 loc) • 936 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BrowserCrypto = void 0;
/** @internal */
class BrowserCrypto {
constructor() {
this.subtle = window.crypto.subtle;
}
async digestBase64(data) {
const encoder = new TextEncoder();
const buffer = (await this.subtle.digest("sha-256", encoder.encode(data))).slice(0, 16);
return btoa(String.fromCharCode(...new Uint8Array(buffer)));
}
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;