-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
219 lines (188 loc) · 5.05 KB
/
extension.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const fs = require('fs');
const crypto = require("crypto");
const vscode = require('vscode');
function activate(context) {
let disposable = vscode.commands.registerCommand('safe_markdown.encrypt', function () {
safe_markdown_encrypt();
});
let disposable2 = vscode.commands.registerCommand('safe_markdown.decrypt', function () {
safe_markdown_decrypt();
});
context.subscriptions.push(disposable);
context.subscriptions.push(disposable2);
}
function deactivate() { }
module.exports = {
activate,
deactivate
}
let template = {
"key": "rsa(uuid)",
"body": "aes(txt,uuid)",
"make_ts": "2006-01-02 15:04:05",
"edit_count": 1,
"last_ts": "2006-01-02 15:04:05"
}
async function safe_markdown_encrypt() {
if (process.env.VSCODE_SAFE_MARKDOWN === undefined) {
vscode.window.showErrorMessage(`env VSCODE_SAFE_MARKDOWN=/path-to/your_rsa, `);
return;
}
let rsa_pub_path = process.env.VSCODE_SAFE_MARKDOWN + ".pub";
let activeDoc = vscode.window.activeTextEditor.document;
// if txt is encrypt return
try {
let data = JSON.parse(activeDoc.getText());
if (data.key) {
vscode.window.showErrorMessage(`file is encrypted txt`);
return;
}
} catch (e) {
//
}
let pem = "";
try {
pem = fs.readFileSync(rsa_pub_path, { encoding: "ascii" });
} catch (e) {
vscode.window.showErrorMessage(`pub file error`);
console.log(e);
return
}
let pubKey;
try {
pubKey = crypto.createPublicKey({
key: pem,
format: "pem",
});
} catch (e) {
vscode.window.showErrorMessage(`pub key error`);
console.log(e);
return
}
let uuid;
try {
uuid = Buffer.from(make_uuid(), 'hex')
if (uuid.length != 16) {
vscode.window.showErrorMessage(`uuid make error`);
return;
}
} catch (e) {
vscode.window.showErrorMessage(`uuid make error`);
console.log(e)
return
}
// console.log("===uuid", uuid.toString('hex'));
const ecryptedKeyData = crypto.publicEncrypt(
{
key: pubKey,
padding: crypto.constants.RSA_PKCS1_PADDING,
},
uuid,//Buffer.from(activeDoc.getText()),
);
let encrypt_body = aes_encrypt(Buffer.from(activeDoc.getText(), 'utf8'), uuid);
vscode.window.activeTextEditor.edit(editBuilder => {
var lastLine = activeDoc.lineAt(activeDoc.lineCount - 1);
var textRange = new vscode.Range(0, 0, activeDoc.lineCount - 1, lastLine.range.end.character);
editBuilder.replace(textRange, JSON.stringify({
"key": ecryptedKeyData.toString('hex'),
"body": Buffer.from(encrypt_body, 'hex').toString('hex'),
}));
});
}
async function safe_markdown_decrypt() {
if (process.env.VSCODE_SAFE_MARKDOWN === undefined) {
vscode.window.showErrorMessage(`env VSCODE_SAFE_MARKDOWN=/path-to/your_rsa`);
return;
}
let rsa_prv_path = process.env.VSCODE_SAFE_MARKDOWN;
let activeDoc = vscode.window.activeTextEditor.document;
let data;
try {
data = JSON.parse(activeDoc.getText());
} catch (e) {
vscode.window.showErrorMessage(`file not json`);
console.log(e);
return;
}
// console.log(data.key);
// let cfg = vscode.workspace.getConfiguration('safe_markdown');
let pem = "";
try {
pem = fs.readFileSync(rsa_prv_path, { encoding: "ascii" });
} catch (e) {
vscode.window.showErrorMessage(`pem file error`);
console.log(e);
return
}
let pass = await vscode.window.showInputBox(
{
value: "",
prompt: "your pri pem pass",
placeHolder: "",
password: true,
});
let priKey;
try {
priKey = crypto.createPrivateKey({
key: pem,
format: "pem",
type: "pkcs1",
passphrase: pass,
});
} catch (e) {
vscode.window.showErrorMessage(`your pem pass error`);
console.log(e);
return
}
let decryptedKeyData;
try {
decryptedKeyData = crypto.privateDecrypt({
key: priKey,
padding: crypto.constants.RSA_PKCS1_PADDING,
},
Buffer.from(data.key, 'hex'),
);
} catch (e) {
return;
}
// console.log("uuid", decryptedKeyData.toString('hex'));
let body;
try {
body = Buffer.from(aes_decrypt(data.body, decryptedKeyData), 'hex').toString('utf8');
}
catch (e) {
vscode.window.showErrorMessage(`aes decrypt error`);
console.log(e);
return
}
vscode.window.activeTextEditor.edit(editBuilder => {
var lastLine = activeDoc.lineAt(activeDoc.lineCount - 1);
var textRange = new vscode.Range(0, 0, activeDoc.lineCount - 1, lastLine.range.end.character);
editBuilder.replace(textRange, body);
});
}
// key len([]byte)=16
function aes_encrypt(plaintext, key) {
var cip, encrypted; encrypted = '';
cip = crypto.createCipheriv('aes-128-cbc', Buffer.from(key, 'hex'), Buffer.from(key, 'hex'));
encrypted += cip.update(plaintext, 'hex', 'hex');
encrypted += cip.final('hex');
return encrypted;
}
function aes_decrypt(encrypted, key) {
var _decipher, decrypted, err;
decrypted = '';
_decipher = crypto.createDecipheriv('aes-128-cbc', Buffer.from(key, 'hex'), Buffer.from(key, 'hex'));
decrypted += _decipher.update(encrypted, 'hex', 'hex');
decrypted += _decipher.final('hex');
return decrypted;
}
function make_uuid() {
var s = [];
var hexDigits = "0123456789abcdef";
for (var i = 0; i < 32; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
var uuid = s.join("");
return uuid;
}