UNPKG

@hazae41/kdbx

Version:

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

37 lines (36 loc) 2.01 kB
// deno-lint-ignore-file no-explicit-any no-unused-vars import { XML } from "../../libs/xml/mod.js"; import { argon2 } from "@hazae41/argon2"; import { argon2Wasm } from "@hazae41/argon2-wasm"; import { Readable, Writable } from "@hazae41/binary"; import { chaCha20Poly1305 } from "@hazae41/chacha20poly1305"; import { chaCha20Poly1305Wasm } from "@hazae41/chacha20poly1305-wasm"; import { Window } from "happy-dom"; import { readFileSync, writeFileSync } from "node:fs"; import { CompositeKey, Database, PasswordKey } from "./mod.js"; await argon2Wasm.load(); await chaCha20Poly1305Wasm.load(); argon2.set(argon2.fromWasm(argon2Wasm)); chaCha20Poly1305.set(chaCha20Poly1305.fromWasm(chaCha20Poly1305Wasm)); const window = new Window({}); globalThis.DOMParser = window.DOMParser; globalThis.XMLSerializer = window.XMLSerializer; const password = await CompositeKey.digestOrThrow(await PasswordKey.digestOrThrow(new TextEncoder().encode("test"))); const encrypted = Readable.readFromBytesOrThrow(Database.Encrypted, readFileSync("./local/input.kdbx")).cloneOrThrow(); const decrypted = await encrypted.decryptOrThrow(password); const file = decrypted.inner.content.value; const root = file.getRootOrThrow(); const meta = file.getMetaOrThrow(); const group0 = root.getDirectGroupByIndexOrThrow(0); const subgroup0 = group0.getDirectGroupByIndexOrThrow(0); const entry0 = subgroup0.getDirectEntryByIndexOrThrow(0); entry0.saveOrThrow(); entry0.getStringByKeyOrNull("Title")?.getValueOrThrow().set("Cloned"); entry0.getStringByKeyOrNull("Password")?.getKeyOrThrow().set("PrivateKey"); entry0.getTimesOrNew().setLastModificationTime(); entry0.getTimesOrNew().setLastAccessTime(); entry0.getTimesOrNew().incrementUsageCount(); console.log(entry0.getHistoryOrNull()?.getDirectEntries().reduce(x => x + 1, 0)); console.log(XML.format(decrypted.inner.content.value.document)); const encrypted2 = await decrypted.encryptOrThrow(password); writeFileSync("./local/output.kdbx", Writable.writeToBytesOrThrow(encrypted2));