Allow to add context on key derivations

This commit is contained in:
2024-09-11 22:43:52 +02:00
parent 11801b81f5
commit 7aef4bed50
5 changed files with 45 additions and 31 deletions
+8 -8
View File
@@ -11,21 +11,21 @@ export default class PwdBox {
private readonly salt: Uint8Array
) {}
private static async derive(pwd: string, salt: Uint8Array): Promise<CryptoKey> {
const k = await pbkdf(salt, pwd)
return await hkdf(k, Usage.box) as CryptoKey
private static async derive(pwd: string, salt: Uint8Array, context?: string): Promise<CryptoKey> {
const k = await pbkdf(salt, pwd) // TODO : null
return await hkdf(k, Usage.box, context) as CryptoKey // TODO : null
}
public static async encrypt(data: Uint8Array, pwd: string): Promise<PwdBox> {
public static async encrypt(data: Uint8Array, pwd: string, context?: string): Promise<PwdBox> {
log.trace('encrypt')
const salt = crypto.getRandomValues(new Uint8Array(16))
const k = await PwdBox.derive(pwd, salt)
const box = await SecretBox.encrypt(data, k)
const k = await PwdBox.derive(pwd, salt, context) // TODO : null
const box = await SecretBox.encrypt(data, k) // TODO : null
return new PwdBox(box, salt)
}
public async decrypt(pwd: string): Promise<Uint8Array | null> {
public async decrypt(pwd: string, context?: string): Promise<Uint8Array | null> {
log.trace('decrypt')
const k = await PwdBox.derive(pwd, this.salt)
const k = await PwdBox.derive(pwd, this.salt, context) // TODO : null
return await this.box.decrypt(k)
}