Task: Add default and e2e encryption package module for cryptojs

This commit is contained in:
2022-02-14 10:54:25 +07:00
parent 5b75d272c5
commit 9d2a43feb9
9 changed files with 119 additions and 14 deletions

3
src/config/index.ts Normal file
View File

@@ -0,0 +1,3 @@
export const DEFAULT_AES_ALGORITHM = "aes-256-cbc"
export const DEFAULT_BUFFER_TYPE = "base64"
export const DEFAULT_ENCODING_TYPE = "utf-8"

View File

@@ -0,0 +1,43 @@
import * as crypto from "crypto"
import {
DEFAULT_AES_ALGORITHM,
DEFAULT_BUFFER_TYPE,
DEFAULT_ENCODING_TYPE,
} from "../config"
import { stringToBuffer } from "../util"
const encrypt = (
data: string,
key: string | Buffer,
iv?: string | Buffer | null
): string => {
const cipher = crypto.createCipheriv(
DEFAULT_AES_ALGORITHM,
stringToBuffer(key),
stringToBuffer(iv || key)
)
let encrypted = cipher.update(
data,
DEFAULT_ENCODING_TYPE,
DEFAULT_BUFFER_TYPE
)
encrypted += cipher.final(DEFAULT_BUFFER_TYPE)
return encrypted
}
const decrypt = (data: string, key: Buffer, iv?: Buffer | null): string => {
const decipher = crypto.createDecipheriv(
DEFAULT_AES_ALGORITHM,
stringToBuffer(key),
stringToBuffer(iv || key)
)
let decrypted = decipher.update(
data,
DEFAULT_BUFFER_TYPE,
DEFAULT_ENCODING_TYPE
)
decrypted += decipher.final(DEFAULT_ENCODING_TYPE)
return decrypted
}
export { encrypt, decrypt }

30
src/crypto/e2e-crypto.ts Normal file
View File

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

4
src/crypto/index.ts Normal file
View File

@@ -0,0 +1,4 @@
import { decrypt, encrypt } from "./default-crypto"
import { encrypt as e2eEncrypt, decrypt as e2eDecrypt } from "./e2e-crypto"
export { encrypt, decrypt, e2eEncrypt, e2eDecrypt }

View File

@@ -1 +1 @@
console.log('Welcome to TS Project!');
export {}

10
src/util/file.util.ts Normal file
View File

@@ -0,0 +1,10 @@
import * as fs from "fs"
import * as path from "path"
import { DEFAULT_ENCODING_TYPE } from "../config"
export const readFileStringDefaultEncoding = (
relativeFilePath: string
): string => {
const absolutePath = path.resolve(relativeFilePath)
return fs.readFileSync(absolutePath, DEFAULT_ENCODING_TYPE)
}

12
src/util/index.ts Normal file
View File

@@ -0,0 +1,12 @@
import { DEFAULT_BUFFER_TYPE } from "../config"
import { readFileStringDefaultEncoding } from "./file.util"
const stringToBuffer = (data: string | Buffer): Buffer => {
if (data instanceof Buffer) {
return data
} else {
return Buffer.from(data, DEFAULT_BUFFER_TYPE)
}
}
export { readFileStringDefaultEncoding as readFileString, stringToBuffer }