UNPKG

@hazae41/kdbx

Version:

Rust-like KeePass (KDBX 4) file format for TypeScript

38 lines (37 loc) 1.15 kB
import { Writable } from "@hazae41/binary"; export class PreHmacKey { index; major; constructor(index, major) { this.index = index; this.major = major; } sizeOrThrow() { return 8 + this.major.bytes.length; } writeOrThrow(cursor) { cursor.writeBigUint64OrThrow(this.index, true); cursor.writeOrThrow(this.major.bytes); } async digestOrThrow() { const bytes = Writable.writeToBytesOrThrow(this); const digest = new Uint8Array(await crypto.subtle.digest("SHA-512", bytes)); const key = await crypto.subtle.importKey("raw", digest, { name: "HMAC", hash: "SHA-256" }, true, ["sign", "verify"]); return new HmacKey(key); } } export class HmacKey { key; constructor(key) { this.key = key; } async signOrThrow(data) { return new Uint8Array(await crypto.subtle.sign("HMAC", this.key, data)); } async verifyOrThrow(data, signature) { const result = await crypto.subtle.verify("HMAC", this.key, signature, data); if (result !== true) throw new Error(); return; } }