-
Notifications
You must be signed in to change notification settings - Fork 8
/
crypto.js
45 lines (38 loc) 路 1.21 KB
/
crypto.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
import {
generateKey,
readKeys,
encrypt,
readMessage,
readPrivateKey,
decryptKey,
decrypt,
createMessage
} from 'openpgp';
export async function generateMyKeys(emailParams, passphraseParams, curveParams) {
const name = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
const email = emailParams;
const passphrase = passphraseParams;
const curve = curveParams;
return await generateKey({
curve: curveParams,
userIDs: [{ name: name, email: email }],
passphrase: passphrase
});
}
export async function encryptText(text, armoredKeys) {
const publicKeys = await readKeys({ armoredKeys });
const encrypted = await encrypt({
message: await createMessage({ text }),
encryptionKeys: publicKeys
});
return encrypted;
}
export async function decryptText(armoredMessage, armoredKey, passphrase) {
const privateKey = await readPrivateKey({ armoredKey });
const decryptedPrivateKey = await decryptKey({ privateKey, passphrase });
const { data: decrypted } = await decrypt({
message: await readMessage({ armoredMessage }),
decryptionKeys: decryptedPrivateKey
});
return decrypted;
}