1# Checking a Key (ArkTS) 2 3<!--Kit: Universal Keystore Kit--> 4<!--Subsystem: Security--> 5<!--Owner: @wutiantian-gitee--> 6<!--Designer: @HighLowWorld--> 7<!--Tester: @wxy1234564846--> 8<!--Adviser: @zengyawen--> 9 10Check whether a key exists. 11 12## How to Develop 13 141. Specify the key alias. For details about the naming rules, see [Key Generation Overview and Algorithm Specifications](huks-key-generation-overview.md). 15 162. Initialize the key property set to specify [the property tags of keys](../../reference/apis-universal-keystore-kit/js-apis-huks.md#hukstag). When a single key is queried, **TAG** can be empty. 17 183. Use [hasKeyItem](../../reference/apis-universal-keystore-kit/js-apis-huks.md#hukshaskeyitem11) to check whether the key exists. 19 20```ts 21import { huks } from '@kit.UniversalKeystoreKit'; 22/* 1. Set the key alias. */ 23let keyAlias = 'test_key'; 24let isKeyExist: Boolean; 25/* 2. Construct an empty object. */ 26let huksOptions: huks.HuksOptions = { 27 properties: [] 28} 29/* 3. Initialize the key property set. */ 30let generateProperties: huks.HuksParam[] = [ 31 { 32 tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 33 value: huks.HuksKeyAlg.HUKS_ALG_DH 34 }, 35 { 36 tag: huks.HuksTag.HUKS_TAG_PURPOSE, 37 value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE 38 }, 39 { 40 tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 41 value: huks.HuksKeySize.HUKS_DH_KEY_SIZE_2048 42 } 43]; 44let generateHuksOptions: huks.HuksOptions = { 45 properties: generateProperties, 46 inData: new Uint8Array([]) 47} 48/* 3. Generate a key. */ 49function generateKeyItem(keyAlias: string, huksOptions: huks.HuksOptions) { 50 return new Promise<void>((resolve, reject) => { 51 try { 52 huks.generateKeyItem(keyAlias, huksOptions, (error, data) => { 53 if (error) { 54 reject(error); 55 } else { 56 resolve(data); 57 } 58 }); 59 } catch (error) { 60 throw (error as Error); 61 } 62 }); 63} 64async function publicGenKeyFunc(keyAlias: string, huksOptions: huks.HuksOptions): Promise<string> { 65 console.info(`enter promise generateKeyItem`); 66 try { 67 await generateKeyItem(keyAlias, huksOptions) 68 .then((data) => { 69 console.info(`promise: generateKeyItem success, data = ${JSON.stringify(data)}`); 70 }) 71 .catch((error: Error) => { 72 console.error(`promise: generateKeyItem failed, ${JSON.stringify(error)}`); 73 }); 74 return 'Success'; 75 } catch (error) { 76 console.error(`promise: generateKeyItem input arg invalid, ` + JSON.stringify(error)); 77 return 'Failed'; 78 } 79} 80async function testGenKey(): Promise<string> { 81 let ret = await publicGenKeyFunc(keyAlias, generateHuksOptions); 82 return ret; 83} 84/* Check whether the key exists. */ 85function hasKeyItem(keyAlias: string, huksOptions: huks.HuksOptions) { 86 return new Promise<boolean>((resolve, reject) => { 87 try { 88 huks.hasKeyItem(keyAlias, huksOptions, (error, data) => { 89 if (error) { 90 reject(error); 91 } else { 92 if (data !== null && data.valueOf() !== null) { 93 resolve(data.valueOf()); 94 } else { 95 resolve(false); 96 } 97 } 98 }); 99 } catch (error) { 100 throw (error as Error); 101 } 102 }); 103} 104async function check(): Promise<string> { 105 try { 106 /* 1. Generate a key. */ 107 let genResult = await testGenKey(); 108 /* 2. Check whether the key exists. */ 109 if (genResult === 'Success') { 110 isKeyExist = await hasKeyItem(keyAlias, huksOptions); 111 console.info(`callback: hasKeyItem success, isKeyExist = ${isKeyExist}`); 112 } else { 113 console.error('Key generation failed, skipping query'); 114 return 'Failed'; 115 } 116 return 'Success'; 117 } catch (error) { 118 console.error(`callback: hasKeyItem input arg invalid, ` + JSON.stringify(error)); 119 return 'Failed'; 120 } 121} 122``` 123