MCPcopy Create free account
hub / github.com/ConsortiumAI/Consortium / decryptWithDataKey

Function decryptWithDataKey

packages/consortium-web/src/lib/encryption.ts:163–185  ·  view source on GitHub ↗
(
  bundle: Uint8Array,
  dataKey: Uint8Array,
)

Source from the content-addressed store, hash-verified

161}
162
163export async function decryptWithDataKey(
164 bundle: Uint8Array,
165 dataKey: Uint8Array,
166): Promise<unknown | null> {
167 if (bundle.length < 1 || bundle[0] !== 0) return null;
168 if (bundle.length < 13 + 16) return null;
169
170 const nonce = bundle.slice(1, 13);
171 // SubtleCrypto expects authTag appended to ciphertext
172 const ciphertextWithTag = bundle.slice(13);
173
174 try {
175 const key = await getAesKey(dataKey);
176 const decrypted = await crypto.subtle.decrypt(
177 { name: 'AES-GCM', iv: toArrayBuffer(nonce) },
178 key,
179 toArrayBuffer(ciphertextWithTag),
180 );
181 return JSON.parse(new TextDecoder().decode(decrypted));
182 } catch {
183 return null;
184 }
185}
186
187// --- Unified encrypt/decrypt ---
188

Callers 1

decryptFunction · 0.70

Calls 2

getAesKeyFunction · 0.85
toArrayBufferFunction · 0.85

Tested by

no test coverage detected