Browse Source

Genrate keys

Carsten Porth 5 years ago
parent
commit
94852b6a8b
2 changed files with 41 additions and 2 deletions
  1. 3 2
      app/src/pages/settings/settings.html
  2. 38 0
      app/src/pages/settings/settings.ts

+ 3 - 2
app/src/pages/settings/settings.html

@@ -20,9 +20,10 @@
   <ion-card>
     <ion-card-header>Encryption</ion-card-header>
     <ion-card-content>
-      <p>To protect your privacy, all data send to the P2P network will be encrypted. Therefore you need to enter or generate
+      <p>To protect your privacy, all data send to the P2P network will be encrypted. Therefore you need to enter or
+        generate
         a pair of keys. If you run the app on multiple devices, please enter everywhere the same pair of keys.</p>
-      <button ion-button block>Generate keys</button>
+      <button ion-button block (click)="generateKeys()">Generate keys</button>
       <ion-label color="primary" stacked>Public Key:</ion-label>
       <ion-textarea [(ngModel)]="privateKey"></ion-textarea>
       <ion-label color="primary" stacked>Private Key:</ion-label>

+ 38 - 0
app/src/pages/settings/settings.ts

@@ -25,6 +25,13 @@ export class SettingsPage {
     this.keywords = await this.storage.get("keywords");
   }
 
+  async generateKeys() {
+    const keys = await this.generateRsaKeys();
+
+    this.publicKey = await this.extractPublicKey(keys);
+    this.privateKey = await this.extractPrivateKey(keys);
+  }
+
   save() {
     this.storage.set("publicKey", this.publicKey);
     this.storage.set("privateKey", this.privateKey);
@@ -37,4 +44,35 @@ export class SettingsPage {
     });
     toast.present();
   }
+
+  private async generateRsaKeys() {
+    return await crypto.subtle.generateKey(
+      {
+        name: "RSA-OAEP",
+        modulusLength: 1024,
+        publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
+        hash: { name: "SHA-256" }
+      },
+      true,
+      ["encrypt", "decrypt"]
+    );
+  }
+
+  private async extractPrivateKey(keys): Promise<string> {
+    return btoa(
+      String.fromCharCode.apply(
+        null,
+        new Uint8Array(await crypto.subtle.exportKey("pkcs8", keys.privateKey))
+      )
+    );
+  }
+
+  private async extractPublicKey(keys): Promise<string> {
+    return btoa(
+      String.fromCharCode.apply(
+        null,
+        new Uint8Array(await crypto.subtle.exportKey("spki", keys.publicKey))
+      )
+    );
+  }
 }