Skip to content

Commit

Permalink
Check for crypto subtle then fallback to noble
Browse files Browse the repository at this point in the history
  • Loading branch information
leordev committed Oct 5, 2023
1 parent 9cb49c9 commit 1b641f9
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions packages/agent/src/app-data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,53 @@ export class AppDataVault implements AppDataStore {
throw new Error ('Not implemented');
}

private async generateVaultUnlockKey(options: {
/**
* The salt value derived in Step 3 and the passphrase entered by the
* end-user are inputs to the PBKDF2 algorithm to derive a 32-byte secret
* key that will be referred to as the Vault Unlock Key (VUK).
*/
private generateVaultUnlockKey(options: {
passphrase: string,
salt: Uint8Array
}): Promise<Uint8Array> {
if (crypto && typeof crypto.subtle === 'object' && crypto.subtle != null) {
return this.generateVaultUnlockKeyWithSubtleCrypto(options);
} else {
return this.generateVaultUnlockKeyWithNoble(options);
}

Check warning on line 151 in packages/agent/src/app-data-store.ts

View check run for this annotation

Codecov / codecov/patch

packages/agent/src/app-data-store.ts#L150-L151

Added lines #L150 - L151 were not covered by tests
}

private async generateVaultUnlockKeyWithSubtleCrypto(options: {
passphrase: string,
salt: Uint8Array
}): Promise<Uint8Array> {
const { passphrase, salt } = options;

const passwordBuffer = new TextEncoder().encode(passphrase);

const importedKey = await crypto.subtle.importKey(
'raw',
passwordBuffer,
'PBKDF2',
false,
['deriveBits']
);

const vaultUnlockKey = await crypto.subtle.deriveBits(
{
name : 'PBKDF2',
hash : 'SHA-512',
salt : salt,
iterations : this._keyDerivationWorkFactor,
},
importedKey,
32 * 8, // 32 bytes
);

return new Uint8Array(vaultUnlockKey);
}

private async generateVaultUnlockKeyWithNoble(options: {
passphrase: string,
salt: Uint8Array
}): Promise<Uint8Array> {
Expand All @@ -151,11 +197,6 @@ export class AppDataVault implements AppDataStore {
['deriveBits']
);

/**
* The salt value derived in Step 3 and the passphrase entered by the
* end-user are inputs to the PBKDF2 algorithm to derive a 32-byte secret
* key that will be referred to as the Vault Unlock Key (VUK).
*/
const vaultUnlockKey = await crypto.subtle.deriveBits(

Check warning on line 200 in packages/agent/src/app-data-store.ts

View check run for this annotation

Codecov / codecov/patch

packages/agent/src/app-data-store.ts#L190-L200

Added lines #L190 - L200 were not covered by tests
{
name : 'PBKDF2',
Expand Down

0 comments on commit 1b641f9

Please sign in to comment.