|
| 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 | + |
0 commit comments