hasKey helper
This commit is contained in:
parent
6aa922f380
commit
eda99e4f12
|
@ -15,6 +15,7 @@ export abstract class CryptoService {
|
||||||
getPrivateKey: () => Promise<ArrayBuffer>;
|
getPrivateKey: () => Promise<ArrayBuffer>;
|
||||||
getOrgKeys: () => Promise<Map<string, SymmetricCryptoKey>>;
|
getOrgKeys: () => Promise<Map<string, SymmetricCryptoKey>>;
|
||||||
getOrgKey: (orgId: string) => Promise<SymmetricCryptoKey>;
|
getOrgKey: (orgId: string) => Promise<SymmetricCryptoKey>;
|
||||||
|
hasKey: () => Promise<boolean>;
|
||||||
clearKey: () => Promise<any>;
|
clearKey: () => Promise<any>;
|
||||||
clearKeyHash: () => Promise<any>;
|
clearKeyHash: () => Promise<any>;
|
||||||
clearEncKey: (memoryOnly?: boolean) => Promise<any>;
|
clearEncKey: (memoryOnly?: boolean) => Promise<any>;
|
||||||
|
|
|
@ -192,6 +192,10 @@ export class CryptoService implements CryptoServiceAbstraction {
|
||||||
return orgKeys.get(orgId);
|
return orgKeys.get(orgId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async hasKey(): Promise<boolean> {
|
||||||
|
return (await this.getKey()) != null;
|
||||||
|
}
|
||||||
|
|
||||||
clearKey(): Promise<any> {
|
clearKey(): Promise<any> {
|
||||||
this.key = this.legacyEtmKey = null;
|
this.key = this.legacyEtmKey = null;
|
||||||
return this.secureStorageService.remove(Keys.key);
|
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> {
|
async encrypt(plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey): Promise<CipherString> {
|
||||||
if (plainValue == null) {
|
if (plainValue == null) {
|
||||||
return Promise.resolve(null);
|
return Promise.resolve(null);
|
||||||
|
@ -594,6 +591,13 @@ export class CryptoService implements CryptoServiceAbstraction {
|
||||||
return key;
|
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
|
// ref: https://tools.ietf.org/html/rfc5869
|
||||||
private async hkdfExpand(prk: ArrayBuffer, info: Uint8Array, size: number) {
|
private async hkdfExpand(prk: ArrayBuffer, info: Uint8Array, size: number) {
|
||||||
const hashLen = 32; // sha256
|
const hashLen = 32; // sha256
|
||||||
|
|
Loading…
Reference in New Issue