目录

使用PIR协议进行隐私信息检索

场景介绍

PIR协议(隐私信息检索,Private Information Retrieval)适用于在不暴露查询条件的前提下从对方数据集中获取匹配项附加值的场景,例如根据标识查询配置信息、根据凭证查询权益信息等。

通过PIR协议进行隐私信息检索,包含生成隐私目标genPrivacyTarget、隐私搜索privacySearch、获取搜索结果getSearchResult三个步骤。PIR协议在命中时返回匹配元素的附加值。

开发步骤

  1. 接口所需模块导入。

    import { privacyComputation } from '@kit.DataProtectionKit';
  2. 发起端生成隐私目标。

    构造TargetElement(目标元素原始数据与哈希算法)和PrivacyProtocol(协议类型设为PIR_PROTOCOL),调用genPrivacyTarget生成隐私目标。

    const targetElement: privacyComputation.TargetElement = {
      elemData: new Uint8Array([0x01, 0x02, 0x03]),
      hashAlg: privacyComputation.HashAlg.SHA256
    };
    const privacyProtocol: privacyComputation.PrivacyProtocol = {
      dataSetSize: privacyComputation.DataSetSize.SIZE_256,
      protocolType: privacyComputation.ProtocolType.PIR_PROTOCOL
    };
    const privacyTarget: Uint8Array = await privacyComputation.genPrivacyTarget(targetElement, privacyProtocol);
  3. 响应端执行隐私搜索。

    构造Element数组(数据集元素,包含键、哈希算法与附加值elemValue),连同隐私目标和协议配置,调用privacySearch执行隐私搜索。elemValue为命中时返回的附加值,仅在PIR协议中使用。

    const elements: privacyComputation.Element[] = [
      {
        elemKey: new Uint8Array([0x01, 0x02, 0x03]),
        hashAlg: privacyComputation.HashAlg.SHA256,
        elemValue: new Uint8Array([0xA1, 0xA2])
      },
      {
        elemKey: new Uint8Array([0x07, 0x08, 0x09]),
        hashAlg: privacyComputation.HashAlg.SHA256,
        elemValue: new Uint8Array([0xB1, 0xB2])
      }
    ];
    const searchResult: privacyComputation.PrivacySearchResult =
      await privacyComputation.privacySearch(privacyTarget, elements, privacyProtocol);
  4. 发起端获取最终结果。

    将privacySearch返回的结果传入getSearchResult,解密得到SearchResult。matchedResult指示是否命中;命中时attachedValues返回匹配元素的附加值。

    const result: privacyComputation.SearchResult =
      await privacyComputation.getSearchResult(searchResult, privacyProtocol);
    console.info('matched: ' + result.matchedResult);
    if (result.matchedResult && result.attachedValues) {
      console.info('attachedValues count: ' + result.attachedValues.length);
    }