Task: Completed for cryptojs and tests functions and validate keys

This commit is contained in:
2022-02-14 12:30:32 +07:00
parent 9d2a43feb9
commit fd27e6f9dc
23 changed files with 324 additions and 24 deletions

View File

@@ -3,11 +3,11 @@ import {
DEFAULT_AES_ALGORITHM,
DEFAULT_BUFFER_TYPE,
DEFAULT_ENCODING_TYPE,
} from "../config"
import { stringToBuffer } from "../util"
} from "../../config"
import { bufferToString, stringToBuffer } from "../../util"
const encrypt = (
data: string,
data: string | Buffer,
key: string | Buffer,
iv?: string | Buffer | null
): string => {
@@ -16,8 +16,9 @@ const encrypt = (
stringToBuffer(key),
stringToBuffer(iv || key)
)
const _data = bufferToString(data)
let encrypted = cipher.update(
data,
_data,
DEFAULT_ENCODING_TYPE,
DEFAULT_BUFFER_TYPE
)
@@ -25,14 +26,19 @@ const encrypt = (
return encrypted
}
const decrypt = (data: string, key: Buffer, iv?: Buffer | null): string => {
const decrypt = (
data: string | Buffer,
key: string | Buffer,
iv?: string | Buffer | null
): string => {
const decipher = crypto.createDecipheriv(
DEFAULT_AES_ALGORITHM,
stringToBuffer(key),
stringToBuffer(iv || key)
)
const _data = bufferToString(data)
let decrypted = decipher.update(
data,
_data,
DEFAULT_BUFFER_TYPE,
DEFAULT_ENCODING_TYPE
)

View File

@@ -1,25 +1,23 @@
import * as crypto from "crypto"
import { DEFAULT_BUFFER_TYPE, DEFAULT_ENCODING_TYPE } from "../config"
import { readFileString, stringToBuffer } from "../util"
import { DEFAULT_BUFFER_TYPE, DEFAULT_ENCODING_TYPE } from "../../config"
import { stringToBuffer } from "../../util"
const encrypt = (data: string | Buffer, privOrPubKeyPath: string): string => {
const key = readFileString(privOrPubKeyPath)
const encrypt = (data: string | Buffer, privateOrPublicKey: string): string => {
const buffer = stringToBuffer(data)
const encrypted = crypto.publicEncrypt(key, buffer)
const encrypted = crypto.publicEncrypt(privateOrPublicKey, buffer)
return encrypted.toString(DEFAULT_BUFFER_TYPE)
}
const decrypt = (
data: string | Buffer,
privateKeyPath: string,
privateKey: string,
passphrase?: string | null
): string => {
const key = readFileString(privateKeyPath)
const buffer = stringToBuffer(data)
const decrypted = crypto.privateDecrypt(
{
key: key,
key: privateKey,
passphrase: passphrase || "",
},
buffer

21
src/crypto/crypto.ts Normal file
View File

@@ -0,0 +1,21 @@
import { ICryptoProvider } from "./provider.crypto"
export class CryptoProvider {
constructor(private readonly provider: ICryptoProvider) {}
public encrypt(data: string | Buffer): string {
return this.provider.encrypt(data)
}
public decrypt(data: string): string {
return this.provider.decrypt(data)
}
private static _instance: CryptoProvider | null | undefined
public static newInstance(provider: ICryptoProvider): CryptoProvider {
if (!CryptoProvider._instance) {
CryptoProvider._instance = new CryptoProvider(provider)
}
return CryptoProvider._instance
}
}

View File

@@ -1,4 +1,19 @@
import { decrypt, encrypt } from "./default-crypto"
import { encrypt as e2eEncrypt, decrypt as e2eDecrypt } from "./e2e-crypto"
import { decrypt, encrypt } from "./core/default.crypto"
import { encrypt as e2eEncrypt, decrypt as e2eDecrypt } from "./core/e2e.crypto"
import { CryptoProvider } from "./crypto"
import { ICryptoProvider } from "./provider.crypto"
import { DefaultCryptoProvider } from "./provider/default.provider"
import { E2ECryptoProvider } from "./provider/e2e.provider"
export { encrypt, decrypt, e2eEncrypt, e2eDecrypt }
export {
encrypt,
decrypt,
e2eEncrypt,
e2eDecrypt,
// Some implementations of ICryptoProvider
ICryptoProvider,
DefaultCryptoProvider,
E2ECryptoProvider,
// Core crypto functions
CryptoProvider,
}

View File

@@ -0,0 +1,4 @@
export interface ICryptoProvider {
encrypt(data: string | Buffer): string
decrypt(data: string): string
}

View File

@@ -0,0 +1,33 @@
import { assertNotNullOrUndefined } from "../../util"
import { ICryptoProvider } from "../provider.crypto"
import { decrypt as dec, encrypt as enc } from "./../core/default.crypto"
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)
this._key = json.key
this._iv = json.iv
} else {
this._key = key
this._iv = iv || key
}
assertNotNullOrUndefined(this._key, "key is required")
}
encrypt(data: string | Buffer): string {
return enc(data, this._key!, this._iv)
}
decrypt(data: string): string {
return dec(data, this._key!, this._iv)
}
}

View File

@@ -0,0 +1,82 @@
import { e2eDecrypt, e2eEncrypt } from ".."
import {
assertNotNullOrUndefined,
isNullOrUndefinedOrEmpty,
readFileToString,
} from "../../util"
import { ICryptoProvider } from "../provider.crypto"
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
) {
let isPrivateKeyInitialized = false
let isPublicKeyInitialized = false
if (privateKeyPath) {
this.privateKey = readFileToString(privateKeyPath)
isPrivateKeyInitialized = !isNullOrUndefinedOrEmpty(this.privateKey)
}
if (publicKeyPath) {
this.publicKey = readFileToString(publicKeyPath)
isPublicKeyInitialized = !isNullOrUndefinedOrEmpty(this.publicKey)
}
if (!isPrivateKeyInitialized && !isPublicKeyInitialized && jsonPath) {
const json = require(jsonPath)
if (json.privateKey) {
this.privateKey = json.privateKey
isPrivateKeyInitialized = !isNullOrUndefinedOrEmpty(
this.privateKey
)
}
if (json.publicKey) {
this.publicKey = json.publicKey
isPublicKeyInitialized = !isNullOrUndefinedOrEmpty(
this.publicKey
)
}
if (!isPrivateKeyInitialized && json.privateKeyPath) {
this.privateKey = readFileToString(json.privateKeyPath)
isPrivateKeyInitialized = !isNullOrUndefinedOrEmpty(
this.privateKey
)
}
if (!isPublicKeyInitialized && json.publicKeyPath) {
this.publicKey = readFileToString(json.publicKeyPath)
isPublicKeyInitialized = !isNullOrUndefinedOrEmpty(
this.publicKey
)
}
}
if (!isPrivateKeyInitialized) {
this.privateKey = privateKey
}
if (!isPublicKeyInitialized) {
this.publicKey = publicKey
}
assertNotNullOrUndefined(this.privateKey, "Private key is required")
}
encrypt(data: string | Buffer): string {
const key = this.publicKey || this.privateKey
return e2eEncrypt(data, key!.toString())
}
decrypt(data: string): string {
return e2eDecrypt(data, this.privateKey!.toString())
}
}

