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
+5 -5
View File
@@ -14,19 +14,19 @@ export default class PrivateBox {
public static async gen(extractable: boolean = true): Promise<CryptoKeyPair> {
log.trace('generate keypair')
return await crypto.subtle.generateKey(consts.ECDH, extractable, ['deriveKey']) as CryptoKeyPair
return await crypto.subtle.generateKey(consts.ECDH, extractable, ['deriveBits']) as CryptoKeyPair
}
public static async encrypt(data: Uint8Array, pubkey: CryptoKey): Promise<PrivateBox> {
public static async encrypt(data: Uint8Array, pubkey: CryptoKey, context?: string): Promise<PrivateBox> {
log.trace('encrypt')
const tmp_pair = await PrivateBox.gen(false)
const key = await ecdh(tmp_pair.privateKey, pubkey, DHusage.box)
const key = await ecdh(tmp_pair.privateKey, pubkey, DHusage.box, context) // TODO : null
const box = await SecretBox.encrypt(data, key)
return new this(tmp_pair.publicKey, box)
}
public async decrypt(privkey: CryptoKey): Promise<Uint8Array | null> {
public async decrypt(privkey: CryptoKey, context?: string): Promise<Uint8Array | null> {
log.trace('decrypt')
const key = await ecdh(privkey, this.pubkey, DHusage.box)
const key = await ecdh(privkey, this.pubkey, DHusage.box, context) // TODO : null
return await this.box.decrypt(key)
}