added utils

This commit is contained in:
PhilReact 2025-03-22 06:50:08 +02:00
parent 418c2c79a9
commit d1580a3162
2 changed files with 10 additions and 8 deletions

View File

@ -2,7 +2,7 @@ import React, { createContext, useContext, useMemo } from "react";
import { useAuth, UseAuthProps } from "../hooks/useAuth"; import { useAuth, UseAuthProps } from "../hooks/useAuth";
import { useResources } from "../hooks/useResources"; import { useResources } from "../hooks/useResources";
import { useAppInfo } from "../hooks/useAppInfo"; import { useAppInfo } from "../hooks/useAppInfo";
import { addAndEncryptSymmetricKeys, IdentifierBuilder } from "../utils/encryption"; import { addAndEncryptSymmetricKeys, decryptWithSymmetricKeys, encryptWithSymmetricKeys, IdentifierBuilder } from "../utils/encryption";
import { useIdentifiers } from "../hooks/useIdentifiers"; import { useIdentifiers } from "../hooks/useIdentifiers";
import { objectToBase64 } from "../utils/base64"; import { objectToBase64 } from "../utils/base64";
import { base64ToObject } from "../utils/publish"; import { base64ToObject } from "../utils/publish";
@ -11,7 +11,9 @@ import { base64ToObject } from "../utils/publish";
const utils = { const utils = {
objectToBase64, objectToBase64,
base64ToObject, base64ToObject,
addAndEncryptSymmetricKeys addAndEncryptSymmetricKeys,
encryptWithSymmetricKeys,
decryptWithSymmetricKeys
} }

View File

@ -179,7 +179,7 @@ export const addAndEncryptSymmetricKeys = async ({
}) => { }) => {
try { try {
let highestKey = 0; let highestKey = 0;
if (previousData) { if (previousData && Object.keys(previousData)?.length > 0) {
highestKey = Math.max( highestKey = Math.max(
...Object.keys(previousData || {}) ...Object.keys(previousData || {})
.filter((item) => !isNaN(+item)) .filter((item) => !isNaN(+item))
@ -203,7 +203,7 @@ export const addAndEncryptSymmetricKeys = async ({
}); });
if (encryptedData) { if (encryptedData) {
return encryptedData; return {encryptedData, publicKeys: groupmemberPublicKeys, symmetricKeys: objectToSave};
} else { } else {
throw new Error("Cannot encrypt content"); throw new Error("Cannot encrypt content");
} }
@ -213,7 +213,7 @@ export const addAndEncryptSymmetricKeys = async ({
}; };
export const encryptWithSymmetricKeys = async ({ export const encryptWithSymmetricKeys = async ({
data64, base64,
secretKeyObject, secretKeyObject,
typeNumber = 2, typeNumber = 2,
}: any) => { }: any) => {
@ -226,7 +226,7 @@ export const encryptWithSymmetricKeys = async ({
const highestKeyObject = secretKeyObject[highestKey]; const highestKeyObject = secretKeyObject[highestKey];
// Convert data and keys from base64 // Convert data and keys from base64
const Uint8ArrayData = base64ToUint8Array(data64); const Uint8ArrayData = base64ToUint8Array(base64);
const messageKey = base64ToUint8Array(highestKeyObject.messageKey); const messageKey = base64ToUint8Array(highestKeyObject.messageKey);
if (!(Uint8ArrayData instanceof Uint8Array)) { if (!(Uint8ArrayData instanceof Uint8Array)) {
@ -294,11 +294,11 @@ export const encryptWithSymmetricKeys = async ({
return finalEncryptedData; return finalEncryptedData;
}; };
interface SecretKeyValue { export interface SecretKeyValue {
messageKey: string; messageKey: string;
} }
export const decryptSingle = async ({ export const decryptWithSymmetricKeys = async ({
base64, base64,
secretKeyObject, secretKeyObject,
}: { }: {