1# 管理群组关键资产(C/C++) 2 3以下为管理群组关键资产使用示例,请先查看开发指导: 4 5- [新增关键资产(C/C++)](asset-native-add.md) 6- [删除关键资产(C/C++)](asset-native-remove.md) 7- [更新关键资产(C/C++)](asset-native-update.md) 8- [查询关键资产(C/C++)](asset-native-query.md) 9 10## 前置条件 11 12在应用配置文件app.json5中,配置群组ID:demo_group_id。 13 14```json 15{ 16 "app": { 17 //其他配置项此处省略 18 "assetAccessGroups": [ 19 "demo_group_id" 20 ] 21 } 22} 23``` 24 25## 新增群组关键资产 26 27在群组中新增一条密码是demo_pwd,别名是demo_alias,附属信息是demo_label的关键资产,该关键资产在用户首次解锁设备后可被访问。 28 29```c 30#include <string.h> 31 32#include "asset/asset_api.h" 33 34void AddAsset() { 35 static const char *SECRET = "demo_pwd"; 36 static const char *ALIAS = "demo_alias"; 37 static const char *LABEL = "demo_label"; 38 static const char *GROUP_ID = "demo_group_id"; 39 40 Asset_Blob secret = { (uint32_t)(strlen(SECRET)), (uint8_t *)SECRET }; 41 Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; 42 Asset_Blob label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL }; 43 Asset_Blob group_id = { (uint32_t)(strlen(GROUP_ID)), (uint8_t *)GROUP_ID }; 44 Asset_Attr attr[] = { 45 { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_FIRST_UNLOCKED }, 46 { .tag = ASSET_TAG_SECRET, .value.blob = secret }, 47 { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, 48 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = label }, 49 { .tag = ASSET_TAG_GROUP_ID, .value.blob = group_id }, 50 }; 51 52 int32_t ret = OH_Asset_Add(attr, sizeof(attr) / sizeof(attr[0])); 53 if (ret == ASSET_SUCCESS) { 54 // Asset added to the group successfully. 55 } else { 56 // Failed to add Asset to the group. 57 } 58} 59``` 60 61## 删除群组关键资产 62 63在群组中删除别名是demo_alias的关键资产。 64 65```c 66#include <string.h> 67 68#include "asset/asset_api.h" 69 70void RemoveAsset() { 71 static const char *ALIAS = "demo_alias"; 72 static const char *GROUP_ID = "demo_group_id"; 73 74 Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; 75 Asset_Blob group_id = { (uint32_t)(strlen(GROUP_ID)), (uint8_t *)GROUP_ID }; 76 Asset_Attr attr[] = { 77 { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, // 此处指定别名删除单条群组关键资产,也可不指定别名删除多条群组关键资产 78 { .tag = ASSET_TAG_GROUP_ID, .value.blob = group_id }, 79 }; 80 81 int32_t ret = OH_Asset_Remove(attr, sizeof(attr) / sizeof(attr[0])); 82 if (ret == ASSET_SUCCESS) { 83 // Asset removed from the group successfully. 84 } else { 85 // Failed to remove Asset from the group. 86 } 87} 88``` 89 90## 更新群组关键资产 91 92在群组中更新别名是demo_alias的关键资产,将关键资产明文更新为demo_pwd_new,附属信息更新成demo_label_new。 93 94```c 95#include <string.h> 96 97#include "asset/asset_api.h" 98 99void UpdateAsset() { 100 static const char *ALIAS = "demo_alias"; 101 static const char *SECRET = "demo_pwd_new"; 102 static const char *LABEL = "demo_label_new"; 103 static const char *GROUP_ID = "demo_group_id"; 104 105 Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; 106 Asset_Blob new_secret = { (uint32_t)(strlen(SECRET)), (uint8_t *)SECRET }; 107 Asset_Blob new_label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL }; 108 Asset_Blob group_id = { (uint32_t)(strlen(GROUP_ID)), (uint8_t *)GROUP_ID }; 109 Asset_Attr query[] = { 110 { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, 111 { .tag = ASSET_TAG_GROUP_ID, .value.blob = group_id }, 112 }; 113 Asset_Attr attributesToUpdate[] = { 114 { .tag = ASSET_TAG_SECRET, .value.blob = new_secret }, 115 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = new_label }, 116 }; 117 118 int32_t ret = OH_Asset_Update(query, sizeof(query) / sizeof(query[0]), attributesToUpdate, 119 sizeof(attributesToUpdate) / sizeof(attributesToUpdate[0])); 120 if (ret == ASSET_SUCCESS) { 121 // Asset updated in the group successfully. 122 } else { 123 // Failed to update Asset in the group. 124 } 125} 126``` 127 128## 查询单条群组关键资产明文 129 130在群组中查询别名是demo_alias的关键资产明文。 131 132```c 133#include <string.h> 134 135#include "asset/asset_api.h" 136 137void QueryAsset() { 138 static const char *ALIAS = "demo_alias"; 139 static const char *GROUP_ID = "demo_group_id"; 140 Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; 141 Asset_Blob group_id = { (uint32_t)(strlen(GROUP_ID)), (uint8_t *)GROUP_ID }; 142 Asset_Attr attr[] = { 143 { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, // 指定了群组关键资产别名,最多查询到一条满足条件的群组关键资产 144 { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL }, // 此处表示需要返回群组关键资产的所有信息,即属性+明文 145 { .tag = ASSET_TAG_GROUP_ID, .value.blob = group_id }, 146 }; 147 148 Asset_ResultSet resultSet = {0}; 149 int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet); 150 if (ret == ASSET_SUCCESS) { 151 // Parse the resultSet. 152 for (uint32_t i = 0; i < resultSet.count; i++) { 153 // Parse the secret: the data is secret->blob.data, the size is secret->blob.size. 154 Asset_Attr *secret = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_SECRET); 155 } 156 } 157 OH_Asset_FreeResultSet(&resultSet); 158} 159``` 160 161## 查询单条群组关键资产属性 162 163查询别名是demo_alias的关键资产属性。 164 165```c 166#include <string.h> 167 168#include "asset/asset_api.h" 169 170void QueryAttributes() { 171 static const char *ALIAS = "demo_alias"; 172 static const char *GROUP_ID = "demo_group_id"; 173 Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; 174 Asset_Blob group_id = { (uint32_t)(strlen(GROUP_ID)), (uint8_t *)GROUP_ID }; 175 Asset_Attr attr[] = { 176 { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, // 指定了群组关键资产别名,最多查询到一条满足条件的群组关键资产 177 { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ATTRIBUTES }, // 此处表示仅返回群组关键资产属性,不包含群组关键资产明文 178 { .tag = ASSET_TAG_GROUP_ID, .value.blob = group_id }, 179 }; 180 181 Asset_ResultSet resultSet = {0}; 182 int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet); 183 if (ret == ASSET_SUCCESS) { 184 // Parse the result. 185 for (uint32_t i = 0; i < resultSet.count; i++) { 186 // Parse the data label: the data is label->blob.data, the size is label->blob.size. 187 Asset_Attr *label = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_DATA_LABEL_NORMAL_1); 188 } 189 } 190 OH_Asset_FreeResultSet(&resultSet); 191} 192``` 193