From e240085351dbada5b1580fdadff95f269d885c40 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 10 Sep 2018 12:13:30 -0400 Subject: [PATCH] expose decrypt to bytes --- src/abstractions/crypto.service.ts | 1 + src/services/crypto.service.ts | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/abstractions/crypto.service.ts b/src/abstractions/crypto.service.ts index d8adfca748..3dc13669fa 100644 --- a/src/abstractions/crypto.service.ts +++ b/src/abstractions/crypto.service.ts @@ -36,6 +36,7 @@ export abstract class CryptoService { encrypt: (plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey) => Promise; encryptToBytes: (plainValue: ArrayBuffer, key?: SymmetricCryptoKey) => Promise; rsaEncrypt: (data: ArrayBuffer, publicKey?: ArrayBuffer, key?: SymmetricCryptoKey) => Promise; + decryptToBytes: (cipherString: CipherString, key?: SymmetricCryptoKey) => Promise; decryptToUtf8: (cipherString: CipherString, key?: SymmetricCryptoKey) => Promise; decryptFromBytes: (encBuf: ArrayBuffer, key: SymmetricCryptoKey) => Promise; randomNumber: (min: number, max: number) => Promise; diff --git a/src/services/crypto.service.ts b/src/services/crypto.service.ts index 5f0e779629..b6a9097884 100644 --- a/src/services/crypto.service.ts +++ b/src/services/crypto.service.ts @@ -120,10 +120,10 @@ export class CryptoService implements CryptoServiceAbstraction { let decEncKey: ArrayBuffer; const encKeyCipher = new CipherString(encKey); if (encKeyCipher.encryptionType === EncryptionType.AesCbc256_B64) { - decEncKey = await this.decrypt(encKeyCipher, key); + decEncKey = await this.decryptToBytes(encKeyCipher, key); } else if (encKeyCipher.encryptionType === EncryptionType.AesCbc256_HmacSha256_B64) { const newKey = await this.stretchKey(key); - decEncKey = await this.decrypt(encKeyCipher, newKey); + decEncKey = await this.decryptToBytes(encKeyCipher, newKey); } else { throw new Error('Unsupported encKey type.'); } @@ -159,7 +159,7 @@ export class CryptoService implements CryptoServiceAbstraction { return null; } - this.privateKey = await this.decrypt(new CipherString(encPrivateKey), null); + this.privateKey = await this.decryptToBytes(new CipherString(encPrivateKey), null); return this.privateKey; } @@ -383,7 +383,7 @@ export class CryptoService implements CryptoServiceAbstraction { return new CipherString(type, Utils.fromBufferToB64(encBytes), null, mac); } - async decrypt(cipherString: CipherString, key?: SymmetricCryptoKey): Promise { + async decryptToBytes(cipherString: CipherString, key?: SymmetricCryptoKey): Promise { const iv = Utils.fromB64ToArray(cipherString.iv).buffer; const data = Utils.fromB64ToArray(cipherString.data).buffer; const mac = cipherString.mac ? Utils.fromB64ToArray(cipherString.mac).buffer : null;