View File

@@ -2,9 +2,13 @@ import * as fs from "fs"
import * as path from "path"
import { DEFAULT_ENCODING_TYPE } from "../config"
export const readFileStringDefaultEncoding = (
export const readFileToStringDefaultEncoding = (
relativeFilePath: string
): string => {
const absolutePath = path.resolve(relativeFilePath)
return fs.readFileSync(absolutePath, DEFAULT_ENCODING_TYPE)
}
export const readFileToJson = (relativeFilePath: string): any => {
return JSON.parse(readFileToStringDefaultEncoding(relativeFilePath))
}

View File

@@ -1,5 +1,11 @@
import { DEFAULT_BUFFER_TYPE } from "../config"
import { readFileStringDefaultEncoding } from "./file.util"
import { DEFAULT_BUFFER_TYPE, DEFAULT_ENCODING_TYPE } from "../config"
import { readFileToStringDefaultEncoding, readFileToJson } from "./file.util"
import {
assertNotNullOrUndefined,
assertNotNullOrUndefinedOrEmpty,
isNullOrUndefined,
isNullOrUndefinedOrEmpty,
} from "./test.util"
const stringToBuffer = (data: string | Buffer): Buffer => {
if (data instanceof Buffer) {
@@ -9,4 +15,21 @@ const stringToBuffer = (data: string | Buffer): Buffer => {
}
}
export { readFileStringDefaultEncoding as readFileString, stringToBuffer }
const bufferToString = (data: string | Buffer): string => {
if (data instanceof Buffer) {
return data.toString(DEFAULT_ENCODING_TYPE)
} else {
return data
}
}
export {
readFileToStringDefaultEncoding as readFileToString,
readFileToJson,
stringToBuffer,
bufferToString,
assertNotNullOrUndefined,
assertNotNullOrUndefinedOrEmpty,
isNullOrUndefined,
isNullOrUndefinedOrEmpty,
}

22
src/util/test.util.ts Normal file
View File

@@ -0,0 +1,22 @@
export const assertNotNullOrUndefined = (value: unknown, msg?: string) => {
if (isNullOrUndefined(value)) {
throw new Error(msg || "value is null or undefined")
}
}
export const assertNotNullOrUndefinedOrEmpty = (
value: unknown,
msg?: string
) => {
if (isNullOrUndefinedOrEmpty(value)) {
throw new Error(msg || "value is null or undefined or empty")
}
}
export const isNullOrUndefined = (value: unknown): boolean => {
return value === null || value === undefined
}
export const isNullOrUndefinedOrEmpty = (value: unknown): boolean => {
return isNullOrUndefined(value) || value === ""
}