import { cryptoAeadEncrypt } from './romulus-m' import { v4 as uuidv4 } from 'uuid' /** * Encrypt a message using the Romulus-M encryption algorithm. * N.B. A nonce is automatically prepended to the ciphertext using this function. * @param message The plaintext message to encrypt. * @param associatedData The associated data. * @param key The encryption key. * @returns The nonce-prepended ciphertext. */ export function encrypt (message: Buffer, associatedData: Buffer, key: Buffer): Buffer { // Generate a nonce. const nonce = Buffer.alloc(16) uuidv4({}, nonce) // Encrypt the data using the associated data, newly generated nonce and encryption key. const ciphertext = Buffer.from(cryptoAeadEncrypt(Array.from(message), Array.from(associatedData), Array.from(nonce), Array.from(key))) // Return the nonce-prepended ciphertext. return Buffer.concat([nonce, ciphertext]) }