Exporting a Key (ArkTS)

This topic walks you through on how to export the public key of a persistently stored asymmetric key. Currently, HUKS supports export of the ECC, RSA, Ed25519, and X25519 public keys.

How to Develop

  1. Set the key alias (keyAlias), which cannot exceed 64 bytes.

  2. Use exportKeyItem to export the key based on the specified keyAlias and options. options is a reserved parameter and is left empty currently.

  3. In the HuksReturnResult object returned, the public key is in the outData field in the DER format defined in X.509. For details about the format, see Public Key Material Format.

import huks from '@ohos.security.huks';
/* 1. Set the key alias. */
let keyAlias = 'keyAlias';
/* Leave options empty. */
let emptyOptions: huks.HuksOptions = {
    properties: []
};
try {
    /* 2. Export the key. */
    huks.exportKeyItem(keyAlias, emptyOptions, (error, data) => {
        if (error) {
            console.error(`callback: exportKeyItem failed` + error);
        } else {
            console.info(`callback: exportKeyItem success, data = ${JSON.stringify(data)}`);
        }
    });
} catch (error) {
    console.error(`callback: exportKeyItem input arg invalid` + error);
}