-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcrypt.js
More file actions
124 lines (109 loc) · 3.77 KB
/
Copy pathcrypt.js
File metadata and controls
124 lines (109 loc) · 3.77 KB
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
const { createCipheriv, createDecipheriv, generateKeyPairSync, publicEncrypt, privateDecrypt, constants } = require('crypto');
//const { writeFileSync, readFileSync, existsSync, mkdirSync } = require('fs');
//const path = require('path');
class CryptorAES {
constructor(password, iv) {
this.password = password
this.iv = iv
}
encrypt(chunk) {
let cipher = createCipheriv("aes-256-ctr", this.password, this.iv);
let result = Buffer.concat([cipher.update(chunk), cipher.final()]);
return result;
}
decrypt(chunk) {
let decipher = createDecipheriv("aes-256-ctr", this.password, this.iv);
let result = Buffer.concat([decipher.update(chunk), decipher.final()]);
return result;
}
}
/*
// EXPERIMENTAL
class CryptorRSA {
constructor(publicKeyPath, privateKeyPath) {
this.publicKey
this.privateKey
if (publicKeyPath === undefined || privateKeyPath === undefined){
console.log("No keys have been defined. Checking for existing key pair.")
try {
if (!existsSync('./keys') === true){
console.log("Default keys dir does not exist.")
mkdirSync(path.join(__dirname, 'keys'), (err) => {
console.log("created keys dir")
console.log("generating new key pair and loading them.")
this.generateAndLoad()
})
} else {
if(existsSync('./keys/public.pem') && existsSync('./keys/private.pem')){
console.log("Found existing keys.")
this.loadKeys("./keys/public.pem", "./keys/private.pem")
} else {
this.generateAndLoad()
}
}
} catch (error) {
console.error(error)
}
} else {
this.loadKeys(publicKeyPath, privateKeyPath)
}
}
loadKeys(publicKeyPath, privateKeyPath){
try {
const publicKey = readFileSync(publicKeyPath)
const privateKey = readFileSync(privateKeyPath)
this.publicKey = publicKey
this.privateKey = privateKey
} catch (error) {
console.error(error)
}
}
generateNewKeyPair() {
const { publicKey, privateKey } = generateKeyPairSync("rsa", {
modulusLength: 2048,
publicKeyEncoding: {
type: "pkcs1",
format: "pem",
},
privateKeyEncoding: {
type: "pkcs1",
format: "pem",
}
})
try {
console.log("Writing pub/priv keys to files.")
writeFileSync("./keys/public.pem", publicKey);
writeFileSync("./keys/private.pem", privateKey);
console.log("Done.")
} catch (error) {
console.error(error)
}
}
generateAndLoad(){
this.generateNewKeyPair()
this.loadKeys("./keys/public.pem", "./keys/private.pem")
}
encrypt(cleartext) {
const encryptedData = publicEncrypt(
{
key: this.publicKey,
padding: constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},
Buffer.from(cleartext)
);
return encryptedData
}
decrypt(buffer) {
const decryptedData = privateDecrypt(
{
key: this.privateKey,
padding: constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},
buffer
);
return (decryptedData.toString());
}
} */
module.exports = { CryptorAES }