From d1ef7959a9f5ee54684c405ea52d3b4856410b32 Mon Sep 17 00:00:00 2001 From: Alexey Lunin <alexey.lunin@vereign.com> Date: Wed, 3 Aug 2022 17:11:52 +0300 Subject: [PATCH] Modify CryptoService in order to add support for injecting custom implementation of ICryptoService --- dist/index.js | 6 +- .../CryptoService/CryptoServiceNode.js | 6 +- dist/services/CryptoService/index.d.ts | 3 + dist/services/CryptoService/index.js | 98 ++++++++++++- dist/utils/common.js | 6 +- dist/utils/qrCodeTemplateUtils.js | 6 +- src/services/CryptoService/index.ts | 136 ++++++++++++++++-- 7 files changed, 242 insertions(+), 19 deletions(-) diff --git a/dist/index.js b/dist/index.js index 04de505..72d2d74 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,7 +1,11 @@ "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; diff --git a/dist/services/CryptoService/CryptoServiceNode.js b/dist/services/CryptoService/CryptoServiceNode.js index f789e30..2d3c26d 100644 --- a/dist/services/CryptoService/CryptoServiceNode.js +++ b/dist/services/CryptoService/CryptoServiceNode.js @@ -1,7 +1,11 @@ "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; diff --git a/dist/services/CryptoService/index.d.ts b/dist/services/CryptoService/index.d.ts index a3ce65f..17f73e5 100644 --- a/dist/services/CryptoService/index.d.ts +++ b/dist/services/CryptoService/index.d.ts @@ -1,3 +1,6 @@ import { ICryptoService } from "./ICryptoService"; +export interface Proxy { + injectCustomImplementation: (service: ICryptoService) => void; +} declare const service: ICryptoService; export default service; diff --git a/dist/services/CryptoService/index.js b/dist/services/CryptoService/index.js index cbea690..610f267 100644 --- a/dist/services/CryptoService/index.js +++ b/dist/services/CryptoService/index.js @@ -1,11 +1,95 @@ "use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); }; Object.defineProperty(exports, "__esModule", { value: true }); -const CryptoServiceNode_1 = __importDefault(require("./CryptoServiceNode")); -const CryptoServiceWeb_1 = __importDefault(require("./CryptoServiceWeb")); -const service = typeof crypto !== "undefined" && crypto.subtle - ? new CryptoServiceWeb_1.default() - : new CryptoServiceNode_1.default(); +class CryptoServiceProxy { + constructor() { + // this._target = typeof crypto !== "undefined" && crypto.subtle + // ? new CryptoServiceWeb() + // : new CryptoServiceNode(); + } + injectCustomImplementation(service) { + this._target = service; + } + encryptAESGCM(data) { + return __awaiter(this, arguments, void 0, function* () { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.encryptAESGCM.apply(this._target, arguments); + }); + } + decryptAESGCM(data, key, iv, returnBuffer) { + return __awaiter(this, arguments, void 0, function* () { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.decryptAESGCM.apply(this._target, arguments); + }); + } + verifyRSASignature(publicKeyPEM, data, signature) { + return __awaiter(this, arguments, void 0, function* () { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.verifyRSASignature.apply(this._target, arguments); + }); + } + generateRSAKeys() { + return __awaiter(this, arguments, void 0, function* () { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.generateRSAKeys.apply(this._target, arguments); + }); + } + encryptRSA(publicKeyPEM, data) { + return __awaiter(this, arguments, void 0, function* () { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.encryptAESGCM.apply(this._target, arguments); + }); + } + decryptRSA(privateKeyPEM, data) { + return __awaiter(this, arguments, void 0, function* () { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.decryptRSA.apply(this._target, arguments); + }); + } + signRSA(privateKeyPEM, data) { + return __awaiter(this, arguments, void 0, function* () { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.signRSA.apply(this._target, arguments); + }); + } + SHA1(value, encoding = "utf8") { + return __awaiter(this, arguments, void 0, function* () { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.SHA1.apply(this._target, arguments); + }); + } + SHA256(value, encoding = "utf8") { + return __awaiter(this, arguments, void 0, function* () { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.SHA256.apply(this._target, arguments); + }); + } + SHA384(value, encoding = "utf8") { + return __awaiter(this, arguments, void 0, function* () { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.SHA384.apply(this._target, arguments); + }); + } + SHA512(value, encoding = "utf8") { + return __awaiter(this, arguments, void 0, function* () { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.SHA512.apply(this._target, arguments); + }); + } + MD5(value, encoding = "utf8") { + return __awaiter(this, arguments, void 0, function* () { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.MD5.apply(this._target, arguments); + }); + } +} +const service = new CryptoServiceProxy(); exports.default = service; diff --git a/dist/utils/common.js b/dist/utils/common.js index 0233c1a..86fbc86 100644 --- a/dist/utils/common.js +++ b/dist/utils/common.js @@ -1,7 +1,11 @@ "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; diff --git a/dist/utils/qrCodeTemplateUtils.js b/dist/utils/qrCodeTemplateUtils.js index 3b29f8f..fb64020 100644 --- a/dist/utils/qrCodeTemplateUtils.js +++ b/dist/utils/qrCodeTemplateUtils.js @@ -1,7 +1,11 @@ "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; diff --git a/src/services/CryptoService/index.ts b/src/services/CryptoService/index.ts index 1ef2637..d2b256f 100644 --- a/src/services/CryptoService/index.ts +++ b/src/services/CryptoService/index.ts @@ -1,10 +1,130 @@ -import CryptoServiceNode from "./CryptoServiceNode"; -import CryptoServiceWeb from "./CryptoServiceWeb"; -import { ICryptoService } from "./ICryptoService"; - -const service: ICryptoService = - typeof crypto !== "undefined" && crypto.subtle - ? new CryptoServiceWeb() - : new CryptoServiceNode(); +// import CryptoServiceNode from "./CryptoServiceNode"; +// import CryptoServiceWeb from "./CryptoServiceWeb"; +import { AESGCMOutput, ICryptoService, RSAKeys } from "./ICryptoService"; + +export interface Proxy { + injectCustomImplementation: (service: ICryptoService) => void; +} + +class CryptoServiceProxy implements ICryptoService, Proxy { + private _target: ICryptoService; + constructor() { + // this._target = typeof crypto !== "undefined" && crypto.subtle + // ? new CryptoServiceWeb() + // : new CryptoServiceNode(); + } + + public injectCustomImplementation(service: ICryptoService) { + this._target = service; + } + + public async encryptAESGCM( + data: string | ArrayBuffer + ): Promise<AESGCMOutput> { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.encryptAESGCM.apply(this._target, arguments); + } + + public async decryptAESGCM( + data: ArrayBuffer, + key: ArrayBuffer, + iv: ArrayBuffer + ): Promise<string>; + public async decryptAESGCM( + data: ArrayBuffer, + key: ArrayBuffer, + iv: ArrayBuffer, + returnBuffer: true + ): Promise<ArrayBuffer>; + public async decryptAESGCM( + data: ArrayBuffer, + key: ArrayBuffer, + iv: ArrayBuffer, + returnBuffer?: boolean + ): Promise<string | ArrayBuffer> { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.decryptAESGCM.apply(this._target, arguments); + } + + public async verifyRSASignature( + publicKeyPEM: string, + data: ArrayBuffer, + signature: ArrayBuffer + ): Promise<boolean> { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.verifyRSASignature.apply(this._target, arguments); + } + + public async generateRSAKeys(): Promise<RSAKeys> { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.generateRSAKeys.apply(this._target, arguments); + } + + public async encryptRSA( + publicKeyPEM: string, + data: ArrayBuffer + ): Promise<ArrayBuffer> { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.encryptAESGCM.apply(this._target, arguments); + } + + public async decryptRSA( + privateKeyPEM: string, + data: ArrayBuffer + ): Promise<ArrayBuffer> { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.decryptRSA.apply(this._target, arguments); + } + + public async signRSA( + privateKeyPEM: string, + data: ArrayBuffer + ): Promise<ArrayBuffer> { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.signRSA.apply(this._target, arguments); + } + + public async SHA1( + value: string | ArrayBuffer, + encoding = "utf8" + ): Promise<ArrayBuffer> { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.SHA1.apply(this._target, arguments); + } + + public async SHA256( + value: string | ArrayBuffer, + encoding = "utf8" + ): Promise<ArrayBuffer> { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.SHA256.apply(this._target, arguments); + } + + public async SHA384( + value: string | ArrayBuffer, + encoding = "utf8" + ): Promise<ArrayBuffer> { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.SHA384.apply(this._target, arguments); + } + + public async SHA512( + value: string | ArrayBuffer, + encoding = "utf8" + ): Promise<ArrayBuffer> { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.SHA512.apply(this._target, arguments); + } + + public async MD5( + value: string | ArrayBuffer, + encoding = "utf8" + ): Promise<ArrayBuffer> { + // eslint-disable-next-line prefer-spread,prefer-rest-params + return this._target.MD5.apply(this._target, arguments); + } +} + +const service: ICryptoService = new CryptoServiceProxy(); export default service; -- GitLab