@hazae41/kdbx
Version:
Rust-like KeePass (KDBX 4) file format for TypeScript
126 lines (123 loc) • 4.42 kB
JavaScript
import { __addDisposableResource, __disposeResources } from '../../../../../../node_modules/tslib/tslib.es6.mjs';
import { ChaCha20Poly1305 } from '@hazae41/chacha20poly1305';
import { Cursor } from '@hazae41/cursor';
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;
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;
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.importOrThrow(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.importOrThrow(cursor.readOrThrow(32)), false);
const nonce = __addDisposableResource(env_1, Memory.importOrThrow(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 = {}));
export { Cipher };
//# sourceMappingURL=index.mjs.map