Ver Fonte

move key generation to crypto provider

Carsten Porth há 5 anos atrás
pai
commit
28722d8335
2 ficheiros alterados com 34 adições e 34 exclusões
  1. 3 34
      app/src/pages/settings/settings.ts
  2. 31 0
      app/src/providers/crypto/crypto.ts

+ 3 - 34
app/src/pages/settings/settings.ts

@@ -28,10 +28,10 @@ export class SettingsPage {
   }
 
   async generateKeys() {
-    const keys = await this.generateRsaKeys();
+    const keys = await this.cryptoUtils.generateRsaKeys();
 
-    this.publicKey = await this.extractPublicKey(keys);
-    this.privateKey = await this.extractPrivateKey(keys);
+    this.publicKey = await this.cryptoUtils.extractPublicKey(keys);
+    this.privateKey = await this.cryptoUtils.extractPrivateKey(keys);
   }
 
   save() {
@@ -50,35 +50,4 @@ export class SettingsPage {
   publishPublicKey() {
     this.cryptoUtils.publishPublicKey(this.publicKey);
   }
-
-  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))
-      )
-    );
-  }
 }

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

@@ -72,4 +72,35 @@ export class CryptoProvider {
     const hits = word.match(/ipfs:\/\/Qm[a-zA-Z0-9]+/);
     return hits != null;
   }
+
+  public 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"]
+    );
+  }
+
+  public async extractPrivateKey(keys): Promise<string> {
+    return btoa(
+      String.fromCharCode.apply(
+        null,
+        new Uint8Array(await crypto.subtle.exportKey("pkcs8", keys.privateKey))
+      )
+    );
+  }
+
+  public async extractPublicKey(keys): Promise<string> {
+    return btoa(
+      String.fromCharCode.apply(
+        null,
+        new Uint8Array(await crypto.subtle.exportKey("spki", keys.publicKey))
+      )
+    );
+  }
 }