add link at bottom of page to import/export keys, add copy to clipboard buttons, adjust the How It Works section, add FAQ section

This commit is contained in:
2024-02-01 16:37:18 -05:00
parent 141788714b
commit d2d91e6ab3
7 changed files with 263 additions and 29 deletions

View File

@@ -80,6 +80,11 @@ async function saveKeys(ecdhPublic: CryptoKey, ecdhPrivate: CryptoKey) {
// Store ECDH key pair in local storage
localStorage.setItem("ecdhPublic", JSON.stringify(ecdhPublicJwk));
localStorage.setItem("ecdhPrivate", JSON.stringify(ecdhPrivateJwk));
return {
public: JSON.stringify(ecdhPublicJwk),
private: JSON.stringify(ecdhPrivateJwk),
};
}
export async function encrypt(
@@ -141,3 +146,57 @@ function base64ToUint8Array(base64: string): Uint8Array {
}
return bytes;
}
function downloadFile(content: string, fileName: string, contentType: string) {
const a = document.createElement("a");
const file = new Blob([content], { type: contentType });
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
URL.revokeObjectURL(a.href);
}
export async function exportKeys(): Promise<void> {
const keys = await retrieveOrGenerateKeyPair();
const exportedKeys = await saveKeys(keys.publicKey, keys.privateKey);
const exportedKeysJson = JSON.stringify(exportedKeys);
downloadFile(exportedKeysJson, "keys.json", "application/json");
}
export async function importAndSaveKeys(keys: {
public: string;
private: string;
}): Promise<void> {
const ecdhPublicJwk = JSON.parse(keys.public);
const ecdhPrivateJwk = JSON.parse(keys.private);
// Import the keys
const ecdhPublic = await window.crypto.subtle.importKey(
"jwk",
ecdhPublicJwk,
{
name: "ECDH",
namedCurve: "P-256",
},
true,
[]
);
const ecdhPrivate = await window.crypto.subtle.importKey(
"jwk",
ecdhPrivateJwk,
{
name: "ECDH",
namedCurve: "P-256",
},
true,
["deriveKey", "deriveBits"]
);
// Save the imported keys
saveKeys(ecdhPublic, ecdhPrivate);
}