Task: Completed tests and fixed for default and e2e encryption module

This commit is contained in:
2022-02-14 15:59:51 +07:00
parent fd27e6f9dc
commit 23ee44e0e6
10 changed files with 188 additions and 132 deletions

View File

@@ -1,20 +1,33 @@
import { assertNotNullOrUndefined } from "../../util"
import {
assertNotNullOrUndefined,
readFileToJson,
readFileToString,
} from "../../util"
import { ICryptoProvider } from "../provider.crypto"
import { decrypt as dec, encrypt as enc } from "./../core/default.crypto"
interface DefaultCryptoProviderOptions {
key?: string | Buffer | null | undefined
iv?: string | Buffer | null | undefined
keyiVPath?: string | null | undefined
jsonPath?: string | null | undefined
}
export class DefaultCryptoProvider implements ICryptoProvider {
private _key: string | Buffer | null | undefined
private _iv: string | Buffer | null | undefined
constructor(
key?: string | Buffer | null,
iv?: string | Buffer | null,
keyJsonFilePath?: string | null
) {
if (keyJsonFilePath) {
const json: any = require(keyJsonFilePath)
constructor(options: DefaultCryptoProviderOptions) {
const { key, iv, keyiVPath, jsonPath } = options
if (keyiVPath) {
const json: any = readFileToJson(keyiVPath)
this._key = json.key
this._iv = json.iv
} else if (jsonPath) {
const json: any = readFileToJson(jsonPath)
this._key = readFileToString(json.keyPath)
this._iv = readFileToString(json.ivPath)
} else {
this._key = key
this._iv = iv || key

View File

@@ -2,21 +2,32 @@ import { e2eDecrypt, e2eEncrypt } from ".."
import {
assertNotNullOrUndefined,
isNullOrUndefinedOrEmpty,
readFileToJson,
readFileToString,
} from "../../util"
import { ICryptoProvider } from "../provider.crypto"
interface E2ECryptoProviderOptions {
privateKey?: string | Buffer | null | undefined
publicKey?: string | Buffer | null | undefined
privateKeyPath?: string | null | undefined
publicKeyPath?: string | null | undefined
jsonPath?: string | null | undefined
}
export class E2ECryptoProvider implements ICryptoProvider {
private privateKey: string | Buffer | null | undefined
private publicKey: string | Buffer | null | undefined
constructor(
privateKey?: string | Buffer | null | undefined,
publicKey?: string | Buffer | null | undefined,
privateKeyPath?: string | null | undefined,
publicKeyPath?: string | null | undefined,
jsonPath?: string | null | undefined
) {
constructor(options: E2ECryptoProviderOptions) {
const {
privateKey,
publicKey,
privateKeyPath,
publicKeyPath,
jsonPath,
} = options
let isPrivateKeyInitialized = false
let isPublicKeyInitialized = false
if (privateKeyPath) {
@@ -30,7 +41,7 @@ export class E2ECryptoProvider implements ICryptoProvider {
}
if (!isPrivateKeyInitialized && !isPublicKeyInitialized && jsonPath) {
const json = require(jsonPath)
const json = readFileToJson(jsonPath)
if (json.privateKey) {
this.privateKey = json.privateKey
isPrivateKeyInitialized = !isNullOrUndefinedOrEmpty(

View File

@@ -1 +1,7 @@
export {}
import {
CryptoProvider,
DefaultCryptoProvider,
E2ECryptoProvider,
} from "./crypto"
export { CryptoProvider, DefaultCryptoProvider, E2ECryptoProvider }