huks-guidelines.md 5.4 KB
Newer Older
A
annie_wangli 已提交
1 2 3 4
# HUKS Development

## When to Use

A
Annie_wang 已提交
5
 OpenHarmony Universal KeyStore (HUKS) provides KeyStore (KS) capabilities for applications, including key management and key cryptography operations. HUKS also provides APIs for applications to import or generate keys.
A
annie_wangli 已提交
6 7 8 9


## Available APIs

A
Annie_wang 已提交
10
| API                                                      | Description       |
A
annie_wangli 已提交
11
| ------------------------------------------------------------ | ---------------- |
A
Annie_wang 已提交
12 13 14 15 16 17 18 19
| generateKey(keyAlias: string, options: HuksOptions, callback: AsyncCallback<HuksResult>) : void| Generates a key. This method uses an asynchronous callback to return the result.        |
| generateKey(keyAlias: string, options: HuksOptions) : Promise<HuksResult>| Generates a key. This method uses a promise to return the result.        |
| exportKey(keyAlias: string, options: HuksOptions, callback: AsyncCallback<HuksResult>) : void| Exports the public key. This method uses an asynchronous callback to return the result.        |
| exportKey(keyAlias: string, options: HuksOptions) : Promise<HuksResult>| Exports the public key. This method uses a promise to return the result.        |
| isKeyExist(keyAlias: string, options: HuksOptions, callback: AsyncCallback<boolean>) : void | Checks whether a key exists. This method uses an asynchronous callback to return the result.|
| isKeyExist(keyAlias: string, options: HuksOptions) : Promise<boolean> | Checks whether a key exists. This method uses a promise to return the result.|
| deleteKey(keyAlias: string, options: HuksOptions, callback: AsyncCallback<HuksResult>) : void| Deletes a key. This method uses an asynchronous callback to return the result.        |
| deleteKey(keyAlias: string, options: HuksOptions) : Promise<HuksResult>| Deletes a key. This method uses a promise to return the result.        |
A
annie_wangli 已提交
20 21 22 23 24 25 26 27 28

## How to Develop

1. Import the HUKS module.

   ```js
   import huks from '@ohos.security.huks'
   ```

A
Annie_wang 已提交
29
2. Call **generateKey()** to generate a key.
A
annie_wangli 已提交
30

A
annie_wangli 已提交
31
   **keyAlias** indicates the alias of the key generated. **options** indicates the parameters used for generating the key. Set **options** based on the actual situation.
A
annie_wangli 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

   The value returned indicates whether the key is successfully generated.

   ```js
   var alias = 'testAlias';
   var properties = new Array();
   properties[0] = {
     tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
     value: huks.HuksKeyAlg.HUKS_ALG_ECC
   };
   properties[1] = {
     tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
     value: huks.HuksKeySize.HUKS_ECC_KEY_SIZE_224
   };
   properties[2] = {
     tag: huks.HuksTag.HUKS_TAG_PURPOSE,
     value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE
   };
   properties[3] = {
     tag: huks.HuksTag.HUKS_TAG_DIGEST,
     value: huks.HuksKeyDigest.HUKS_DIGEST_NONE
   };
   var options = {
     properties: properties
   }
   var resultA = huks.generateKey(alias, options);
   ```

3. Call **Init()** to initialize the key.

A
Annie_wang 已提交
62
   **Alias** indicates the alias of the key to initialize, and **options** indicates the parameters used for initializing the key. Set **options** based on the actual situation.
A
annie_wangli 已提交
63 64 65 66 67 68 69 70

   The value returned indicates whether the **Init** operation is successful.

   ```js
   var alias = 'test001'
   var properties = new Array();
   properties[0] = {
     tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
A
Annie_wang 已提交
71
     value: huks.HuksKeyAlg.HUKS_ALG_DH
A
annie_wangli 已提交
72 73
   };
   properties[1] = {
A
Annie_wang 已提交
74 75
     tag: huks.HuksTag.HUKS_TAG_PURPOSE,
     value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE
A
annie_wangli 已提交
76 77
   };
   properties[2] = {
A
Annie_wang 已提交
78 79
     tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
     value: huks.HuksKeySize.HUKS_DH_KEY_SIZE_4096
A
annie_wangli 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
   };
   var options = {
     properties: properties
   };
   huks.init(alias, options, function(err, data) {
       if (err.code !== 0) {
           console.log("test init err information: " + JSON.stringify(err));
       } else {
           console.log(`test init data: ${JSON.stringify(data)}`);
       }
   })
   ```
   
4. Call **update()** to update the key.

A
Annie_wang 已提交
95
   **handle** indicates the session ID for updating the key. **options** indicates the parameters used for updating the key. Set **options** based on the actual situation.
A
annie_wangli 已提交
96 97 98 99 100 101 102

   The value returned indicates whether the **Update** operation is successful.

   ```js
   var properties = new Array();
   properties[0] = {
     tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
A
Annie_wang 已提交
103
     value: huks.HuksKeyAlg.HUKS_ALG_DH
A
annie_wangli 已提交
104 105
   };
   properties[1] = {
A
Annie_wang 已提交
106 107
     tag: huks.HuksTag.HUKS_TAG_PURPOSE,
     value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE
A
annie_wangli 已提交
108 109
   };
   properties[2] = {
A
Annie_wang 已提交
110 111
     tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
     value: huks.HuksKeySize.HUKS_DH_KEY_SIZE_4096
A
annie_wangli 已提交
112 113 114 115 116 117 118 119 120
   };
   var options = {
     properties: properties
   };
   var result = huks.update(handle, options)
   ```
   
5. Call **finish()** to complete the operation.

A
Annie_wang 已提交
121
   **handle** indicates the session ID of the **Finish** operation. **options** indicates the parameters used for this operation. Set **options** based on the actual situation.
A
annie_wangli 已提交
122 123 124 125 126 127 128

   The value returned indicates whether the **Finish** operation is successful.

   ```js
   var properties = new Array();
   properties[0] = {
     tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
A
Annie_wang 已提交
129
     value: huks.HuksKeyAlg.HUKS_ALG_DH
A
annie_wangli 已提交
130 131
   };
   properties[1] = {
A
Annie_wang 已提交
132 133
     tag: huks.HuksTag.HUKS_TAG_PURPOSE,
     value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE
A
annie_wangli 已提交
134 135
   };
   properties[2] = {
A
Annie_wang 已提交
136 137
     tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
     value: huks.HuksKeySize.HUKS_DH_KEY_SIZE_4096
A
annie_wangli 已提交
138 139 140 141 142 143
   };
   var options = {
     properties: properties
   };
   var result = huks.finish(handle, options) 
   ```