-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrypto.js
32 lines (28 loc) · 919 Bytes
/
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
const forge = require('node-forge');
const rsa = forge.pki.rsa;
const crypto = {
forge,
generateKeys: () => {
return new Promise((resolve, reject) => {
// generate an RSA key pair asynchronously (uses web workers if available)
// use workers: -1 to run a fast core estimator to optimize # of workers
rsa.generateKeyPair({bits: 1024, workers: 2}, (err, keypair) => {
if (err) {
return reject(err);
}
let newKeys = {
publicKey: forge.pki.publicKeyToPem(keypair.publicKey),
privateKey: forge.pki.privateKeyToPem(keypair.privateKey)
};
resolve(newKeys);
});
});
},
encrypt: (msg, pubKey) => {
return rsa.encrypt(msg, forge.pki.publicKeyFromPem(pubKey), true);
},
decrypt: (msg, priKey) => {
return rsa.decrypt(msg, forge.pki.privateKeyFromPem(priKey), false, false);
}
}
module.exports = crypto;