Skip to main content
Go to Latest
File
import { DarkflareError } from './DarkflareError.ts'import { decode, hash } from './utilities.ts'
export async function decrypt(message: string, secret?: string) { if (!secret) { const c = window.__d.encryption
if (!c || !c.secretKey) throw new DarkflareError('crypto', 'CRYPTO NOT CONFIGURED')
secret = c.secretKey }
try { const iv = atob(message).slice(0,12)
, alg = { name: 'AES-GCM', iv: new Uint8Array(Array.from(iv).map(ch => ch.charCodeAt(0))) }
, key = await crypto.subtle.importKey('raw', await hash(secret), alg, false, ['decrypt'])
, cipherStr = atob(message).slice(12) , cipherUint8 = new Uint8Array(Array.from(cipherStr).map(ch => ch.charCodeAt(0)))
, buf = await crypto.subtle.decrypt(alg, key, cipherUint8)
return decode(buf) } catch (_err) { throw new DarkflareError('crypto', 'CANNOT DECRYPT MESSAGE') }}