From 580c7e5cb661f49260d564762d2d4896c03b3c67 Mon Sep 17 00:00:00 2001
From: Zdravko Iliev <zdravko.iliev@vereign.com>
Date: Thu, 2 Jun 2022 15:57:03 +0300
Subject: [PATCH] add optional key and iv for AES-GSM encryption

---
 dist/services/CryptoService/CryptoServiceNode.js |  8 +++++---
 src/services/CryptoService/CryptoServiceNode.ts  | 11 ++++++++---
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/dist/services/CryptoService/CryptoServiceNode.js b/dist/services/CryptoService/CryptoServiceNode.js
index ce4a075..85eef9e 100644
--- a/dist/services/CryptoService/CryptoServiceNode.js
+++ b/dist/services/CryptoService/CryptoServiceNode.js
@@ -48,10 +48,12 @@ const getBytes = (value, encoding) => {
     return bytes;
 };
 class CryptoServiceNode {
-    encryptAESGCM(data) {
+    encryptAESGCM(data, key, iv) {
         return __awaiter(this, void 0, void 0, function* () {
-            const key = crypto.randomBytes(32);
-            const iv = crypto.randomBytes(12);
+            if (!key && !iv) {
+                key = crypto.randomBytes(32);
+                iv = crypto.randomBytes(12);
+            }
             const cipher = crypto.createCipheriv(AES_GCM_ALGO, key, iv);
             let encrypted;
             if (typeof data === "string") {
diff --git a/src/services/CryptoService/CryptoServiceNode.ts b/src/services/CryptoService/CryptoServiceNode.ts
index 6ac8627..1af2cca 100644
--- a/src/services/CryptoService/CryptoServiceNode.ts
+++ b/src/services/CryptoService/CryptoServiceNode.ts
@@ -26,10 +26,15 @@ class CryptoServiceNode implements ICryptoService {
   public async encryptAESGCM(data: string): Promise<AESGCMOutput>;
   public async encryptAESGCM(data: ArrayBuffer): Promise<AESGCMOutput>;
   public async encryptAESGCM(
-    data: string | ArrayBuffer
+    data: string | ArrayBuffer,
+    key?: Buffer,
+    iv?: Buffer
   ): Promise<AESGCMOutput> {
-    const key = crypto.randomBytes(32);
-    const iv = crypto.randomBytes(12);
+    if (!key && !iv) {
+      key = crypto.randomBytes(32);
+      iv = crypto.randomBytes(12);
+    }
+
     const cipher = crypto.createCipheriv(AES_GCM_ALGO, key, iv);
 
     let encrypted;
-- 
GitLab