From 8dff1a64b2ff4996be9d1c40b2be32943c914c54 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 17 Aug 2021 14:05:03 -0400 Subject: [PATCH] extended crypto api --- static/client.js | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/static/client.js b/static/client.js index 7157a1f..21cae8f 100644 --- a/static/client.js +++ b/static/client.js @@ -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); + }, } /* -- 2.52.0