From c2ae23137b8a74477ed54a5e40086eb3fa1dd290 Mon Sep 17 00:00:00 2001 From: Romaric Mourgues Date: Mon, 24 Apr 2023 21:37:09 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=20Ability=20to=20work=20without=20?= =?UTF-8?q?encryption=20in=20db=20(#33)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Ability to work without encryption in db --- tdrive/backend/node/config/test.json | 2 +- tdrive/backend/node/src/core/crypto/index.ts | 31 ++++++++++++-------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/tdrive/backend/node/config/test.json b/tdrive/backend/node/config/test.json index 65aadc6f..682f49df 100644 --- a/tdrive/backend/node/config/test.json +++ b/tdrive/backend/node/config/test.json @@ -1,6 +1,6 @@ { "database": { - "secret": "ab63bb3e90c0271c9a1c06651a7c0967eab8851a7a897766", + "secret": "", "mongodb": { "uri": "mongodb://mongo:27017" }, diff --git a/tdrive/backend/node/src/core/crypto/index.ts b/tdrive/backend/node/src/core/crypto/index.ts index 417e7cad..05a5ae02 100644 --- a/tdrive/backend/node/src/core/crypto/index.ts +++ b/tdrive/backend/node/src/core/crypto/index.ts @@ -19,22 +19,26 @@ export type CryptoResult = { }; export function decrypt(data: string, encryptionKey: string): CryptoResult { - let result = v2.decrypt(data, encryptionKey); - if (result.done) { + if (encryptionKey) { + let result = v2.decrypt(data, encryptionKey); + if (result.done) { + return result; + } + + result = v1.decrypt(data, encryptionKey); + if (result.done) { + return result; + } + + result = legacy.decrypt(data, encryptionKey); + if (result.done) { + return result; + } + return result; } - result = v1.decrypt(data, encryptionKey); - if (result.done) { - return result; - } - - result = legacy.decrypt(data, encryptionKey); - if (result.done) { - return result; - } - - return result; + return { data, done: true }; } export function md5(value: string): string { @@ -46,5 +50,6 @@ export function encrypt( encryptionKey: any, options: { disableSalts?: boolean } = {}, ): CryptoResult { + if (!encryptionKey) return { data: `${value}`, done: true }; return v2.encrypt(value, encryptionKey, options); }