revokeEnc method

Future<String> revokeEnc(
  1. String plainPrevEncKey
)

Revoke the encryption. All the encrypted files will be rewritten with their respective plaintext and the encryption key hashes will be deleted from the server.

Implementation

Future<String> revokeEnc(String plainPrevEncKey) async {
  /// Verify old encryption key before moving forward
  if (await verifyEncKey(plainPrevEncKey)) {
    /// Create hash values for encrypted key
    String encKey = sha256
        .convert(utf8.encode(plainPrevEncKey))
        .toString()
        .substring(0, 32);

    /// Get the list of locations of files that are encrypted
    List encFileList = await getEncFileList();

    /// Loop over each file, decrypt the encrypted values, and write the
    /// plaintext values to the file
    for (var i = 0; i < encFileList.length; i++) {
      String encFilePath = encFileList[i];
      var fileInfo = await fetchFile(encFilePath);
      EncProfile encFile = EncProfile(fileInfo.toString());
      String encFileCont = encFile.getEncFileCont();
      String encFileIv = encFile.getEncIvVal();

      String plainFileCont = decryptVal(encKey, encFileCont, encFileIv);
      String fileName = encFilePath.split('/').last;

      List filePathList = encFilePath.split('/');
      filePathList.remove(filePathList.last);
      String fileDir = filePathList.join('/');

      String delResponse = await deleteItem(true, encFilePath);
      String fileCreateRes =
          await createItem(true, fileName, plainFileCont, fileLoc: fileDir);

      if (delResponse == 'ok' && fileCreateRes == 'ok') {
        continue;
      } else {
        throw Exception('Failed to revoke encrypted file $encFilePath.');
      }
    }

    /// Delete the file which stores the encryption keys
    String delKeyFileRes = await deleteItem(true, encKeyFileLoc);
    String delKeyDirRes = await deleteItem(false, encKeyFileDir + '/');

    if (delKeyFileRes == 'ok' && delKeyDirRes == 'ok') {
      return 'ok';
    } else {
      throw Exception('Failed to delete encryption key file.');
    }
  } else {
    throw Exception('Failed to verify encryption key.');
  }
}