Enterprise Data Guard Kit allows apps to obtain an enterprise recovery key. After successful configuration of the enterprise public key certificate, the enterprise recovery key can be directly obtained to decrypt hard disk data that has been encrypted.
An enterprise recovery key can only be obtained once. After it is obtained, it can be decrypted and stored on a device that holds the enterprise private key. If an enterprise recovery key needs to be obtained again, you need to delete the recovery key first and then perform the export.
For details about these APIs, please refer to the API Reference.
| API | Description |
|---|---|
| getEnterpriseRecoveryKey(userId: number): Promise<EnterpriseRecoveryKeyInfo> | Obtains an enterprise recovery key using a promise. |
Import required modules.
import { buffer } from '@kit.ArkTS';
import { osAccount, BusinessError } from '@kit.BasicServicesKit';
import { recoveryKey } from '@kit.EnterpriseDataGuardKit';
import { hilog } from '@kit.PerformanceAnalysisKit'; Call the getEnterpriseRecoveryKey API, pass the ID of the user whose enterprise recovery key needs to be obtained, and obtain the enterprise recovery key.
const TAG: string = 'EnterpriseRecoveryKey_GetRecoveryKey';
const DOMAIN: number = 0x0000;
/**
* Obtain the enterprise recovery key for decrypting hard disk data and return the result asynchronously using a promise.
*/
function getEnterpriseRecoveryKey() {
let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
accountManager.getOsAccountLocalId().then((userId: number) => {
hilog.info(DOMAIN, TAG, `getEnterpriseRecoveryKey userId: ${userId}.`);
recoveryKey.getEnterpriseRecoveryKey(userId).then((info: recoveryKey.EnterpriseRecoveryKeyInfo) => {
hilog.info(DOMAIN, TAG, `Succeeded in getting enterprise recovery key.`);
hilog.info(DOMAIN, TAG,
`EnterpriseRecoveryKeyInfo enterpriseRecoveryKey: ${buffer.from(info.enterpriseRecoveryKey)
.toString('hex')}`);
hilog.info(DOMAIN, TAG,
`EnterpriseRecoveryKeyInfo exportPublicKey: ${buffer.from(info.exportPublicKey).toString('hex')}`);
hilog.info(DOMAIN, TAG, `EnterpriseRecoveryKeyInfo iv: ${buffer.from(info.iv).toString('hex')}`);
hilog.info(DOMAIN, TAG, `EnterpriseRecoveryKeyInfo tag: ${buffer.from(info.tag).toString('hex')}`);
}).catch((error: BusinessError) => {
hilog.error(DOMAIN, TAG,
`Failed to get enterprise recovery key. Code: ${error.code}, message: ${error.message}`);
});
});
}