import crypto from "crypto" ; param k param n param ad param plaintext returns export function ccpEncrypt ( k , n , ad , plaintext ) { const cipher = crypto . createCipheriv ( "ChaCha20-Poly1305" as any , k , n , { authTagLength : 16 } ) ; cipher . setAAD ( ad , undefined ) ; const pad = cipher . update ( plaintext ) ; cipher . final ( ) ; const tag = cipher . getAuthTag ( ) ; return Buffer . concat ( [ pad , tag ] ) ; } param k param n param ad param ciphertext returns export function ccpDecrypt ( k , n , ad , ciphertext ) { const decipher = crypto . createDecipheriv ( "ChaCha20-Poly1305" as any , k , n , { authTagLength : 16 , } ) ; decipher . setAAD ( ad , undefined ) ; if ( ciphertext . length === 16 ) { decipher . setAuthTag ( ciphertext ) ; return decipher . final ( ) ; } if ( ciphertext . length > 16 ) { const tag = ciphertext . slice ( ciphertext . length - 16 ) ; const pad = ciphertext . slice ( 0 , ciphertext . length - 16 ) ; decipher . setAuthTag ( tag ) ; let m = decipher . update ( pad ) ; const f = decipher . final ( ) ; m = Buffer . concat ( [ m , f ] ) ; return m ; } }