]> git.mdlowis.com Git - proto/achat.git/commitdiff
extended crypto api
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 17 Aug 2021 18:05:03 +0000 (14:05 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 17 Aug 2021 18:05:03 +0000 (14:05 -0400)
static/client.js

index 7157a1fe5a80a6aafe1f3178a6dd4c7353d12e50..21cae8fd8586af80c1fa6eaf8c010f22e982a849 100644 (file)
@@ -36,12 +36,25 @@ const Data = {
     clr: ()     => localStorage.clear(),
 };
 
+
+
 /*
     Cryptographic API
 */
 const Crypto = {
     api: window.crypto.subtle,
-    gen: ()=> Crypto.api.generateKey(
+
+    stoa: (str)=>{
+      const buf = new Uint8Array(new ArrayBuffer(str.length));
+      for (let i = 0; i < str.length; i++) {
+        bufView[i] = str.charCodeAt(i);
+      }
+      return buf;
+    },
+
+    atos: (array)=> String.fromCharCode.apply(null, new Uint8Array(array)),
+
+    generate: ()=> Crypto.api.generateKey(
         {
             name: "RSA-OAEP",
             modulusLength: 4096,
@@ -49,8 +62,31 @@ const Crypto = {
             hash: "SHA-256",
         },
         true,
-        ["encrypt", "decrypt"]
+        ["encrypt", "decrypt", "sign", "verify"]
     ),
+
+    import: (key)=>{
+        const binkey = window.atob(key);
+        const keybuf = Crypto.stoa(binkey);
+        return Crypto.api.importKey(
+            "pkcs8",
+            keybuf,
+            {
+              name: "RSA-OAEP",
+              modulusLength: 4096,
+              publicExponent: new Uint8Array([1, 0, 1]),
+              hash: "SHA-256",
+            },
+            true,
+            ["encrypt", "decrypt", "sign", "verify"]
+        );
+    },
+
+    export: (key)=>{
+        const expkey = await Crypto.api.exportKey("pkcs8", key);
+        const strkey = Crypto.atos(expkey);
+        return window.btoa(strkey);
+    },
 }
 
 /*