Forráskód Böngészése

Generate and set user's private and public key pair

Vishnu Mohandas 4 éve
szülő
commit
a9e0821db0

+ 6 - 1
src/pages/generate/index.tsx

@@ -57,11 +57,16 @@ export default function Generate() {
                     await cryptoWorker.fromString(passphrase), kekSalt);
                 const kekHash = await cryptoWorker.hash(kek);
                 const encryptedKeyAttributes = await cryptoWorker.encrypt(key, kek);
+                const keyPair = await cryptoWorker.generateKeyPair();
+                const encryptedKeyPairAttributes = await cryptoWorker.encrypt(keyPair.privateKey, kek);
                 const keyAttributes = {
                     kekSalt: await cryptoWorker.toB64(kekSalt),
-                    kekHash,
+                    kekHash: kekHash,
                     encryptedKey: await cryptoWorker.toB64(encryptedKeyAttributes.encryptedData),
                     keyDecryptionNonce: await cryptoWorker.toB64(encryptedKeyAttributes.nonce),
+                    publicKey: await cryptoWorker.toB64(keyPair.publicKey),
+                    encryptedSecretKey: await cryptoWorker.toB64(encryptedKeyPairAttributes.encryptedData),
+                    secretKeyDecryptionNonce: await cryptoWorker.toB64(encryptedKeyPairAttributes.nonce)
                 };
                 await putKeyAttributes(token, keyAttributes);
                 setData(LS_KEYS.KEY_ATTRIBUTES, keyAttributes);

+ 1 - 1
src/services/userService.ts

@@ -13,7 +13,7 @@ export const verifyOtt = (email: string, ott: string) => {
 }
 
 export const putKeyAttributes = (token: string, keyAttributes: keyAttributes) => {
-    return HTTPService.put(`${ENDPOINT}/users/key-attributes`, keyAttributes, null, {
+    return HTTPService.put(`${ENDPOINT}/users/attributes`, { 'keyAttributes': keyAttributes, 'name': 'Dummy Name' }, null, {
         'X-Auth-Token': token,
     });
 }

+ 5 - 0
src/utils/crypto/libsodium.ts

@@ -87,6 +87,11 @@ export async function generateSaltToDeriveKey() {
     return sodium.randombytes_buf(sodium.crypto_pwhash_SALTBYTES);
 }
 
+export async function generateKeyPair() {
+    await sodium.ready;
+    return sodium.crypto_box_keypair();
+}
+
 export async function fromB64(input: string) {
     await sodium.ready;
     return sodium.from_base64(input, sodium.base64_variants.ORIGINAL);

+ 4 - 0
src/worker/crypto.worker.js

@@ -53,6 +53,10 @@ export class Crypto {
         return libsodium.deriveKey(passphrase, salt);
     }
 
+    async generateKeyPair() {
+        return libsodium.generateKeyPair();
+    }
+
     async fromString(string) {
         return libsodium.fromString(string);
     }