encryptNodeToShare method

Future<Map> encryptNodeToShare(
  1. Node node,
  2. String keyPath
)

Implementation

Future<Map> encryptNodeToShare(toolbox_api.Node node, String keyPath) async {
  late Map convertedNode;
  //String fullPath = node.path;
  //String identifier = node.name;
  try {
    toolbox_api.Node keyNode = await getNode(keyPath);
    final hexEncodedKey = await keyNode
        .getValue('key')
        .then((value) => value!.getValue("en").toString());
    final onlyKey = hexEncodedKey.split(':');
    final String decodedKey = onlyKey[1];
    log("KEY USED TO ENCRYPT NODE DATA: " + decodedKey);
    final keyVal = Enc.Key.fromBase64(decodedKey);
    final iv = Enc.IV.fromLength(16);
    final enc = Enc.Encrypter(Enc.AES(keyVal, mode: Enc.AESMode.cfb64));
    convertedNode = await convertNodeToJsonString(node);
    Enc.Encrypted encrypted =
        enc.encrypt(json.encode(convertedNode['custom_fields']), iv: iv);
    convertedNode['custom_fields'] = encrypted.base64;
    return convertedNode;
  } catch (e) {
    log("[EXCEPTION] DECRYPT CLOUD DATA");
    throw ReplicationException(e.toString());
  }
}