-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.js
186 lines (164 loc) · 4.57 KB
/
aes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/node
const minimist = require('minimist');
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const AES = require('./src/implementation');
const encrypt = AES.encrypt;
const decrypt = AES.decrypt;
const directoryName = path.resolve(__dirname);
/**
* @description - Starts the AES process
* @param {Object} args
*/
function start(args) {
if (args.h || args.help) {
printUsage();
return;
}
if (args.v || args.version) {
printVersion();
return;
}
const keysize = getKeysize(process.env.AES_KEYSIZE || args.keysize);
if (!keysize) {
console.log();
printUsage();
return;
}
const mode = getMode(process.env.AES_MODE || args.mode);
if (!mode) {
console.log();
printUsage();
return;
}
const outputFilename = process.env.AES_OUTPUT_FILE ||
args.outputfile || 'output.txt';
Promise.all([
readFile(process.env.AES_KEY_FILE || args.keyfile),
readFile(process.env.AES_INPUT_FILE || args.inputfile),
])
.then(function(args) {
const key = args[0];
const input = args[1];
if (mode === 'encrypt') {
return encrypt(keysize, key, input, outputFilename);
} else if (mode === 'decrypt') {
return decrypt(keysize, key, input, outputFilename);
}
})
.catch(function(err) {
console.error(chalk.red(err.stack));
});
}
/**
* @description - Prints the usage for the user
*/
function printUsage() {
console.log('Usage: node aes.js [options] [arguments | ' +
'--keysize <AES_KEYSIZE=[128|256]> ' +
'--keyfile <AES_KEY_FILE> ' +
'--inputfile <AES_INPUT_FILE> ' +
'--outputfile <AES_OUTPUT_FILE[output.txt]> ' +
'--mode <AES_MODE=[encrypt|decrypt]>' +
']'
);
console.log();
console.log('Options:');
console.log(chalk.gray('-v, --version ' +
'print AES.js version'));
console.log(chalk.gray('-h, --help ' +
'print this help message'));
console.log();
console.log('Arguments:');
console.log(chalk.gray('--keysize=<128|256> ' +
'size of the key for AES, either 128 or 256 bits'));
console.log(chalk.gray('--keyfile ' +
'filename containing a key of the specified size'));
console.log(chalk.gray('--inputfile ' +
'filename containing the input text'));
console.log(chalk.gray('--outputfile=[output.txt] ' +
'filename for the result'));
console.log(chalk.gray('--mode=<encrypt|decrypt> ' +
'mode in which to run the AES algorithm'));
}
/**
* @description - Prints the version number for AES.js
*/
function printVersion() {
try {
const packageFile = fs.readFileSync(
path.resolve(directoryName, 'package.json')
);
const packageJSON = JSON.parse(packageFile);
console.log('NodeJS');
console.log(chalk.gray(process.version));
console.log();
console.log('AES.js');
console.log(chalk.gray(`v${packageJSON.version}`));
} catch (err) {
console.error(chalk.red(err.stack));
}
}
/**
* @description - Used to get the keysize argument while checking for errors
* @param {String} arg - the arg passed in from `--keysize`
* @return {Number}
*/
function getKeysize(arg) {
// check keysize argument
if (!arg) {
console.error(chalk.red('Error: Must specify a keysize of 128 or 256'));
return null;
}
const keysize = Number(arg);
if (isNaN(keysize)) {
console.error(chalk.red('Error: Invalid keysize argument'));
return null;
}
if (keysize !== 128 && keysize !== 256) {
console.error(chalk.red('Error: Keysize must be either 128 or 256'));
return null;
}
return keysize;
}
/**
* @description - Used to retrieve the mode from the arguments list
* @param {String} arg
* @return {String}
*/
function getMode(arg) {
if (!arg) {
console.error(
chalk.red('Error: Must specify a mode-- either `encrypt` or `decrypt`')
);
return null;
}
const mode = arg.toLowerCase();
if (mode !== 'encrypt' && mode !== 'decrypt') {
console.error(chalk.red(`Error: Invalid mode '${arg}'`));
return null;
}
return mode;
}
/**
* @description - Helper method to validate and read a file into memory
* @param {String} filename
* @return {String}
*/
function readFile(filename) {
if (!filename) {
throw new Error(`Error: Invalid filename '${filename}'`);
}
return new Promise(function(resolve, reject) {
fs.readFile(path.resolve(filename), function(err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
const argv = minimist(process.argv.slice(2));
start(argv);