All files / src/utils decrypt.ts

12.5% Statements 1/8
100% Branches 0/0
0% Functions 0/1
12.5% Lines 1/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18      4x                            
import { gcm } from '@noble/ciphers/aes'
import { inflateRaw } from 'pako'
 
const SEALED_HEADER = new Uint8Array([0x9e, 0x85, 0xdc, 0xed])
 
// Decrypts data using "@noble/ciphers/aes" package, because default node encryption API is not available in edge runtime
export function decrypt(sealedData: Uint8Array, decryptionKey: Uint8Array) {
  const nonceLength = 12
  const nonce = sealedData.slice(SEALED_HEADER.length, SEALED_HEADER.length + nonceLength)
  const ciphertext = sealedData.slice(SEALED_HEADER.length + nonceLength)
 
  const aes = gcm(decryptionKey, nonce)
  const data = aes.decrypt(ciphertext)
  const decompressed = inflateRaw(data)
 
  return new TextDecoder().decode(decompressed)
}