All files / src/utils decrypt.ts

100% Statements 8/8
100% Branches 0/0
100% Functions 1/1
100% Lines 8/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      7x       3x 3x 3x   3x 3x 2x   1x    
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)
}