diff --git a/twake/backend/node/src/core/crypto/legacy.ts b/twake/backend/node/src/core/crypto/legacy.ts index f18479e0..63a61527 100644 --- a/twake/backend/node/src/core/crypto/legacy.ts +++ b/twake/backend/node/src/core/crypto/legacy.ts @@ -2,7 +2,7 @@ import { createDecipheriv } from "crypto"; import { CryptoResult } from "."; const PREFIX = "encrypted_"; -const DEFAULT_IV = "twake_constantiv"; +// const DEFAULT_IV = Buffer.from("twake_constantiv", "utf-8"); export default { decrypt, @@ -25,9 +25,9 @@ function decrypt(data: string, encryptionKey: string): CryptoResult { const salt = encryptedArray[1] ? Buffer.from(encryptedArray[1], "utf-8") : Buffer.from("", "utf-8"); - const iv = encryptedArray[2] + const iv = encryptedArray.length >= 3 ? Buffer.from(encryptedArray[2], "base64") - : Buffer.from(DEFAULT_IV, "utf-8"); + : Buffer.from("twake_constantiv", "utf-8"); const password = Buffer.concat([Buffer.from(encryptionKey, "hex"), salt], 32); const decipher = createDecipheriv("AES-256-CBC", password, iv); diff --git a/twake/backend/node/test/unit/core/services/database/services/utils.test.ts b/twake/backend/node/test/unit/core/services/database/services/utils.test.ts new file mode 100644 index 00000000..11fbac06 --- /dev/null +++ b/twake/backend/node/test/unit/core/services/database/services/utils.test.ts @@ -0,0 +1,31 @@ +import "reflect-metadata"; +import { describe, expect, it } from "@jest/globals"; +import { + fromMongoDbOrderable, + toMongoDbOrderable, +} from "../../../../../../src/core/platform/services/database/services/orm/utils"; +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-ignore +import _ from "lodash"; +import { v1 as uuidv1 } from "uuid"; + +describe("The MongoDb to Orderable module", () => { + describe("The to/from orderable function", () => { + it("should be unique", () => { + const uuid1 = toMongoDbOrderable(uuidv1()); + const uuid2 = toMongoDbOrderable(uuidv1()); + const uuid3 = toMongoDbOrderable(uuidv1()); + const uuid4 = toMongoDbOrderable(uuidv1()); + + expect(_.uniq([uuid1, uuid2, uuid3, uuid4]).length).toBe(4); + }); + + it("should convert both ways", () => { + const uuid = uuidv1(); + const orderable = toMongoDbOrderable(uuid); + + expect(fromMongoDbOrderable(orderable)).toBe(uuid); + expect(orderable).toBe(toMongoDbOrderable(fromMongoDbOrderable(orderable))); + }); + }); +}); diff --git a/twake/backend/node/test/unit/core/services/encryption/encryption.test.ts b/twake/backend/node/test/unit/core/services/encryption/encryption.test.ts new file mode 100644 index 00000000..83f7ea1c --- /dev/null +++ b/twake/backend/node/test/unit/core/services/encryption/encryption.test.ts @@ -0,0 +1,31 @@ +import { describe, expect, it } from "@jest/globals"; +import { decrypt } from "../../../../../src/core/crypto/index"; +import v2 from "../../../../../src/core/crypto/v2"; +import v1 from "../../../../../src/core/crypto/v1"; +import legacy from "../../../../../src/core/crypto/legacy"; + +describe("Encryption", () => { + const encryptionKey = "a7c06651a7c063bb3e90c0c9a17eab88ab8977665127196a"; + + describe("The encrypt/decrypt functions", () => { + it("should successfully describe legacy encrypted values", async () => { + const legacyEncrypted = "encrypted_DwMLnKhuFbIanqBJPA5rcw=="; + + expect(legacy.decrypt(legacyEncrypted, encryptionKey).data).toBe("My company"); + expect(decrypt(legacyEncrypted, encryptionKey).data).toBe("My company"); + }); + + it("should successfully describe all versions", async () => { + const myData = { key: "some data" }; + + const v1Encrypted = v1.encrypt(myData, encryptionKey); + const v2Encrypted = v2.encrypt(myData, encryptionKey); + + expect(v1.decrypt(v1Encrypted.data, encryptionKey).data).toMatchObject(myData); + expect(v2.decrypt(v2Encrypted.data, encryptionKey).data).toMatchObject(myData); + + expect(decrypt(v1Encrypted.data, encryptionKey).data).toMatchObject(myData); + expect(decrypt(v2Encrypted.data, encryptionKey).data).toMatchObject(myData); + }); + }); +});