encodePayload static method

String encodePayload(
  1. Map<String, dynamic> data, {
  2. required Nonce nonce,
  3. ChaCha20? chaCha20,
  4. bool compressed = true,
})

Encodes the payload with a given nonce and key. Compresses the content if a key is present before encryption.

Implementation

static String encodePayload(Map<String, dynamic> data, {required Nonce nonce, ChaCha20? chaCha20, bool compressed = true}) {
  if ( chaCha20 == null ) compressed = false;
  String output = jsonEncode(data);
  if ( !compressed && chaCha20 == null ) return output;
  if ( compressed ) {
    ZLibEncoder zlibE = ZLibEncoder(level: 9);
    output = base64Encode(
      zlibE.convert(
          utf8.encode(
              output
          )
      ),
    );
  }

  if ( chaCha20 != null ) {
    return chaCha20.encrypt(output, nonce);
  } else {
    return output;
  }
}