feat: add secret derivation for KeePass key files
This commit is contained in:
@@ -13,3 +13,20 @@ export function deriveKey(password: string, salt: Uint8Array, rounds: number, le
|
||||
|
||||
return new Uint8Array(pbkdf2Sync(password, Buffer.from(salt), rounds, length, "sha256"));
|
||||
}
|
||||
|
||||
export function normalizeKeyFileBytes(bytes: Uint8Array): Uint8Array {
|
||||
return sha256(bytes);
|
||||
}
|
||||
|
||||
export function combineSecrets(password: string, keyFileBytes?: Uint8Array): Uint8Array {
|
||||
const passwordHash = sha256(password);
|
||||
if (!keyFileBytes) return passwordHash;
|
||||
return sha256(Buffer.concat([Buffer.from(passwordHash), Buffer.from(normalizeKeyFileBytes(keyFileBytes))]));
|
||||
}
|
||||
|
||||
export function deriveMasterKey(secret: Uint8Array, salt: Uint8Array, rounds: number): Uint8Array {
|
||||
if (secret.length === 0) {
|
||||
throw new Error("Missing secret for key derivation");
|
||||
}
|
||||
return new Uint8Array(pbkdf2Sync(Buffer.from(secret), Buffer.from(salt), rounds, 32, "sha256"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user