bf4d447977fb79fe1671ed482df4569e9781debe
[platform/framework/web/wrtjs.git] / device_home / service / security.js
1 const crypto = require("crypto");
2 const { generateKeyPair } = require("crypto");
3
4 const TAG = '[DeviceHome][security.js]';
5
6 module.exports.Security = class Security {
7   constructor() {
8     this.privateKey = '';
9     this.publicKey = '';
10   }
11
12   getKeyPairPromise(pincode) {
13     console.log(`${TAG} getKeyPairPromise`);
14     return new Promise((resolve, reject) => {
15       console.log(`${TAG} Generating key will take some time..`);
16       generateKeyPair("rsa", {
17         modulusLength: 1024,
18         publicKeyEncoding: {
19           type: "spki",
20           format: "pem"
21         },
22         privateKeyEncoding: {
23           type: "pkcs1",
24           format: "pem",
25           // TODO: Enable ciper and passphrase
26           //cipher: "aes-256-cbc",
27           //passphrase: pincode
28         }
29       }, (err, publicKey, privateKey) => {
30         // Handle errors and use the generated key pair.
31         resolve({
32           publicKey: publicKey,
33           privateKey: privateKey
34         });
35       });
36     });
37   }
38
39   awaitKeyPair(pincode) {
40     return this.getKeyPairPromise(pincode).then(r => {
41       this.privateKey = r.privateKey;
42       this.publicKey = r.publicKey;
43       console.log(`${TAG}[awaitKeyPair] RSA keys are generated`);
44     }).catch(err => {
45       console.log(`${TAG} Error : ${err}`);
46     });
47   }
48
49   // TODO: Use following APIs instead of JSEncryptLib
50   decryptWithPrivateKey(encryptedString, pincode) {
51     const key = crypto.createPrivateKey({
52       key: this.privateKey,
53       format: "pem",
54       passphrase: pincode
55     });
56     const encrypted = crypto.privateDecrypt(key, Buffer.from(encryptedString, "base64"));
57     const decryptedString = encrypted.toString("utf8");
58     return decryptedString;
59   }
60
61   encryptWithPrivateKey(plain, pincode) {
62     const keyp = crypto.createPrivateKey({
63       key: this.privateKey,
64       format: "pem",
65       passphrase: pincode
66     });
67     const encrypted = crypto.privateEncrypt(keyp, Buffer.from(plain));
68     const encryptedString = encrypted.toString("base64");
69     return encryptedString;
70   }
71
72   decryptWithPublicKey(encryptedString) {
73     const decrypted = crypto.publicDecrypt(this.publicKey, Buffer.from(encryptedString, "base64"));
74     const decryptedString = decrypted.toString("utf8");
75     return decryptedString;
76   }
77
78   encryptWithPublicKey(plain) {
79     const encrypted = crypto.publicEncrypt(this.publicKey, Buffer.from(plain));
80     const encryptedString = encrypted.toString("base64");
81     return encryptedString;
82   }
83
84   getPrivateKey() {
85     console.log(`${TAG} getPrivateKey`);
86     return this.privateKey;
87   }
88
89   getPublicKey() {
90     console.log(`${TAG} getPublicKey`);
91     return this.publicKey;
92   }
93 }