generated from cubetiq/ts-project
Task: Add default and e2e encryption package module for cryptojs
This commit is contained in:
parent
5b75d272c5
commit
9d2a43feb9
17
README.md
17
README.md
@ -1,14 +1,15 @@
|
|||||||
# TS Project (Template for TypeScript)
|
# CUBETIQ CryptoJS
|
||||||
|
|
||||||
[![Build Status](https://dci.osa.cubetiqs.com/api/badges/CUBETIQ/ts-project/status.svg)](https://dci.osa.cubetiqs.com/CUBETIQ/ts-project)
|
- Default Encryption
|
||||||
|
- E2E Encryption
|
||||||
|
|
||||||
- TypeScript
|
### Generate for Default Encryption
|
||||||
- TS Node Dev
|
|
||||||
- Husky
|
|
||||||
- Prettier
|
|
||||||
- ESLint
|
|
||||||
|
|
||||||
...
|
```js
|
||||||
|
const key = crypto.randomBytes(32)
|
||||||
|
const iv = crypto.randomBytes(16)
|
||||||
|
```
|
||||||
|
|
||||||
### Contributors
|
### Contributors
|
||||||
|
|
||||||
- Sambo Chea <sombochea@cubetiqs.com>
|
- Sambo Chea <sombochea@cubetiqs.com>
|
10
package.json
10
package.json
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@cubetiq/ts-project",
|
"name": "@cubetiq/cryptojs",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "TS project template",
|
"description": "A basic cryptojs for cubetiq application data encryption and protection.",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "ts-node-dev --respawn --transpile-only src/index.ts",
|
"start": "ts-node-dev --respawn --transpile-only src/index.ts",
|
||||||
@ -12,11 +12,13 @@
|
|||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://git.cubetiqs.com/CUBETIQ/ts-project.git"
|
"url": "https://git.cubetiqs.com/CUBETIQ/cubetiq-cryptojs.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"TypeScript",
|
"TypeScript",
|
||||||
"Nodejs"
|
"Nodejs",
|
||||||
|
"Crypto",
|
||||||
|
"Encryption"
|
||||||
],
|
],
|
||||||
"author": "Sambo Chea <sombochea@cubetiqs.com>",
|
"author": "Sambo Chea <sombochea@cubetiqs.com>",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
|
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 }
|
Loading…
Reference in New Issue
Block a user