在 MIP 保護 SDK 中 mip::ProtectionHandler ,會公開用來加密和解密受保護資料流程和緩衝區的函式、執行存取檢查、取得發佈授權,以及從受保護的資訊取得屬性。
需求
ProtectionHandler建立 以使用特定檔案需要:
- 進行
mip::MipContext - 進行
mip::ProtectionProfile mip::ProtectionEngine已新增至 的ProtectionProfile- 繼承 的
mip::ProtectionHandler::Observer類別。 mip::ProtectionDescriptor或發佈授權
建立保護處理常式
mip::ProtectionHandler物件是針對保護 或 取 用 作業所建構。 處理常式是使用四個函式的其中一個來建立,視案例而定。
mip::ProtectionEngine->CreateProtectionHandlerForConsumptionAsync()mip::ProtectionEngine->CreateProtectionHandlerForConsumption()mip::ProtectionEngine->CreateProtectionHandlerForPublishingAsync()mip::ProtectionEngine->CreateProtectionHandlerForPublishing()
這些函式接受 mip::ProtectionHandler::PublishingSettings 或 mip::ProtectionHandler::ConsumptionSettings 物件。
建立發佈處理常式
建立發行處理常式需要三個步驟:
- 建立
mip::ProtectionDescriptor物件。 mip::ProtectionDescriptor使用 來具現化mip::ProtectionHandler::PublishingSettings。- 呼叫
mip::ProtectionEngine::CreateProtectionHandlerForPublishingAsync()傳入PublishingSettings物件、觀察者和承諾。
從描述元建立
如果保護尚未保護的內容,或將新的保護套用至內容時,這表示其已解密, mip::ProtectionDescriptor 則必須建構 。 建構之後,它會用來具現化 mip::ProtectionHandler::PublishingSettings() 物件。 結果會透過 mip::ProtectionHandler::Observer 傳回。
// Create the protection descriptor, passing in a templateId.
auto descriptorBuilder = mip::ProtectionDescriptorBuilder::CreateFromTemplate(protectionOptions.templateId);
std::shared_ptr<mip::ProtectionDescriptor> descriptor = descriptorBuilder->Build();
// Define the handler promise, future, and observer.
auto handlerPromise = std::make_shared<std::promise<std::shared_ptr<ProtectionHandler>>>();
auto handlerFuture = handlerPromise->get_future();
auto handlerObserver = std::make_shared<ProtectionHandlerObserverImpl>();
// Create the PublishingSettings object using the previously-created descriptor as input.
mip::ProtectionHandler::PublishingSettings publishingSettings = mip::ProtectionHandler::PublishingSettings(descriptor);
// Create/get the publishing handler from the publishing settings, observer, and promise.
mEngine->CreateProtectionHandlerForPublishingAsync(publishingSettings, handlerObserver, handlerPromise);
auto handler = handlerFuture.get();
return handler;
成功建立 ProtectionHandler 物件之後,可以執行保護作業(加密/解密)。 發行 授權 必須從處理常式擷取,並以加密的內容儲存。 您可以呼叫下列專案來擷取發佈授權: handler->GetSerializedPublishingLicense();
沒有對應發佈授權 的受保護內容無法解密 。
建立取用處理程式
建立發行處理常式需要三個步驟:
- 從受保護的內容擷取序列化的發佈授權
std::vector<uint8_t>。 - 使用序列化發行授權來具現化
mip::ProtectionHandler::ConsumptionSettings。 - 呼叫
mip::ProtectionEngine::CreateProtectionHandlerForConsumptionAsync()傳入ConsumptionSettings物件、觀察者和承諾。
此範例假設發佈授權已經從某些來源讀取並儲存在 中 std::vector<uint8_t> serializedPublishingLicense 。
//TODO: Implement GetPublishingLicense()
//Snip implies that function reads PL from source file, database, stream, etc.
std::vector<uint8_t> serializedPublishingLicense = GetPublishingLicense(filePath);
// Define the handler promise, future, and observer.
auto handlerPromise = std::make_shared<std::promise<std::shared_ptr<ProtectionHandler>>>();
auto handlerFuture = handlerPromise->get_future();
shared_ptr<ProtectionHandlerObserverImpl> handlerObserver = std::make_shared<ProtectionHandlerObserverImpl>();
// Create the consumption settings object from the publishing license.
mip::ProtectionHandler::ConsumptionSettings consumptionSettings = mip::ProtectionHandler::ConsumptionSettings(serializedPublishingLicense);
// Create/get the publishing handler from the publishing settings, observer, and promise.
mEngine->CreateProtectionHandlerForConsumptionAsync(consumptionSettings, handlerObserver, handlerPromise);
auto handler = handlerFuture.get();