-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathextension.js
665 lines (643 loc) · 19.9 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
const vscode = require('vscode')
const caesar = require('./scripts/Caesar')
const vigenere = require('./scripts/Vigenere')
const morse = require('xmorse')
const basic = require('./scripts/Basic')
const manip = require('./scripts/textManipulation')
const crypto = require('crypto')
const fs = require('fs')
const fence = require('./scripts/Fence')
const jssm4 = require('jssm4')
/**
* 插件被激活时触发,所有代码总入口
* @param {*} context 插件上下文
*/
//格式化
String.prototype.format = function () {
var args = arguments
return this.replace(/\{(\d+)\}/gm, (ms, p1) => {
return typeof args[p1] == 'undefined' ? ms : args[p1]
})
}
//显示信息与更新文本
function outputChannelShow(text) {
outputChannel.show()
outputChannel.appendLine(text)
}
function outputChannelUpdate(alg, before, after, update) {
if (after) {
//output
outputChannelShow('------------------------------------')
outputChannelShow(' ◆ ' + alg + ':')
outputChannelShow('[ ⇐ ] ' + before)
outputChannelShow('[ ⇒ ] ' + after)
//update
if (update) {
editor.edit((editBuilder) => {
editBuilder.replace(selection, after)
})
outputChannelShow('[ ✔ ] 文本已自动更新,按 Ctrl + Z 撤销')
}
outputChannelShow('------------------------------------')
} else {
vscode.window.showErrorMessage('转换失败!请检查文本类型或查看日志!')
}
}
//凯撒
async function caesarCipher(text) {
result = caesar.caesar(text)
console.log(result)
let choise = await vscode.window.showQuickPick(result, {
placeHolder: '选择其中一种结果进行替换',
})
if (choise) {
vscode.window.showInformationMessage(
'选择了位移了' + result.indexOf(choise) + '位的结果',
)
outputChannelUpdate('caesarCipher', text, choise, true)
}
}
//位移
async function characterOffset(text) {
let value = await vscode.window.showInputBox({
prompt: '输入最大的位移位数n和方向l或r,以逗号隔开',
value: '50,l',
})
if (value) {
let n = value.split(',')[0]
let d = value.split(',')[1]
result = caesar.shift(text, n, d)
console.log(result)
let choise = await vscode.window.showQuickPick(result, {
placeHolder: '选择其中一种结果进行替换',
})
if (choise) {
vscode.window.showInformationMessage(
'选择了位移了' + result.indexOf(choise) + '位的结果',
)
outputChannelUpdate('characterOffset', text, choise, true)
}
}
}
//栅栏
async function fenceCipher(text, alg) {
let result = []
if (alg.includes('Encode')) {
for (var i = 2; i < text.length; i++) {
result.push(fence.encrypt(text, i))
}
} else {
for (var i = 2; i < text.length; i++) {
result.push(fence.decrypt(text, i))
}
}
console.log(result)
let choise = await vscode.window.showQuickPick(result, {
placeHolder: '选择其中一种结果进行替换',
})
if (choise) {
vscode.window.showInformationMessage(
'选择了栏数为' + (result.indexOf(choise) + 2) + '的结果',
)
outputChannelUpdate('fenceCipher', text, choise, true)
}
}
//维吉尼亚
async function vigenereCipher(text, alg) {
let key = await vscode.window.showInputBox({
placeHolder: '输入秘钥',
})
if (key) {
if (alg.includes('Encode')) {
var result = vigenere.Vigenere(text, key, true)
} else {
var result = vigenere.Vigenere(text, key, false)
}
outputChannelUpdate(alg, text, result, true)
}
}
async function vigenereAutoDecode(text) {
let minlen = 1 //从1开始猜测key的长度
while (true) {
let guessKey = new Promise((resolve) => {
let result = vigenere.deVigenereAuto(text, false, minlen, 100) //默认key的长度未知
resolve(result)
})
result = await guessKey
outputChannelUpdate('vigenereAutoDecode', text, result[0], false)
outputChannelShow('最有可能的key为:' + result[1])
minlen = result[2] + 1 //如果没猜对,从猜出的秘钥长度+1之后继续猜
let choise = await vscode.window.showInformationMessage(
'请在输出日志中确认秘钥和明文是否猜测正确?',
'是',
'否',
)
if (choise != '否') {
outputChannelUpdate('vigenereAutoDecode', text, result[0], true)
break
}
}
}
function cryptoCipher(plainText, algorithm, key, iv, encoding = 'base64') {
let cipher = crypto.createCipheriv(algorithm, key, iv)
return cipher.update(plainText, 'utf8', encoding) + cipher.final(encoding)
}
function cryptoDecipher(cipher, algorithm, key, iv, encoding = 'base64') {
let decipher = crypto.createDecipheriv(algorithm, key, iv)
return decipher.update(cipher, encoding, 'utf8') + decipher.final('utf8')
}
let latestSymmetricCryptionKeyIv
//对称加密
async function symmetricCryption(text, alg) {
let ciphers = crypto.getCiphers()
ciphers.includes('sm4-ecb') || ciphers.push('sm4-ecb')
let algorithm = await vscode.window.showQuickPick(ciphers, {
placeHolder: '请选择要使用的对称加密算法',
})
if (!algorithm) return
let value = await vscode.window.showInputBox({
placeHolder: '请输入秘钥和初始向量(如有),并以英文“,”隔开',
value: latestSymmetricCryptionKeyIv || '',
})
latestSymmetricCryptionKeyIv = value
let key = value.split(',')[0] || ''
let iv = value.split(',')[1] || ''
outputChannelShow('Key: ' + key + ' IV: ' + iv)
let result
try {
if (alg.includes('Encryption')) {
result =
algorithm === 'sm4-ecb'
? new jssm4(key).encryptData_ECB(text)
: cryptoCipher(text, algorithm, key, iv)
} else {
result =
algorithm === 'sm4-ecb'
? new jssm4(key).decryptData_ECB(text)
: cryptoDecipher(text, algorithm, key, iv)
}
} catch (err) {
outputChannelShow('[ ✘ ] ' + err)
}
outputChannelUpdate(alg + ' - ' + algorithm, text, result, true)
}
//RSA加密
function checkKey(keyfile, key) {
let pri = [crypto.privateEncrypt, crypto.privateDecrypt]
let pub = [crypto.publicEncrypt, crypto.publicDecrypt]
if (key.includes('PRIVATE')) {
outputChannelShow('选择了私钥文件: ' + keyfile)
return pri
} else if (key.includes('PUBLIC')) {
outputChannelShow('选择了公钥文件: ' + keyfile)
return pub
} else {
outputChannelShow('[ ✘ ] 秘钥格式似乎不对')
outputChannelShow('私钥的开头和结尾分别为:')
outputChannelShow('-----BEGIN RSA PRIVATE KEY-----')
outputChannelShow('-----END RSA PRIVATE KEY-----')
outputChannelShow('公钥的开头和结尾分别为:')
outputChannelShow('-----BEGIN PUBLIC KEY-----')
outputChannelShow('-----END PUBLIC KEY-----')
}
}
async function rsaCryption(text, alg) {
try {
let file = await vscode.window.showOpenDialog({
openLabel: '选择秘钥',
})
if (file) {
let keyfile = file[0].fsPath
let key = fs.readFileSync(keyfile).toString('utf-8')
if (alg.includes('Encryption')) {
crypt = checkKey(keyfile, key)[0]
var result = crypt(key, new Buffer(text)).toString('base64')
} else {
crypt = checkKey(keyfile, key)[1]
var result = crypt(key, new Buffer(text, 'base64')).toString('utf8')
}
outputChannelUpdate(alg, text, result, true)
}
} catch (err) {
outputChannelShow(err)
}
}
//摩斯
async function morseCoding(text, alg) {
let value = await vscode.window.showInputBox({
value: "{ space: '/', long: '-', short: '.' }",
prompt: '请输入对应的间隔符、划、点的符号',
})
if (value) {
let option = eval('(' + value + ')')
if (alg.includes('Encode')) {
var result = morse.encode(text, option)
} else {
var result = morse.decode(text, option)
}
outputChannelUpdate(alg, text, result, true)
}
}
//十六进制计算器
async function calculator(text) {
let value = await vscode.window.showInputBox({
placeHolder: '输入要计算的公式,非十进制数以0x、0b、0o开头',
value: text,
})
if (value) {
try {
let result = eval(value)
let dec = parseFloat(result)
let hex = '0x' + dec.toString(16)
let oct = '0o' + dec.toString(8)
let bin = '0b' + dec.toString(2)
let str =
/^[\x20-\x7e]+$/.test(basic.number2string(dec)) &&
basic.number2string(dec)
log = `dec: {0}
hex: {1}
bin: {2}
oct: {3}`.format(dec, hex, bin, oct)
if (str) {
log += '\n str: "{0}"'.format(str)
}
outputChannelUpdate('hexadecimalConverter', value, log, false)
} catch (err) {
vscode.window.showErrorMessage('输入非公式,将先转换成十六进制再计算')
let hex = '0x' + basic.string2hex(value)
calculator(hex)
}
}
}
//处理text/x-code-output兼容问题
//获取output channel的高亮语法
function getPatterns(file) {
let content = fs.readFileSync(file, 'utf-8')
let patterns = /<key>patterns<\/key>[\s|\S]*?<array>([\s|\S]*?)<\/array>/.exec(
content,
)[1]
return patterns
}
//去除相关插件JSON文件里的"text/x-code-output",以免冲突
function rmOutput(file) {
let content = fs.readFileSync(file, 'utf-8')
let New = content.replace('text/x-code-output', 'text/bak-x-code-output')
fs.writeFileSync(file, New)
}
//获取定义了output channel语法的文件,并整合语法
function getLang() {
let lang = ''
const extension = vscode.extensions.all
for (let e of extension) {
try {
let mimetypes = e.packageJSON.contributes.languages[0].mimetypes
if (mimetypes.includes('text/x-code-output')) {
let id = e.id
if (id != 'fofolee.crypto-tools') {
let extensionPath = e.extensionPath
let grammarsPath = e.packageJSON.contributes.grammars[0].path.substr(
1,
)
rmOutput(manip.convPath(extensionPath + '/package.json'))
outputChannelShow('发现定义了output语法的冲突插件:\n' + extensionPath)
file = manip.convPath(extensionPath + grammarsPath)
console.log(file)
lang +=
' <!-- ' +
id +
' start -->' +
getPatterns(file) +
'<!-- ' +
id +
' end -->\n'
}
}
} catch (err) {}
}
return lang
}
outputChannel = vscode.window.createOutputChannel('crypto')
exports.activate = (context) => {
// 注册命令
context.subscriptions.push(
vscode.commands.registerCommand('crypto.EncodeDecode', async () => {
editor = vscode.window.activeTextEditor
selection = editor.selection
let text = editor.document.getText(selection).trim()
let algorithm = await vscode.window.showQuickPick(
[
{
label: 'Base64/32/16 Decode',
detail: '自动进行(url-safe)base64/32/16解密',
target: basic.baseDecode,
},
{
label: 'Base64 Encode',
detail: 'base64加密',
target: basic.base64Encode,
},
{
label: 'Url-Safe Base64 Encode',
detail: 'url-safe base64加密',
target: basic.urlSafeBase64Encode,
},
{
label: 'Base32 Encode',
detail: 'base32加密',
target: basic.base32Encode,
},
{
label: 'Base16 Encode',
detail: 'base16加密',
target: basic.base16Encode,
},
{
label: 'MD5',
detail: 'MD5哈希算法',
target: basic.md5Hash,
},
{
label: 'SHA512',
detail: 'SHA512哈希算法',
target: basic.sha512Hash,
},
{
label: 'Url Decode',
detail: 'url加密',
target: basic.urlDecode,
},
{
label: 'Url Encode',
detail: 'url解密',
target: basic.urlEncode,
},
{
label: 'Html Entities',
detail: 'html加密',
target: basic.htmlEncode,
},
{
label: 'Html Entity Decode',
detail: 'html解密',
target: basic.htmlDecode,
},
{
label: 'Morse Encode',
detail: '摩斯密码加密',
target: morseCoding,
},
{
label: 'Morse Decode',
detail: '摩斯密码解密',
target: morseCoding,
},
{
label: 'ROT13',
detail: 'ROT13加密',
target: basic.rot13,
},
{
label: 'Quote-Printable Decode',
detail: 'Quote-Printable解密',
target: basic.quopriDecode,
},
{
label: 'Quote-Printable Encode',
detail: 'Quote-Printable加密',
target: basic.quopriEncode,
},
{
label: 'Bubble Babble Decode',
detail: 'Bubble Babble 解密',
target: basic.bubbleDecode,
},
{
label: 'Bubble Babble Encode',
detail: 'Bubble Babble 加密',
target: basic.bubbleEncode,
},
{
label: 'Brainfuck Decode',
detail: 'Brainfuck 解密',
target: basic.brainfuckDecode,
},
{
label: 'Number To String',
detail: '整型转字符',
target: basic.number2string,
},
{
label: 'String To Number',
detail: '字符转整型',
target: basic.string2number,
},
{
label: 'String To Hex',
detail: '字符转十六进制',
target: basic.string2hex,
},
{
label: 'Hex To String',
detail: '十六进制转字符',
target: basic.hex2string,
},
{
label: 'String To Bin',
detail: '字符转二进制',
target: basic.string2bin,
},
{
label: 'Bin To String',
detail: '二进制转字符',
target: basic.bin2string,
},
],
{
placeHolder: '选择一种算法',
},
)
if (algorithm) {
let result = algorithm.target(text)
outputChannelUpdate(algorithm.label, text, result, true)
}
}),
)
context.subscriptions.push(
vscode.commands.registerCommand('crypto.EncryptDecrypt', async () => {
editor = vscode.window.activeTextEditor
selection = editor.selection
let text = editor.document.getText(selection).trim()
let algorithm = await vscode.window.showQuickPick(
[
{
label: 'Symmetric Decryption',
detail: '对称密码解密算法,密文格式为Base64',
target: symmetricCryption,
},
{
label: 'Symmetric Encryption',
detail: '对称密码加密算法,密文格式为Base64',
target: symmetricCryption,
},
{
label: 'RSA Decryption',
detail: '使用公钥或私钥文件进行RSA解密',
target: rsaCryption,
},
{
label: 'RSA Encryption',
detail: '使用公钥或私钥文件进行RSA加密',
target: rsaCryption,
},
{
label: 'Caesar Cipher',
detail: '凯撒密码',
target: caesarCipher,
},
{
label: 'Character Offset',
detail: '字符位移',
target: characterOffset,
},
{
label: 'Fence Decode',
detail: '栅栏密码解密',
target: fenceCipher,
},
{
label: 'Fence Encode',
detail: '栅栏密码加密',
target: fenceCipher,
},
{
label: 'Vigenere Encode',
detail: '维基尼亚加密',
target: vigenereCipher,
},
{
label: 'Vigenere Decode',
detail: '维基尼亚解密',
target: vigenereCipher,
},
{
label: 'Vigenere Decode with No Key',
detail: '维基尼亚无秘钥解密',
target: vigenereAutoDecode,
},
],
{
placeHolder: '选择一种算法',
},
)
if (algorithm) {
algorithm.target(text, algorithm.label)
}
}),
)
context.subscriptions.push(
vscode.commands.registerCommand('crypto.textManipulation', async () => {
editor = vscode.window.activeTextEditor
selection = editor.selection
let text = editor.document.getText(selection).trim()
let algorithm = await vscode.window.showQuickPick(
[
{
label: 'Reverse String',
detail: '字符串逆转',
target: manip.reverseString,
},
{
label: 'Upper Case',
detail: '全部大写',
target: manip.upperString,
},
{
label: 'Lower Case',
detail: '全部小写',
target: manip.lowerString,
},
{
label: 'Strip String',
detail: '去除左右空格、换行',
target: manip.stripString,
},
{
label: 'Space To None',
detail: '去除所有空格',
target: manip.space2None,
},
{
label: 'Space To Line',
detail: '空格转换行',
target: manip.space2Line,
},
{
label: 'Convert Path',
detail: '\\和/互转',
target: manip.convPath,
},
{
label: 'Title Case',
detail: '所有词首字母大写',
target: manip.titleCase,
},
{
label: 'String Lenght',
detail: '获取文本长度',
target: manip.stringLen,
},
{
label: 'Add Quot By Comma',
detail: '为每个以逗号分隔的词加上双引号',
target: manip.addQuotByComma,
},
{
label: 'Add Quot By Space',
detail: '为每个以空格分隔的词加上双引号',
target: manip.addQuotBySpace,
},
],
{
placeHolder: '选择一种处理方案',
},
)
if (algorithm) {
let result = algorithm.target(text)
if (algorithm.label != 'String Lenght') {
outputChannelUpdate(algorithm.label, text, result, true)
} else {
outputChannelUpdate(algorithm.label, text, result, false)
vscode.window.showInformationMessage('长度为:' + result)
}
}
}),
)
context.subscriptions.push(
vscode.commands.registerCommand('crypto.hexadecimalCalculator', () => {
calculator('')
}),
)
context.subscriptions.push(
vscode.commands.registerCommand('crypto.outputColorPatch', async () => {
let choise = await vscode.window.showInformationMessage(
'如果安装本插件后,本插件的输出没有高亮,或者造成其他插件的输出没有高亮,则是因为和其他一些插件的输出语法产生了冲突,是否尝试自动修复这些冲突?',
'是',
'否',
)
if (choise == '是') {
outputChannelShow('正在尝试寻找问题···')
let langFile = __dirname + '/syntaxes/crypto-tools-output.tmLanguage'
let file = fs.readFileSync(langFile, 'utf-8')
let lang = getLang()
let New = file.replace(' </array>', lang + ' </array>')
console.log(New)
fs.writeFileSync(langFile, New)
if (lang) {
outputChannelShow('语法文件已整合,请重启编辑器!')
} else {
outputChannelShow('未发现冲突的文件!')
}
}
}),
)
}