mirror of
https://github.com/bitwarden/browser.git
synced 2024-12-24 16:49:26 +01:00
remakeEncKey
This commit is contained in:
parent
1454aff46c
commit
00562d083b
@ -32,6 +32,7 @@ export abstract class CryptoService {
|
||||
makeKeyPair: (key?: SymmetricCryptoKey) => Promise<[string, CipherString]>;
|
||||
hashPassword: (password: string, key: SymmetricCryptoKey) => Promise<string>;
|
||||
makeEncKey: (key: SymmetricCryptoKey) => Promise<[SymmetricCryptoKey, CipherString]>;
|
||||
remakeEncKey: (key: SymmetricCryptoKey) => Promise<[SymmetricCryptoKey, CipherString]>
|
||||
encrypt: (plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey) => Promise<CipherString>;
|
||||
encryptToBytes: (plainValue: ArrayBuffer, key?: SymmetricCryptoKey) => Promise<ArrayBuffer>;
|
||||
rsaEncrypt: (data: ArrayBuffer, publicKey?: ArrayBuffer, key?: SymmetricCryptoKey) => Promise<CipherString>;
|
||||
|
@ -318,16 +318,12 @@ export class CryptoService implements CryptoServiceAbstraction {
|
||||
|
||||
async makeEncKey(key: SymmetricCryptoKey): Promise<[SymmetricCryptoKey, CipherString]> {
|
||||
const encKey = await this.cryptoFunctionService.randomBytes(64);
|
||||
let encKeyEnc: CipherString = null;
|
||||
if (key.key.byteLength === 32) {
|
||||
const newKey = await this.stretchKey(key);
|
||||
encKeyEnc = await this.encrypt(encKey, newKey);
|
||||
} else if (key.key.byteLength === 64) {
|
||||
encKeyEnc = await this.encrypt(encKey, key);
|
||||
} else {
|
||||
throw new Error('Invalid key size.');
|
||||
}
|
||||
return [new SymmetricCryptoKey(encKey), encKeyEnc];
|
||||
return this.buildEncKey(key, encKey);
|
||||
}
|
||||
|
||||
async remakeEncKey(key: SymmetricCryptoKey): Promise<[SymmetricCryptoKey, CipherString]> {
|
||||
const encKey = await this.getEncKey();
|
||||
return this.buildEncKey(key, encKey.key);
|
||||
}
|
||||
|
||||
async encrypt(plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey): Promise<CipherString> {
|
||||
@ -677,4 +673,18 @@ export class CryptoService implements CryptoServiceAbstraction {
|
||||
}
|
||||
return okm;
|
||||
}
|
||||
|
||||
private async buildEncKey(key: SymmetricCryptoKey, encKey: ArrayBuffer = null)
|
||||
: Promise<[SymmetricCryptoKey, CipherString]> {
|
||||
let encKeyEnc: CipherString = null;
|
||||
if (key.key.byteLength === 32) {
|
||||
const newKey = await this.stretchKey(key);
|
||||
encKeyEnc = await this.encrypt(encKey, newKey);
|
||||
} else if (key.key.byteLength === 64) {
|
||||
encKeyEnc = await this.encrypt(encKey, key);
|
||||
} else {
|
||||
throw new Error('Invalid key size.');
|
||||
}
|
||||
return [new SymmetricCryptoKey(encKey), encKeyEnc];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user