查询认证状态(C/C++)
=============

从API 22开始，huksExternalCrypto提供PIN码认证状态查询功能接口。应用可以通过该接口查询PIN码是否认证通过。具体的场景介绍及规格，请参考[Ukey PIN码认证介绍及规格](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/huks-ukey-pin-authentication-management-overview)。

#### 在CMake脚本中链接相关动态库

target\_link\_libraries(entry PUBLIC libhuks\_ndk.z.so libhuks\_external\_crypto.z.so)

#### 开发步骤

1.  通过证书管理系统能力提供的[证书选择接口](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-certmanagerdialog#certificatemanagerdialogopenauthorizedialog22)获取[keyUri](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-certmanagerdialog#certreference22)，并将其作为resourceId。
    
2.  调用[OH\_Huks\_InitExternalCryptoParamSet](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-api-h#oh_huks_initexternalcryptoparamset)指定参数配置。
    
3.  调用[OH\_Huks\_GetUkeyPinAuthState](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-api-h#oh_huks_getukeypinauthstate)获取PIN码认证状态。
    

#### 开发案例

#include "huks/native\_huks\_external\_crypto\_api.h"
#include "huks/native\_huks\_param.h"
#include "napi/native\_api.h"
#include <string.h>

OH\_Huks\_Result InitParamSet(
    struct OH\_Huks\_ExternalCryptoParamSet \*\*paramSet,
    const struct OH\_Huks\_ExternalCryptoParam \*params,
    uint32\_t paramCount)
{
    OH\_Huks\_Result ret = OH\_Huks\_InitExternalCryptoParamSet(paramSet);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        return ret;
    }
    ret = OH\_Huks\_AddExternalCryptoParams(\*paramSet, params, paramCount);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        OH\_Huks\_FreeExternalCryptoParamSet(paramSet);
        return ret;
    }
    ret = OH\_Huks\_BuildExternalCryptoParamSet(paramSet);
    if (ret.errorCode != OH\_HUKS\_SUCCESS) {
        OH\_Huks\_FreeExternalCryptoParamSet(paramSet);
        return ret;
    }
    return ret;
}

static const char \*resourceId = "{\\"providerName\\":\\"testProviderName\\",\\"abilityName\\":\\"CryptoExtension\\",\\"bundleName\\":\\"com.example.cryptoapplication\\",\\"index\\":{\\"key\\":\\"testKey\\"}}";

static struct OH\_Huks\_ExternalCryptoParam g\_getPinStateParamsTest\[\] = {};

static napi\_value GetUkeyPinAuthState(napi\_env env, napi\_callback\_info info)
{
    struct OH\_Huks\_Blob g\_resourceId = {
        (uint32\_t)strlen(resourceId),
        (uint8\_t \*)resourceId
    };
    struct OH\_Huks\_ExternalCryptoParamSet \*pinStateParamSet = nullptr;
    OH\_Huks\_ExternalPinAuthState authState = OH\_HUKS\_EXT\_CRYPTO\_PIN\_NO\_AUTH;
    OH\_Huks\_Result ohResult;
    do {
        ohResult = InitParamSet(&pinStateParamSet, g\_getPinStateParamsTest,
            sizeof(g\_getPinStateParamsTest) / sizeof(OH\_Huks\_ExternalCryptoParam));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        ohResult = OH\_Huks\_GetUkeyPinAuthState(&g\_resourceId, pinStateParamSet, &authState);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
    } while (0);
    OH\_Huks\_FreeExternalCryptoParamSet(&pinStateParamSet);
    
    napi\_value ret;
    napi\_create\_int32(env, ohResult.errorCode, &ret);
    return ret;
}