Skip to content

Commit 23145ba

Browse files
committed
Merge branch 'release/v1' into main
2 parents ccc7790 + ee3267e commit 23145ba

File tree

6 files changed

+224
-0
lines changed

6 files changed

+224
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# secutilsfiles
22
simple utils to crypt / decrypt files with AES
3+
4+
5+
#

package-lock.json

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "secutilsfiles",
3+
"version": "1.0.0",
4+
"description": "A simple package to crypt and decrypt files",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"start": "tsc && node .",
8+
"test": "node test"
9+
},
10+
"files": [
11+
"./"
12+
],
13+
"author": "Paul Millet <[email protected]>",
14+
"license": "MIT",
15+
"dependencies": {
16+
"@types/node": "^16.7.2",
17+
"aes256": "^1.1.0",
18+
"cryptr": "^6.0.2",
19+
"dotenv": "^10.0.0",
20+
"typescript": "^4.4.2"
21+
},
22+
"repository": {
23+
"type": "git",
24+
"url": "git+https://github.com/bigopenworld/secutilsfiles.git"
25+
},
26+
"bugs": {
27+
"url": "https://github.com/bigopenworld/secutilsfiles/issues"
28+
},
29+
"homepage": "https://github.com/bigopenworld/secutilsfiles/issues",
30+
"devDependencies": {
31+
"@types/cryptr": "^4.0.1"
32+
}
33+
}

src/index.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// dependency import
2+
3+
import Cryptr from "cryptr"
4+
import fs from "fs"
5+
import { promisify } from "util"
6+
7+
// Define const
8+
9+
10+
const readdir = promisify(fs.readdir) // promisify the fs.readdir
11+
const readfile = promisify(fs.readFile) // promisify the fs.readFile
12+
const writefile = promisify(fs.writeFile) // promisify the fs.writeFile
13+
const randomChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
14+
const randomcharlengh = randomChar.length;
15+
16+
/** Read dir function
17+
* @param {string} directory take the directory
18+
* @return {Promise<string[]>} return a promise of files path
19+
* Will be use when encrypt dir feature will be created
20+
*/
21+
async function readDir(directory : string) : Promise<string[]> {
22+
let files = await readdir(directory)
23+
return files.map(file => `${directory}/${file}`)
24+
}
25+
26+
/** Read file function
27+
* @param {string} path take the path
28+
* @return {Promise<string>} return promise of data
29+
*/
30+
async function readFile(path : string) : Promise<string> {
31+
let files : Buffer = await readfile(path)
32+
return files.toString()
33+
}
34+
35+
/** RandomString function
36+
* @param {number} length take the number of element in your string
37+
* @return {string} Return the random string
38+
*/
39+
function RandomString(length : number) : string {
40+
var result : string
41+
for ( var i = 0; i < length; i++ ) {
42+
result += randomChar.charAt(Math.floor(Math.random() * randomcharlengh));
43+
}
44+
return result
45+
}
46+
47+
/** CryptFile function
48+
* @param {string} path take the path of the input file
49+
* @param {string} dest take the path of the destination file
50+
* @param {string | undefined} key take the key if none specified one will be generated
51+
* @return {string} Return the key
52+
*/
53+
async function CryptFile(path : string, dest : string, key? : string) : Promise<string> {
54+
let enckey = key
55+
if (!enckey) {
56+
enckey = RandomString(20)
57+
}
58+
let crypt = new Cryptr(enckey)
59+
const data = await readFile(path)
60+
const dataenc = crypt.encrypt(data)
61+
await writefile(dest, dataenc)
62+
return enckey
63+
}
64+
65+
/** CryptFile function
66+
* @param {string} path take the path of the input file
67+
* @param {string} dest take the path of the destination file
68+
* @param {string} key take the secret key
69+
* @return {string} Return the key
70+
*/
71+
async function DecryptFile(path : string, dest : string, key : string) : Promise<string> {
72+
let enckey = key
73+
let crypt = new Cryptr(enckey)
74+
const data = await readFile(path)
75+
const dataenc = crypt.decrypt(data)
76+
await writefile(dest, dataenc)
77+
return enckey
78+
}
79+

tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"esModuleInterop": true,
5+
"target": "es6",
6+
"moduleResolution": "node",
7+
"sourceMap": true,
8+
"outDir": "dist"
9+
},
10+
"lib": ["es2015"]
11+
}

0 commit comments

Comments
 (0)