fixed collection name decryption

This commit is contained in:
Abhinav-grd 2021-02-16 17:13:21 +05:30
parent a0b9a99abe
commit 4bbfb61f56
5 changed files with 25 additions and 24 deletions

View file

@ -12,7 +12,7 @@ import { getData, LS_KEYS, setData } from 'utils/storage/localStorage';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import { getKey, SESSION_KEYS, setKey } from 'utils/storage/sessionStorage'; import { getKey, SESSION_KEYS, setKey } from 'utils/storage/sessionStorage';
import * as Comlink from 'comlink'; import * as Comlink from 'comlink';
import { keyEncryptionResult } from 'services/uploadService'; import { B64EncryptionResult } from 'services/uploadService';
const CryptoWorker: any = const CryptoWorker: any =
typeof window !== 'undefined' && typeof window !== 'undefined' &&
@ -63,12 +63,12 @@ export default function Generate() {
kekSalt kekSalt
); );
const kekHash: string = await cryptoWorker.hash(kek); const kekHash: string = await cryptoWorker.hash(kek);
const encryptedKeyAttributes: keyEncryptionResult = await cryptoWorker.encryptToB64( const encryptedKeyAttributes: B64EncryptionResult = await cryptoWorker.encryptToB64(
key, key,
kek kek
); );
const keyPair = await cryptoWorker.generateKeyPair(); const keyPair = await cryptoWorker.generateKeyPair();
const encryptedKeyPairAttributes: keyEncryptionResult = await cryptoWorker.encryptToB64( const encryptedKeyPairAttributes: B64EncryptionResult = await cryptoWorker.encryptToB64(
keyPair.privateKey, keyPair.privateKey,
key key
); );

View file

