1# Adding an Asset (C/C++) 2 3## Available APIs 4 5You can use [OH_Asset_Add](../../reference/apis-asset-store-kit/_asset_api.md#oh_asset_add) to add an asset. 6 7The following table describes the attributes for adding an asset. 8 9>**NOTE** 10> 11>In the following table, the attributes starting with **ASSET_TAG_DATA_LABEL** are custom asset attributes reserved. These attributes are not encrypted. Therefore, do not put personal data in these attributes. 12 13| Attribute Name (Asset_Tag) | Attribute Content (Asset_Value) | Mandatory| Description | 14| ------------------------------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ | 15| ASSET_TAG_SECRET | Type: uint8[]<br>Length: 1-1024 bytes | Yes | Asset in plaintext. | 16| ASSET_TAG_ALIAS | Type: uint8[]<br>Length: 1-256 bytes | Yes | Asset alias, which uniquely identifies an asset. | 17| ASSET_TAG_ACCESSIBILITY | Type: uint32_t<br>Value range: see [Asset_Accessibility](../../reference/apis-asset-store-kit/_asset_type.md#asset_accessibility)| No | Access control based on the lock screen status. | 18| ASSET_TAG_REQUIRE_PASSWORD_SET | Type: bool | No | Whether the asset is accessible only when a lock screen password is set. | 19| ASSET_TAG_AUTH_TYPE | Type: uint32_t<br>Value range: see [Asset_AuthType](../../reference/apis-asset-store-kit/_asset_type.md#asset_authtype)| No | Type of user authentication required for accessing the asset. | 20| ASSET_TAG_SYNC_TYPE | Type: uint32_t<br>Value range: see [Asset_SyncType](../../reference/apis-asset-store-kit/_asset_type.md#asset_synctype)| No | Type of sync supported by the asset. | 21| ASSET_TAG_IS_PERSISTENT | Type: bool | No | Whether to retain the asset when the application is uninstalled.<br>**NOTE**: If this parameter is set, the application must [apply for](../AccessToken/declare-permissions.md) the ohos.permission.STORE_PERSISTENT_DATA permission.| 22| ASSET_TAG_DATA_LABEL_CRITICAL_1 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 23| ASSET_TAG_DATA_LABEL_CRITICAL_2 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 24| ASSET_TAG_DATA_LABEL_CRITICAL_3 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 25| ASSET_TAG_DATA_LABEL_CRITICAL_4 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 26| ASSET_TAG_DATA_LABEL_NORMAL_1 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 27| ASSET_TAG_DATA_LABEL_NORMAL_2 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 28| ASSET_TAG_DATA_LABEL_NORMAL_3 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 29| ASSET_TAG_DATA_LABEL_NORMAL_4 | Type: uint8[]<br>Length: 1-2048 bytes | No | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.| 30| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_1<sup>12+</sup> | Type: uint8[]<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.| 31| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_2<sup>12+</sup> | Type: uint8[]<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.| 32| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_3<sup>12+</sup> | Type: uint8[]<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.| 33| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_4<sup>12+</sup> | Type: uint8[]<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.| 34| ASSET_TAG_CONFLICT_RESOLUTION | Type: uint32_t<br>Value range: see [Asset_ConflictResolution](../../reference/apis-asset-store-kit/_asset_type.md#asset_conflictresolution)| No | Policy for resolving the conflict (for example, duplicate alias). | 35| ASSET_TAG_REQUIRE_ATTR_ENCRYPTED<sup>14+</sup> | Type: bool| No| Whether to encrypt the customized asset attribute information. By default, the customized asset attribute information is not encrypted.| 36 37## Constraints 38 39* Alias-based access 40 41 Assets are stored in the ASSET database in ciphertext and uniquely identified by the service identity and alias. The alias of each asset must be unique. 42 43* Custom service data storage 44 45 ASSET provides 12 custom asset attributes starting with **ASSET_TAG_DATA_LABEL** for services. If the 12 custom attributes are used, you can combine multiple data segments in a certain format (for example, JSON) into an ASSET attribute. 46 47 ASSET protects the integrity of the attributes starting with **ASSET_TAG_DATA_LABEL_CRITICAL**. These attributes cannot be changed once written. 48 49## Example 50 51Add an asset that is accessible when the user unlocks the device for the first time. The asset includes password **demo_pwd**, alias **demo_alias**, and additional information **demo_label**. 52 531. Add the dynamic library in the CMake script. 54 ```txt 55 target_link_libraries(entry PUBLIC libasset_ndk.z.so) 56 ``` 57 582. Add an asset. 59 ```c 60 #include <string.h> 61 62 #include "asset/asset_api.h" 63 64 void AddAsset() { 65 static const char *SECRET = "demo_pwd"; 66 static const char *ALIAS = "demo_alias"; 67 static const char *LABEL = "demo_label"; 68 69 Asset_Blob secret = { (uint32_t)(strlen(SECRET)), (uint8_t *)SECRET }; 70 Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; 71 Asset_Blob label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL }; 72 Asset_Attr attr[] = { 73 { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_FIRST_UNLOCKED }, 74 { .tag = ASSET_TAG_SECRET, .value.blob = secret }, 75 { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, 76 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = label }, 77 }; 78 79 int32_t ret = OH_Asset_Add(attr, sizeof(attr) / sizeof(attr[0])); 80 if (ret == ASSET_SUCCESS) { 81 // Asset added successfully. 82 } else { 83 // Failed to add Asset. 84 } 85 } 86 ``` 87