@hazae41/kdbx
Version:
Rust-like KeePass (KDBX 4) file format for TypeScript
176 lines (175 loc) • 6.75 kB
JavaScript
// deno-lint-ignore-file no-namespace
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
if (value !== null && value !== void 0) {
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
var dispose, inner;
if (async) {
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
dispose = value[Symbol.asyncDispose];
}
if (dispose === void 0) {
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
dispose = value[Symbol.dispose];
if (async) inner = dispose;
}
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
env.stack.push({ value: value, dispose: dispose, async: async });
}
else if (async) {
env.stack.push({ async: true });
}
return value;
};
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
return function (env) {
function fail(e) {
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
env.hasError = true;
}
var r, s = 0;
function next() {
while (r = env.stack.pop()) {
try {
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
if (r.dispose) {
var result = r.dispose.call(r.value);
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
}
else s |= 1;
}
catch (e) {
fail(e);
}
}
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
if (env.hasError) throw env.error;
}
return next();
};
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
});
import { chaCha20Poly1305 } from "@hazae41/chacha20poly1305";
import { Cursor } from "@hazae41/cursor";
export var Cipher;
(function (Cipher) {
class ArcFourVariant {
constructor() { }
}
Cipher.ArcFourVariant = ArcFourVariant;
(function (ArcFourVariant) {
ArcFourVariant.type = 0x01;
function cloneOrThrow() {
return ArcFourVariant;
}
ArcFourVariant.cloneOrThrow = cloneOrThrow;
function sizeOrThrow() {
return 4;
}
ArcFourVariant.sizeOrThrow = sizeOrThrow;
function writeOrThrow(cursor) {
cursor.writeUint32OrThrow(ArcFourVariant.type, true);
}
ArcFourVariant.writeOrThrow = writeOrThrow;
// deno-lint-ignore require-await
async function initOrThrow(seed) {
throw new Error("ArcFourVariant is not implemented yet");
}
ArcFourVariant.initOrThrow = initOrThrow;
})(ArcFourVariant = Cipher.ArcFourVariant || (Cipher.ArcFourVariant = {}));
class Salsa20 {
key;
nonce;
constructor(key, nonce) {
this.key = key;
this.nonce = nonce;
}
}
Cipher.Salsa20 = Salsa20;
(function (Salsa20) {
Salsa20.type = 0x02;
function cloneOrThrow() {
return Salsa20;
}
Salsa20.cloneOrThrow = cloneOrThrow;
function sizeOrThrow() {
return 4;
}
Salsa20.sizeOrThrow = sizeOrThrow;
function writeOrThrow(cursor) {
cursor.writeUint32OrThrow(Salsa20.type, true);
}
Salsa20.writeOrThrow = writeOrThrow;
// deno-lint-ignore require-await
async function initOrThrow(seed) {
throw new Error("Salsa20 is not implemented yet");
}
Salsa20.initOrThrow = initOrThrow;
})(Salsa20 = Cipher.Salsa20 || (Cipher.Salsa20 = {}));
class ChaCha20 {
cipher;
constructor(cipher) {
this.cipher = cipher;
}
[Symbol.dispose]() {
this.cipher[Symbol.dispose]();
}
applyOrThrow(data) {
const { Memory } = chaCha20Poly1305.get().getOrThrow();
const memory = Memory.fromOrThrow(data);
this.cipher.applyOrThrow(memory);
return memory;
}
}
Cipher.ChaCha20 = ChaCha20;
(function (ChaCha20) {
ChaCha20.type = 0x03;
function cloneOrThrow() {
return ChaCha20;
}
ChaCha20.cloneOrThrow = cloneOrThrow;
function sizeOrThrow() {
return 4;
}
ChaCha20.sizeOrThrow = sizeOrThrow;
function writeOrThrow(cursor) {
cursor.writeUint32OrThrow(ChaCha20.type, true);
}
ChaCha20.writeOrThrow = writeOrThrow;
async function initOrThrow(seed) {
const env_1 = { stack: [], error: void 0, hasError: false };
try {
const { Memory, ChaCha20Cipher } = chaCha20Poly1305.get().getOrThrow();
const hashed = new Uint8Array(await crypto.subtle.digest("SHA-512", seed));
const cursor = new Cursor(hashed);
const key = __addDisposableResource(env_1, Memory.fromOrThrow(cursor.readOrThrow(32)), false);
const nonce = __addDisposableResource(env_1, Memory.fromOrThrow(cursor.readOrThrow(12)), false);
const cipher = ChaCha20Cipher.importOrThrow(key, nonce);
return new ChaCha20(cipher);
}
catch (e_1) {
env_1.error = e_1;
env_1.hasError = true;
}
finally {
__disposeResources(env_1);
}
}
ChaCha20.initOrThrow = initOrThrow;
})(ChaCha20 = Cipher.ChaCha20 || (Cipher.ChaCha20 = {}));
})(Cipher || (Cipher = {}));
(function (Cipher) {
function readOrThrow(cursor) {
const value = cursor.readUint32OrThrow(true);
if (value === Cipher.ArcFourVariant.type)
return Cipher.ArcFourVariant;
if (value === Cipher.Salsa20.type)
return Cipher.Salsa20;
if (value === Cipher.ChaCha20.type)
return Cipher.ChaCha20;
throw new Error();
}
Cipher.readOrThrow = readOrThrow;
})(Cipher || (Cipher = {}));