@ -5,7 +5,7 @@ import localForage from 'localforage';
import HTTPService from './HTTPService'; import HTTPService from './HTTPService';
import * as Comlink from 'comlink'; import * as Comlink from 'comlink';
import { keyEncryptionResult } from './uploadService'; import { B64EncryptionResult } from './uploadService';
import { getActualKey, getToken } from 'utils/common/key'; import { getActualKey, getToken } from 'utils/common/key';
const CryptoWorker: any = const CryptoWorker: any =
@ -77,7 +77,7 @@ const getCollectionSecrets = async (
} }
collection.name = collection.name =
collection.name || collection.name ||
(await worker.decryptString( (await worker.decryptToUTF8(
collection.encryptedName, collection.encryptedName,
collection.nameDecryptionNonce, collection.nameDecryptionNonce,
decryptedKey decryptedKey
@ -208,14 +208,14 @@ export const AddCollection = async (
const { const {
encryptedData: encryptedKey, encryptedData: encryptedKey,
nonce: keyDecryptionNonce, nonce: keyDecryptionNonce,
}: keyEncryptionResult = await worker.encryptToB64( }: B64EncryptionResult = await worker.encryptToB64(
collectionKey, collectionKey,
encryptionKey encryptionKey
); );
const { const {
encryptedData: encryptedName, encryptedData: encryptedName,
nonce: nameDecryptionNonce, nonce: nameDecryptionNonce,
}: keyEncryptionResult = await worker.encryptToB64( }: B64EncryptionResult = await worker.encryptUTF8(
collectionName, collectionName,
collectionKey collectionKey
); );
@ -290,7 +290,7 @@ const addToCollection = async (collection: collection, files: file[]) => {
await Promise.all( await Promise.all(
files.map(async (file) => { files.map(async (file) => {
file.collectionID = collection.id; file.collectionID = collection.id;
const newEncryptedKey: keyEncryptionResult = await worker.encryptToB64( const newEncryptedKey: B64EncryptionResult = await worker.encryptToB64(
file.key, file.key,
collection.key collection.key
); );

View file

@ -19,7 +19,7 @@ interface encryptionResult {
file: fileAttribute; file: fileAttribute;
key: string; key: string;
} }
export interface keyEncryptionResult { export interface B64EncryptionResult {
encryptedData: string; encryptedData: string;
key: string; key: string;
nonce: string; nonce: string;
@ -39,7 +39,7 @@ interface FileinMemory {
interface encryptedFile { interface encryptedFile {
filedata: fileAttribute; filedata: fileAttribute;
thumbnail: fileAttribute; thumbnail: fileAttribute;
fileKey: keyEncryptionResult; fileKey: B64EncryptionResult;
} }
interface objectKey { interface objectKey {
@ -63,7 +63,7 @@ interface uploadFile extends objectKeys {
interface UploadFileWithoutMetaData { interface UploadFileWithoutMetaData {
tempUploadFile: uploadFile; tempUploadFile: uploadFile;
encryptedFileKey: keyEncryptionResult; encryptedFileKey: B64EncryptionResult;
fileName: string; fileName: string;
} }
@ -270,7 +270,7 @@ class UploadService {
fileKey fileKey
); );
const encryptedKey: keyEncryptionResult = await worker.encryptToB64( const encryptedKey: B64EncryptionResult = await worker.encryptB64(
fileKey, fileKey,
encryptionKey encryptionKey
); );
@ -289,7 +289,7 @@ class UploadService {
private async encryptMetadata( private async encryptMetadata(
worker: any, worker: any,
fileName: string, fileName: string,
encryptedFileKey: keyEncryptionResult encryptedFileKey: B64EncryptionResult
) { ) {
const metaData = this.metadataMap.get(fileName); const metaData = this.metadataMap.get(fileName);
const fileKey = await worker.decryptB64( const fileKey = await worker.decryptB64(
@ -340,7 +340,7 @@ class UploadService {
private getuploadFile( private getuploadFile(
collection: collection, collection: collection,
encryptedKey: keyEncryptionResult, encryptedKey: B64EncryptionResult,
objectKeys: objectKeys objectKeys: objectKeys
): uploadFile { ): uploadFile {
const uploadFile: uploadFile = { const uploadFile: uploadFile = {

View file

@ -142,6 +142,10 @@ export async function encryptToB64(data: string, key?: string) {
nonce: await toB64(encrypted.nonce), nonce: await toB64(encrypted.nonce),
}; };
} }
export async function encryptUTF8(data: string, key?: string) {
const b64Data = await toB64(await fromString(data));
return await encryptToB64(b64Data, key);
}
export async function decryptB64(data: string, nonce: string, key: string) { export async function decryptB64(data: string, nonce: string, key: string) {
await sodium.ready; await sodium.ready;
@ -154,7 +158,7 @@ export async function decryptB64(data: string, nonce: string, key: string) {
return await toB64(decrypted); return await toB64(decrypted);
} }
export async function decryptString(data: string, nonce: string, key: string) { export async function decryptToUTF8(data: string, nonce: string, key: string) {
await sodium.ready; await sodium.ready;
const decrypted = await decrypt( const decrypted = await decrypt(
await fromB64(data), await fromB64(data),
@ -250,14 +254,7 @@ export async function boxSealOpen(
export async function fromB64(input: string) { export async function fromB64(input: string) {
await sodium.ready; await sodium.ready;
let result; return sodium.from_base64(input, sodium.base64_variants.ORIGINAL);
try {
result = sodium.from_base64(input, sodium.base64_variants.ORIGINAL);
} catch (e) {
result = await fromB64(await toB64(await fromString(input)));
} finally {
return result;
}
} }
export async function toB64(input: Uint8Array) { export async function toB64(input: Uint8Array) {

View file

@ -69,7 +69,7 @@ export class Crypto {
return libsodium.decryptB64(data, nonce, key); return libsodium.decryptB64(data, nonce, key);
} }
async decryptString(data, nonce, key) { async decryptToUTF8(data, nonce, key) {
return libsodium.decryptString(data, nonce, key); return libsodium.decryptString(data, nonce, key);
} }
@ -77,6 +77,10 @@ export class Crypto {
return libsodium.encryptToB64(data, key); return libsodium.encryptToB64(data, key);
} }
async encryptUTF8(data, key) {
return libsodium.encryptUTF8(data, key);
}
async generateMasterKey() { async generateMasterKey() {
return libsodium.generateMasterKey(); return libsodium.generateMasterKey();
} }