orgnote-api 0.12.1 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -6,6 +6,8 @@ import {
|
|
|
6
6
|
NoKeysProvidedError,
|
|
7
7
|
NoPasswordProvidedError,
|
|
8
8
|
IncorrectOrMissingPrivateKeyPasswordError,
|
|
9
|
+
encrypt,
|
|
10
|
+
decrypt,
|
|
9
11
|
} from '../encryption';
|
|
10
12
|
import { test, expect } from 'vitest';
|
|
11
13
|
|
|
@@ -163,3 +165,43 @@ aGW80jwBXEQ7uTjT8akpOKiH7BIuhEUZIXh+vDveG0Uwf63s2dIklznAEo+E
|
|
|
163
165
|
expect(e).toBeInstanceOf(NoPasswordProvidedError);
|
|
164
166
|
}
|
|
165
167
|
});
|
|
168
|
+
|
|
169
|
+
test('Should encrypt and decrypt text by provided configs via password', async () => {
|
|
170
|
+
const text = 'Hello world';
|
|
171
|
+
const password = '123';
|
|
172
|
+
|
|
173
|
+
const res = await encrypt(text, {
|
|
174
|
+
type: 'gpgPassword',
|
|
175
|
+
password,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
expect(res.startsWith('-----BEGIN PGP MESSAGE-----')).toBeTruthy();
|
|
179
|
+
|
|
180
|
+
const decryptedMessage = await decrypt(res, {
|
|
181
|
+
type: 'gpgPassword',
|
|
182
|
+
password,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
expect(decryptedMessage).toEqual(text);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test('Should encrypt and decrypt text by provided configs via keys', async () => {
|
|
189
|
+
const text = 'Hello world';
|
|
190
|
+
const res = await encrypt(text, {
|
|
191
|
+
type: 'gpgKeys',
|
|
192
|
+
publicKey: armoredPublicKey,
|
|
193
|
+
privateKey: armoredPrivateKey,
|
|
194
|
+
privateKeyPassphrase,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
expect(res.startsWith('-----BEGIN PGP MESSAGE-----')).toBeTruthy();
|
|
198
|
+
|
|
199
|
+
const decryptedMessage = await decrypt(res, {
|
|
200
|
+
type: 'gpgKeys',
|
|
201
|
+
publicKey: armoredPublicKey,
|
|
202
|
+
privateKey: armoredPrivateKey,
|
|
203
|
+
privateKeyPassphrase,
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
expect(decryptedMessage).toEqual(text);
|
|
207
|
+
});
|
package/encryption/encryption.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createMessage,
|
|
3
|
-
decrypt,
|
|
3
|
+
decrypt as _decrypt,
|
|
4
4
|
decryptKey,
|
|
5
|
-
encrypt,
|
|
5
|
+
encrypt as _encrypt,
|
|
6
6
|
readKey,
|
|
7
7
|
readMessage,
|
|
8
8
|
readPrivateKey,
|
|
9
9
|
} from 'openpgp';
|
|
10
|
+
import { ModelsPublicNoteEncryptionTypeEnum } from 'src/remote-api';
|
|
11
|
+
import { OrgNoteEncryption } from '..';
|
|
10
12
|
|
|
11
13
|
export class IncorrectOrMissingPrivateKeyPasswordError extends Error {}
|
|
12
14
|
export class ImpossibleToDecryptWithProvidedKeysError extends Error {}
|
|
@@ -41,6 +43,47 @@ export const encryptViaPassword = withCustomErrors(_encryptViaPassword);
|
|
|
41
43
|
export const decryptViaPassword = withCustomErrors(_decryptViaPassword);
|
|
42
44
|
export const decryptViaKeys = withCustomErrors(_decryptViaKeys);
|
|
43
45
|
|
|
46
|
+
export const encrypt = async (
|
|
47
|
+
text: string,
|
|
48
|
+
encryptionParams: OrgNoteEncryption
|
|
49
|
+
): Promise<string> => {
|
|
50
|
+
if (
|
|
51
|
+
!encryptionParams.type ||
|
|
52
|
+
encryptionParams.type === ModelsPublicNoteEncryptionTypeEnum.Disabled
|
|
53
|
+
) {
|
|
54
|
+
return text;
|
|
55
|
+
}
|
|
56
|
+
return encryptionParams.type === ModelsPublicNoteEncryptionTypeEnum.GpgKeys
|
|
57
|
+
? await encryptViaKeys(
|
|
58
|
+
text,
|
|
59
|
+
encryptionParams.publicKey,
|
|
60
|
+
encryptionParams.privateKey,
|
|
61
|
+
encryptionParams.privateKeyPassphrase
|
|
62
|
+
)
|
|
63
|
+
: await encryptViaPassword(text, encryptionParams.password);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const decrypt = async (
|
|
67
|
+
text: string,
|
|
68
|
+
encryptionParams: OrgNoteEncryption
|
|
69
|
+
): Promise<string> => {
|
|
70
|
+
if (
|
|
71
|
+
!encryptionParams.type ||
|
|
72
|
+
encryptionParams.type === ModelsPublicNoteEncryptionTypeEnum.Disabled
|
|
73
|
+
) {
|
|
74
|
+
return text;
|
|
75
|
+
}
|
|
76
|
+
const decryptedNote =
|
|
77
|
+
encryptionParams.type === ModelsPublicNoteEncryptionTypeEnum.GpgKeys
|
|
78
|
+
? await decryptViaKeys(
|
|
79
|
+
text,
|
|
80
|
+
encryptionParams.privateKey,
|
|
81
|
+
encryptionParams.privateKeyPassphrase
|
|
82
|
+
)
|
|
83
|
+
: await decryptViaPassword(text, encryptionParams.password);
|
|
84
|
+
return decryptedNote;
|
|
85
|
+
};
|
|
86
|
+
|
|
44
87
|
async function _encryptViaPassword(
|
|
45
88
|
text: string,
|
|
46
89
|
password: string
|
|
@@ -49,7 +92,7 @@ async function _encryptViaPassword(
|
|
|
49
92
|
text,
|
|
50
93
|
});
|
|
51
94
|
|
|
52
|
-
const encryptedMessage = await
|
|
95
|
+
const encryptedMessage = await _encrypt({
|
|
53
96
|
message,
|
|
54
97
|
format: 'armored',
|
|
55
98
|
passwords: [password],
|
|
@@ -81,7 +124,7 @@ async function _encryptViaKeys(
|
|
|
81
124
|
})
|
|
82
125
|
: encryptedPrivateKey;
|
|
83
126
|
|
|
84
|
-
const encryptedMessage = await
|
|
127
|
+
const encryptedMessage = await _encrypt({
|
|
85
128
|
message,
|
|
86
129
|
format: 'armored',
|
|
87
130
|
encryptionKeys: publicKey,
|
|
@@ -97,7 +140,7 @@ async function _decryptViaPassword(
|
|
|
97
140
|
): Promise<string> {
|
|
98
141
|
const message = await readMessage({ armoredMessage: data });
|
|
99
142
|
|
|
100
|
-
const { data: decryptedText } = await
|
|
143
|
+
const { data: decryptedText } = await _decrypt({
|
|
101
144
|
message,
|
|
102
145
|
passwords: password,
|
|
103
146
|
});
|
|
@@ -123,7 +166,7 @@ async function _decryptViaKeys(
|
|
|
123
166
|
|
|
124
167
|
const message = await readMessage({ armoredMessage: data });
|
|
125
168
|
|
|
126
|
-
const { data: decryptedText } = await
|
|
169
|
+
const { data: decryptedText } = await _decrypt({
|
|
127
170
|
message,
|
|
128
171
|
decryptionKeys: privateKey,
|
|
129
172
|
});
|
|
@@ -24,6 +24,7 @@ export interface AbstractEncryptedNote {
|
|
|
24
24
|
published?: boolean;
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
|
+
// TODO: master change signature for encrypt notes without content
|
|
27
28
|
export async function encryptNote<T extends AbstractEncryptedNote>(
|
|
28
29
|
note: T,
|
|
29
30
|
encryptionParams: OrgNoteEncryption
|
package/models/note.ts
CHANGED