romulus-js/src/encrypt.ts
Jack Hadrill 6dec3496e8
All checks were successful
continuous-integration/drone/push Build is passing
Remove buffer dependency.
2022-02-17 19:22:26 +00:00

23 lines
928 B
TypeScript

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: Uint8Array, associatedData: Uint8Array, key: Uint8Array): Uint8Array {
// Generate a nonce.
const nonce = new Uint8Array(16)
uuidv4({}, nonce)
// Encrypt the data using the associated data, newly generated nonce and encryption key.
const ciphertext = Uint8Array.from(cryptoAeadEncrypt(Array.from(message), Array.from(associatedData), Array.from(nonce), Array.from(key)))
// Return the nonce-prepended ciphertext.
return new Uint8Array([...nonce, ...ciphertext])
}