1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-27 12:36:14 +01:00

hasKey helper

This commit is contained in:
Kyle Spearrin 2018-06-13 17:10:52 -04:00
parent 6aa922f380
commit eda99e4f12
2 changed files with 12 additions and 7 deletions

View File

@ -15,6 +15,7 @@ export abstract class CryptoService {
getPrivateKey: () => Promise<ArrayBuffer>;
getOrgKeys: () => Promise<Map<string, SymmetricCryptoKey>>;
getOrgKey: (orgId: string) => Promise<SymmetricCryptoKey>;
hasKey: () => Promise<boolean>;
clearKey: () => Promise<any>;
clearKeyHash: () => Promise<any>;
clearEncKey: (memoryOnly?: boolean) => Promise<any>;

View File

@ -192,6 +192,10 @@ export class CryptoService implements CryptoServiceAbstraction {
return orgKeys.get(orgId);
}
async hasKey(): Promise<boolean> {
return (await this.getKey()) != null;
}
clearKey(): Promise<any> {
this.key = this.legacyEtmKey = null;
return this.secureStorageService.remove(Keys.key);
@ -282,13 +286,6 @@ export class CryptoService implements CryptoServiceAbstraction {
*/
}
async stretchKey(key: SymmetricCryptoKey): Promise<SymmetricCryptoKey> {
const newKey = new Uint8Array(64);
newKey.set(await this.hkdfExpand(key.key, Utils.fromUtf8ToArray('enc'), 32));
newKey.set(await this.hkdfExpand(key.key, Utils.fromUtf8ToArray('mac'), 32), 32);
return new SymmetricCryptoKey(newKey.buffer);
}
async encrypt(plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey): Promise<CipherString> {
if (plainValue == null) {
return Promise.resolve(null);
@ -594,6 +591,13 @@ export class CryptoService implements CryptoServiceAbstraction {
return key;
}
private async stretchKey(key: SymmetricCryptoKey): Promise<SymmetricCryptoKey> {
const newKey = new Uint8Array(64);
newKey.set(await this.hkdfExpand(key.key, Utils.fromUtf8ToArray('enc'), 32));
newKey.set(await this.hkdfExpand(key.key, Utils.fromUtf8ToArray('mac'), 32), 32);
return new SymmetricCryptoKey(newKey.buffer);
}
// ref: https://tools.ietf.org/html/rfc5869
private async hkdfExpand(prk: ArrayBuffer, info: Uint8Array, size: number) {
const hashLen = 32; // sha256