generated from cubetiq/ts-project
Task: Add default and e2e encryption package module for cryptojs
This commit is contained in:
3
src/config/index.ts
Normal file
3
src/config/index.ts
Normal 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"
|
||||
43
src/crypto/default-crypto.ts
Normal file
43
src/crypto/default-crypto.ts
Normal 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
30
src/crypto/e2e-crypto.ts
Normal 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
4
src/crypto/index.ts
Normal 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 }
|
||||
@@ -1 +1 @@
|
||||
console.log('Welcome to TS Project!');
|
||||
export {}
|
||||
|
||||
10
src/util/file.util.ts
Normal file
10
src/util/file.util.ts
Normal 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
12
src/util/index.ts
Normal 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 }
|
||||
Reference in New Issue
Block a user