Browse Source

encrypt and decrypt using node-rsa

Carsten Porth 5 years ago
parent
commit
666d294e68
3 changed files with 40 additions and 4 deletions
  1. 24 4
      app/package-lock.json
  2. 1 0
      app/package.json
  3. 15 0
      app/src/providers/crypto/crypto.ts

+ 24 - 4
app/package-lock.json

@@ -382,6 +382,16 @@
         "elliptic": "^6.4.0",
         "node-rsa": "^0.4.0",
         "text-encoding": "^0.6.1"
+      },
+      "dependencies": {
+        "node-rsa": {
+          "version": "0.4.2",
+          "resolved": "http://registry.npmjs.org/node-rsa/-/node-rsa-0.4.2.tgz",
+          "integrity": "sha1-1jkXKewWqDDtWjgEKzFX0tXXJTA=",
+          "requires": {
+            "asn1": "0.2.3"
+          }
+        }
       }
     },
     "@types/localforage": {
@@ -4567,11 +4577,21 @@
       }
     },
     "node-rsa": {
-      "version": "0.4.2",
-      "resolved": "http://registry.npmjs.org/node-rsa/-/node-rsa-0.4.2.tgz",
-      "integrity": "sha1-1jkXKewWqDDtWjgEKzFX0tXXJTA=",
+      "version": "1.0.2",
+      "resolved": "https://registry.npmjs.org/node-rsa/-/node-rsa-1.0.2.tgz",
+      "integrity": "sha512-2Uf7rermLy0tfm74QGXUJwWd7/YiRKIgGIiezC2Ya7DL2S/ej6zkUCpMg7wAOoS0PXGk/vl77fkHitpiW4nErA==",
       "requires": {
-        "asn1": "0.2.3"
+        "asn1": "^0.2.4"
+      },
+      "dependencies": {
+        "asn1": {
+          "version": "0.2.4",
+          "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.4.tgz",
+          "integrity": "sha512-jxwzQpLQjSmWXgwaCZE9Nz+glAG01yF1QnWgbhGwHI5A6FRIEY6IVqtHhIepHqI7/kyEyQEagBC5mBEFlIYvdg==",
+          "requires": {
+            "safer-buffer": "~2.1.0"
+          }
+        }
       }
     },
     "node-sass": {

+ 1 - 0
app/package.json

@@ -49,6 +49,7 @@
     "ionic-angular": "3.9.2",
     "ionicons": "3.0.0",
     "javascript-time-ago": "^1.0.34",
+    "node-rsa": "^1.0.2",
     "rxjs": "5.5.11",
     "sw-toolbox": "3.6.0",
     "twit": "^2.2.11",

+ 15 - 0
app/src/providers/crypto/crypto.ts

@@ -2,6 +2,7 @@ import { Injectable } from "@angular/core";
 import { TwitterApiProvider } from "../twitter-api/twitter-api";
 import { P2pStorageIpfsProvider } from "../p2p-storage-ipfs/p2p-storage-ipfs";
 import { Storage } from "@ionic/storage";
+import NodeRSA from "node-rsa";
 
 @Injectable()
 export class CryptoProvider {
@@ -124,4 +125,18 @@ export class CryptoProvider {
     const privateKey = await this.storage.get("privateKey");
     return privateKey.length > 0;
   }
+
+  public encrypt(plainText: string, privateKey: string) {
+    const key = new NodeRSA(
+      `-----BEGIN PRIVATE KEY-----${privateKey}-----END PRIVATE KEY-----`
+    );
+    return key.encryptPrivate(plainText, "base64");
+  }
+
+  public decrypt(encryptedMessage: string, publicKey: string): string {
+    const key = new NodeRSA(
+      `-----BEGIN PUBLIC KEY-----${publicKey}-----END PUBLIC KEY-----`
+    );
+    return key.decryptPublic(encryptedMessage).toString();
+  }
 }