注册/注销Provider(C/C++)
====================

从API 22开始，huksExternalCrypto提供Provider注册和注销功能接口。

#### 注册Provider

#### \[h2\]在CMake脚本中链接相关动态库

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

#### \[h2\]开发步骤

1.  构造注册参数，需要传入[OH\_HUKS\_EXT\_CRYPTO\_TAG\_ABILITY\_NAME](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-type-h#oh_huks_externalcryptotag)。
    
2.  调用注册接口[OH\_Huks\_RegisterProvider](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-api-h#oh_huks_registerprovider)。
    

#### 开发案例

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

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 struct OH\_Huks\_Blob g\_abilityName = {
    (uint32\_t)strlen("testAbility"),
    (uint8\_t \*)"testAbility"
};

struct OH\_Huks\_Blob g\_providerName = {
    (uint32\_t)strlen("testProviderName"),
    (uint8\_t \*)"testProviderName"
};

static struct OH\_Huks\_ExternalCryptoParam g\_abilityParams\[\] = {
    {
        .tag = OH\_HUKS\_EXT\_CRYPTO\_TAG\_ABILITY\_NAME,
        .blob = g\_abilityName
    },
};

static napi\_value registerProvider(napi\_env env, napi\_callback\_info info)
{
    struct OH\_Huks\_ExternalCryptoParamSet \*providerParamSet = nullptr;
    OH\_Huks\_Result ohResult;
    do {
        ohResult = InitParamSet(&providerParamSet, g\_abilityParams,
            sizeof(g\_abilityParams) / sizeof(OH\_Huks\_ExternalCryptoParam));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        ohResult = OH\_Huks\_RegisterProvider(&g\_providerName, providerParamSet);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
    } while (0);
    OH\_Huks\_FreeExternalCryptoParamSet(&providerParamSet);

    napi\_value ret;
    napi\_create\_int32(env, ohResult.errorCode, &ret);
    return ret;
}

#### 注销Provider

#### \[h2\]在CMake脚本中链接相关动态库

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

#### \[h2\]开发步骤

1.  构造注销参数，注销单个ability需要传入[OH\_HUKS\_EXT\_CRYPTO\_TAG\_ABILITY\_NAME](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-type-h#oh_huks_externalcryptotag)。批量注销不需要传入[OH\_HUKS\_EXT\_CRYPTO\_TAG\_ABILITY\_NAME](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-type-h#oh_huks_externalcryptotag)。
    
2.  调用注销接口[OH\_Huks\_UnregisterProvider](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-huks-external-crypto-api-h#oh_huks_unregisterprovider)。
    

**注销单个ability**

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

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 struct OH\_Huks\_Blob g\_abilityName = {
    (uint32\_t)strlen("testAbility"),
    (uint8\_t \*)"testAbility"
};

struct OH\_Huks\_Blob g\_providerName = {
    (uint32\_t)strlen("testProviderName"),
    (uint8\_t \*)"testProviderName"
};

static struct OH\_Huks\_ExternalCryptoParam g\_abilityParams\[\] = {
    {
        .tag = OH\_HUKS\_EXT\_CRYPTO\_TAG\_ABILITY\_NAME,
        .blob = g\_abilityName
    },
};

static napi\_value unregisterProvider(napi\_env env, napi\_callback\_info info)
{
    struct OH\_Huks\_ExternalCryptoParamSet \*providerParamSet = nullptr;
    OH\_Huks\_Result ohResult;
    do {
        ohResult = InitParamSet(&providerParamSet, g\_abilityParams,
            sizeof(g\_abilityParams) / sizeof(OH\_Huks\_ExternalCryptoParam));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        ohResult = OH\_Huks\_UnregisterProvider(&g\_providerName, providerParamSet);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
    } while (0);
    OH\_Huks\_FreeExternalCryptoParamSet(&providerParamSet);

    napi\_value ret;
    napi\_create\_int32(env, ohResult.errorCode, &ret);
    return ret;
}

**批量注销**

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

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;
}

struct OH\_Huks\_Blob g\_providerName = {
    (uint32\_t)strlen("testProviderName"),
    (uint8\_t \*)"testProviderName"
};

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

static napi\_value unregisterProvider(napi\_env env, napi\_callback\_info info)
{
    struct OH\_Huks\_ExternalCryptoParamSet \*providerParamSet = nullptr;
    OH\_Huks\_Result ohResult;
    do {
        ohResult = InitParamSet(&providerParamSet, g\_abilityParams,
            sizeof(g\_abilityParams) / sizeof(OH\_Huks\_ExternalCryptoParam));
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
        ohResult = OH\_Huks\_UnregisterProvider(&g\_providerName, providerParamSet);
        if (ohResult.errorCode != OH\_HUKS\_SUCCESS) {
            break;
        }
    } while (0);
    OH\_Huks\_FreeExternalCryptoParamSet(&providerParamSet);

    napi\_value ret;
    napi\_create\_int32(env, ohResult.errorCode, &ret);
    return ret;
}