• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Deleting a Key (ArkTS)
2
3To ensure data security, delete the key that is no longer required.
4
5## How to Develop
6
7For example, delete a 256-bit HKDF key.
8
91. Set the key alias (**keyAlias**), which cannot exceed 64 bytes.
10
112. Initialize the key property set to specify the properties of the key to delete, for example, delete all keys or a single key. To delete a single key, leave **properties** empty.
12
133. Use [deleteKeyItem](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksdeletekeyitem9) to delete the key.
14
15```ts
16/*
17 * Delete a 256-bit HKDF key. This example uses promise-based APIs.
18 */
19import huks from '@ohos.security.huks';
20import { BusinessError } from '@ohos.base';
21/* 1. Set the key alias. */
22let keyAlias = "test_Key";
23/* 2. Construct an empty object. */
24let huksOptions:huks.HuksOptions = {
25  properties:[]
26}
27class throwObject{
28  isThrow=false;
29}
30function deleteKeyItem(keyAlias: string, huksOptions: huks.HuksOptions, throwObject: throwObject) {
31  return new Promise<void>((resolve, reject) => {
32    try {
33      huks.deleteKeyItem(keyAlias, huksOptions, (error, data)=> {
34        if (error) {
35          reject(error);
36        } else {
37          resolve(data);
38        }
39      });
40    } catch (error) {
41      throwObject.isThrow = true;
42      throw(error as Error);
43    }
44  });
45}
46/* 3. Delete the key. */
47async function publicDeleteKeyFunc(keyAlias:string, huksOptions:huks.HuksOptions) {
48  console.info(`enter promise deleteKeyItem`);
49  let throwObject:throwObject = {isThrow: false};
50  try {
51    await deleteKeyItem(keyAlias, huksOptions, throwObject)
52      .then ((data) => {
53        console.info(`promise: deleteKeyItem key success, data = ${JSON.stringify(data)}`);
54      })
55      .catch((error: BusinessError) => {
56        if (throwObject.isThrow) {
57          throw(error as Error);
58        } else {
59          console.error(`promise: deleteKeyItem failed` + error);
60        }
61      });
62  } catch (error) {
63    console.error(`promise: deletKeyItem input arg invalid` + error);
64  }
65}
66async function testDerive() {
67  await publicDeleteKeyFunc(keyAlias, huksOptions);
68}
69```
70