int encrypt_custom(const string& videoFile, EncryptionScope scope) { cout << "Custom encryption usage sample:" << endl; MetadataStream mdStream; if (!mdStream.open(videoFile, MetadataStream::Update | MetadataStream::IgnoreUnknownCompressor | MetadataStream::IgnoreUnknownEncryptor)) { cerr << "Can't open file '" + videoFile + "'" << endl; exit(1); } std::shared_ptr<Encryptor> encryptor = std::make_shared<MyEncryptor>("thereisnospoon"); cout << "Generating metadata..." << endl; generateEncryptedMetadata(mdStream, scope); mdStream.setEncryptor(encryptor); cout << "Saving metadata using custom encryptor..." << endl; cout << "Encryption algorithm info: " << encryptor->getHint() << endl; mdStream.save(); mdStream.close(); cout << "Loading metadata back..." << endl; readAndDumpEncryptedMetadata(videoFile, encryptor); cout << "That's all!" << endl; return 0; }
void readAndDumpEncryptedMetadata(const umf_string& videoFile, std::shared_ptr<Encryptor> encryptor) { cout << "Opening file name '" << videoFile << "'" << endl; // Open new metadata stream to load and print saved metadata MetadataStream loadStream; loadStream.setEncryptor(encryptor); if (!loadStream.open(videoFile, MetadataStream::ReadOnly)) { cerr << "Can't open file " << videoFile << endl; exit(1); } if(loadStream.getUseEncryption()) { cout << "The whole stream is encrypted" << endl; } // Get all schemas vector<string> schemas = loadStream.getAllSchemaNames(); // and dump all the related data to console for(size_t sNum = 0; sNum < schemas.size(); sNum++) { string sName = schemas[sNum]; std::shared_ptr<MetadataSchema> schemaPtr = loadStream.getSchema(sName); bool schemaEncrypted = schemaPtr->getUseEncryption(); cout << "* (" << sNum << ") [schema]: " << sName; if(schemaEncrypted) { cout << ", encrypted"; } cout << endl; if(!loadStream.load(sName)) { cerr << "Error loading schema " << sName << endl; exit(1); } vector< shared_ptr<MetadataDesc> > mDescs = schemaPtr->getAll(); for(size_t setNum = 0; setNum < mDescs.size(); setNum++) { shared_ptr<MetadataDesc> mDesc = mDescs[setNum]; string setName = mDesc->getMetadataName(); bool metadescEncrypted = mDesc->getUseEncryption(); MetadataSet mdSet = loadStream.queryByName(setName); cout << "\t* (" << sNum << "." << setNum << ") [set]: "; cout << setName << "(" << mdSet.size() << " items)"; if(mdSet.empty()) continue; vector<string> fields(mdSet[0]->getFieldNames()); cout << " {"; const char * separator = ""; for(FieldDesc fDesc : mDesc->getFields()) { cout << separator << fDesc.name; if(fDesc.useEncryption) { cout << " encrypted"; } separator = ", "; } cout << "}"; if(metadescEncrypted) { cout << ", encrypted"; } cout << endl; int itemNum = 0; for(const std::shared_ptr<Metadata>& item : mdSet) { if (itemNum++ <= 3) { cout << "\t\t* (" << sNum << "." << setNum << "." << itemNum << ") { "; separator = ""; for (const string& f : fields) { cout << separator << f << "="; try { cout << item->getFieldValue(f).toString(); } catch (umf::Exception& e) { cout << '<' << e.what() << '>'; } if(item->findField(f)->getUseEncryption()) { cout << " encrypted"; } separator = ", "; } cout << " }"; long long t = item->getTime(); if (t >= 0) cout << ", time " << t; if(item->getUseEncryption()) cout << ", encrypted"; cout << endl; } else cout << '.'; } cout << "\n\t\t" << itemNum << " items." << endl; } } // Close metadata stream loadStream.close(); }