-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.js
167 lines (131 loc) · 3.99 KB
/
decrypt.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
const alert = require('cli-alerts');
const fs = require('fs');
const jimp = require('jimp');
const path = require('path');
const decrypt = async flags => {
// check if flags contain decrypt flag
if (flags.encrypt) {
alert({
type: `warning`,
name: `Invalid combination of flags`,
msg: `Cannot use both --encrypt and --decrypt flags together`
});
process.exit(1);
}
// find the value of the decrypt flag
const filePath = flags.decrypt;
// check if the filePath is a valid file path
if (!filePath) {
alert({
type: `warning`,
name: `Invalid file path`,
msg: `Please provide a valid file path`
});
process.exit(1);
}
// check if the filePath is a valid file path
if (!fs.existsSync(filePath)) {
alert({
type: `warning`,
name: `Invalid file path`,
msg: `Please provide a valid file path`
});
process.exit(1);
}
// check if the key is in the flags
if (!flags.key) {
alert({
type: `warning`,
name: `Invalid key`,
msg: `Please provide a valid key with --key/-k`
});
process.exit(1);
}
try {
const ora = (await import('ora')).default;
const spinner = ora(`Reading Image...`).start();
const image = await jimp.read(filePath);
// get the image extension using jimp
const extension = image.getExtension();
// get the rgba values of the image
const rgba = image.bitmap.data;
// get the length of the rgba array
const length = rgba.length;
spinner.succeed(`Image read successfully`);
const spinner2 = ora(`Getting the key`).start();
// get the key
const keyPath = flags.key;
// check if the keyPath is a valid file path
if (!fs.existsSync(keyPath)) {
spinner2.fail(`Invalid key path`);
alert({
type: `error`,
name: `Invalid key path`,
msg: `Please provide a valid key path with --key/-k`
});
process.exit(1);
}
// get the base64 encoded key
const key = fs.readFileSync(keyPath, 'utf8');
// decode the key
const keyDecoded = Buffer.from(key, 'base64');
const keyArray = Array.from(keyDecoded);
// check if the key is the same length as the rgba array
if (keyArray.length !== length) {
spinner2.fail(`Invalid key`);
alert({
type: `error`,
name: `Invalid key`,
msg: `The key is not valid`
});
process.exit(1);
}
spinner2.succeed(`Key read successfully`);
const spinner3 = ora(`Decrypting...`).start();
// loop through the rgba array
for (let i = 0; i < length; i++) {
const k = keyArray[i];
rgba[i] = rgba[i] ^ k;
}
// save the image with _decrypted appended to the file name, mimeType and the new extension
image.bitmap.data = rgba;
spinner3.succeed(`Decryption successful`);
// save the image
// get file base name before _encrypted
const spinner4 = ora(`Saving image...`).start();
const fileName = path
.basename(filePath)
// remove _encrypted from the file name if present
.replace(/\_encrypted$/, '');
// remove the extension from the file name
let fileNameWithoutExtension = `${fileName.split('.')[0]}_decrypted`;
if (flags.outputImageFileName) {
fileNameWithoutExtension = flags.outputImageFileName.split('.')[0];
}
if (fs.existsSync(`${fileNameWithoutExtension}.${extension}`)) {
console.log(flags);
spinner4.fail(`Output image file already exists`);
alert({
type: `error`,
name: `Output image file already exists: ${fileNameWithoutExtension}.${extension}`,
msg: `Please provide a different file name with --outputImageFileName/-i flag`
});
process.exit(1);
}
image.write(`${fileNameWithoutExtension}.${extension}`);
spinner4.succeed(`Image saved successfully`);
alert({
type: `success`,
name: `Success`,
msg: `Image decrypted successfully\n
Decrypted Image: ${fileNameWithoutExtension}.${extension}`
});
} catch (err) {
alert({
type: `error`,
name: `Error`,
msg: `${err}`
});
}
};
module.exports = decrypt;