-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvideoEncryptor.js
56 lines (41 loc) · 1.5 KB
/
videoEncryptor.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
const fs = require("fs");
const crypto = require("crypto");
const path = require("path");
class VideoEncryptor {
constructor() {}
encryptVideo(videoPath, encryptionKey, outputPath) {
if (!fs.existsSync(videoPath)) {
throw new Error(`Video file does not exist at ${videoPath}`);
}
const videoBytes = fs.readFileSync(videoPath);
const cipher = crypto.createCipher("aes-256-cbc", encryptionKey);
let encryptedBytes = Buffer.concat([
cipher.update(videoBytes),
cipher.final(),
]);
outputPath = this.replaceExtension(outputPath, "encrypted");
fs.writeFileSync(outputPath, encryptedBytes);
return `Video encrypted and saved as ${outputPath}`;
}
decryptVideo(encryptedVideoPath, decryptionKey, outputPath) {
if (!fs.existsSync(encryptedVideoPath)) {
throw new Error(
`Encrypted video file does not exist at ${encryptedVideoPath}`
);
}
const encryptedBytes = fs.readFileSync(encryptedVideoPath);
const decipher = crypto.createDecipher("aes-256-cbc", decryptionKey);
let decryptedBytes = Buffer.concat([
decipher.update(encryptedBytes),
decipher.final(),
]);
outputPath = this.replaceExtension(outputPath, "mp4");
fs.writeFileSync(outputPath, decryptedBytes);
return `Video decrypted and saved as ${outputPath}`;
}
replaceExtension(filePath, newExtension) {
const { dir, name } = path.parse(filePath);
return path.join(dir, `${name}.${newExtension}`);
}
}
module.exports = VideoEncryptor;