-
Notifications
You must be signed in to change notification settings - Fork 31k
/
Copy pathencryptionMainService.ts
55 lines (48 loc) · 1.57 KB
/
encryptionMainService.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ICommonEncryptionService } from 'vs/platform/encryption/common/encryptionService';
import { ILogService } from 'vs/platform/log/common/log';
export interface Encryption {
encrypt(salt: string, value: string): Promise<string>;
decrypt(salt: string, value: string): Promise<string>;
}
export class EncryptionMainService implements ICommonEncryptionService {
declare readonly _serviceBrand: undefined;
constructor(
private machineId: string,
@ILogService private readonly logService: ILogService
) { }
private encryption(): Promise<Encryption> {
return new Promise((resolve, reject) => require(['vscode-encrypt'], resolve, reject));
}
async encrypt(value: string): Promise<string> {
let encryption: Encryption;
try {
encryption = await this.encryption();
} catch (e) {
return value;
}
try {
return encryption.encrypt(this.machineId, value);
} catch (e) {
this.logService.error(e);
return value;
}
}
async decrypt(value: string): Promise<string> {
let encryption: Encryption;
try {
encryption = await this.encryption();
} catch (e) {
return value;
}
try {
return encryption.decrypt(this.machineId, value);
} catch (e) {
this.logService.error(e);
return value;
}
}
}