forked from streetwriters/notesnook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
146 lines (123 loc) · 3.7 KB
/
index.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
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
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2022 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ready } from "libsodium-wrappers";
import Decryption from "./src/decryption";
import Encryption from "./src/encryption";
import { INNCrypto, IStreamable } from "./src/interfaces";
import KeyUtils from "./src/keyutils";
import Password from "./src/password";
import {
Cipher,
EncryptionKey,
OutputFormat,
Plaintext,
SerializedKey,
Chunk
} from "./src/types";
export class NNCrypto implements INNCrypto {
private isReady = false;
private async init() {
if (this.isReady) return;
await ready;
this.isReady = true;
}
async encrypt(
key: SerializedKey,
plaintext: Plaintext,
outputFormat: OutputFormat = "uint8array"
): Promise<Cipher> {
await this.init();
return Encryption.encrypt(key, plaintext, outputFormat);
}
async decrypt(
key: SerializedKey,
cipherData: Cipher,
outputFormat: OutputFormat = "text"
): Promise<Plaintext> {
await this.init();
return Decryption.decrypt(key, cipherData, outputFormat);
}
async hash(password: string, salt: string): Promise<string> {
await this.init();
return Password.hash(password, salt);
}
async deriveKey(password: string, salt?: string): Promise<EncryptionKey> {
await this.init();
return KeyUtils.deriveKey(password, salt);
}
async exportKey(password: string, salt?: string): Promise<SerializedKey> {
await this.init();
return KeyUtils.exportKey(password, salt);
}
async createEncryptionStream(
key: SerializedKey,
stream: IStreamable
): Promise<string> {
await this.init();
const encryptionStream = Encryption.createStream(key);
// eslint-disable-next-line no-constant-condition
while (true) {
const chunk = await stream.read();
if (!chunk) break;
const { data, final } = chunk;
if (!data) break;
const encryptedChunk: Chunk = {
data: encryptionStream.write(data, final),
final
};
await stream.write(encryptedChunk);
if (final) break;
}
return encryptionStream.header;
}
async createDecryptionStream(
iv: string,
key: SerializedKey,
stream: IStreamable
) {
await this.init();
const decryptionStream = Decryption.createStream(iv, key);
// eslint-disable-next-line no-constant-condition
while (true) {
const chunk = await stream.read();
if (!chunk) break;
const { data, final } = chunk;
if (!data) break;
const decryptedChunk: Chunk = {
data: decryptionStream.read(data),
final
};
await stream.write(decryptedChunk);
if (final) break;
}
}
async encryptStream(
key: SerializedKey,
stream: IStreamable,
_streamId?: string
): Promise<string> {
await this.init();
return await this.createEncryptionStream(key, stream);
}
async decryptStream(
key: SerializedKey,
iv: string,
stream: IStreamable,
_streamId?: string
): Promise<void> {
await this.init();
await this.createDecryptionStream(iv, key, stream);
}
}