HcfResult HcfMdCreate(const char *algoName, HcfMd **md) { if (!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (md == NULL)) { LOGE("Invalid input params while creating md!"); return HCF_INVALID_PARAMS; } HcfMdSpiCreateFunc createSpiFunc = FindAbility(algoName); if (createSpiFunc == NULL) { LOGE("Algo not supported!"); return HCF_NOT_SUPPORT; } HcfMdImpl *returnMdApi = (HcfMdImpl *)HcfMalloc(sizeof(HcfMdImpl), 0); if (returnMdApi == NULL) { LOGE("Failed to allocate Md Obj memory!"); return HCF_ERR_MALLOC; } if (strcpy_s(returnMdApi->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) { LOGE("Failed to copy algoName!"); HcfFree(returnMdApi); return HCF_ERR_COPY; } HcfMdSpi *spiObj = NULL; HcfResult res = createSpiFunc(algoName, &spiObj); if (res != HCF_SUCCESS) { LOGE("Failed to create spi object!"); HcfFree(returnMdApi); return res; } returnMdApi->base.base.getClass = GetMdClass; returnMdApi->base.base.destroy = MdDestroy; returnMdApi->base.update = Update; returnMdApi->base.doFinal = DoFinal; returnMdApi->base.getMdLength = GetMdLength; returnMdApi->base.getAlgoName = GetAlgoName; returnMdApi->spiObj = spiObj; *md = (HcfMd *)returnMdApi; return HCF_SUCCESS; } 代码讲解
时间: 2024-04-27 15:24:48 浏览: 19
这是一段 C 语言代码,主要是用来创建消息摘要(message digest)的对象。以下是对其主要部分的解释:
1. `HcfMdCreate` 函数接收两个参数,分别是算法名称 `algoName` 和一个指向 `HcfMd` 指针的指针 `md`。
2. 在函数内部,首先检查输入参数是否合法。如果算法名称为空或长度超过限制,或者 `md` 为空,就返回 `HCF_INVALID_PARAMS` 错误码。
3. 接着,调用 `FindAbility` 函数查找指定算法的实现函数。如果找不到,就返回 `HCF_NOT_SUPPORT` 错误码。
4. 如果找到了实现函数,就分配一个 `HcfMdImpl` 对象的内存,并将算法名称拷贝到该对象中。如果内存分配失败,就返回 `HCF_ERR_MALLOC` 错误码。
5. 接下来,调用实现函数创建一个 `HcfMdSpi` 对象,如果创建失败,就释放先前分配的内存,并返回实现函数返回的错误码。
6. 如果成功创建了 `HcfMdSpi` 对象,就将其与 `HcfMdImpl` 对象关联起来,并将 `HcfMdImpl` 对象转换为 `HcfMd` 对象,最后将 `HcfMd` 对象赋值给 `md` 指针。
7. 最后,返回 `HCF_SUCCESS` 表示创建成功。
该代码中还涉及到了一些其他函数,如 `HcfMalloc`、`HcfFree`、`strcpy_s` 等,这些函数的作用在此不做赘述。
相关问题
HcfResult HcfMdCreate(const char *algoName, HcfMd **md) { if (!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (md == NULL)) { LOGE("Invalid input params while creating md!"); return HCF_INVALID_PARAMS; } HcfMdSpiCreateFunc createSpiFunc = FindAbility(algoName); if (createSpiFunc == NULL) { LOGE("Algo not supported!"); return HCF_NOT_SUPPORT; } HcfMdImpl *returnMdApi = (HcfMdImpl *)HcfMalloc(sizeof(HcfMdImpl), 0); if (returnMdApi == NULL) { LOGE("Failed to allocate Md Obj memory!"); return HCF_ERR_MALLOC; } if (strcpy_s(returnMdApi->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) { LOGE("Failed to copy algoName!"); HcfFree(returnMdApi); return HCF_ERR_COPY; } HcfMdSpi *spiObj = NULL; HcfResult res = createSpiFunc(algoName, &spiObj); if (res != HCF_SUCCESS) { LOGE("Failed to create spi object!"); HcfFree(returnMdApi); return res; }
这段代码是用来创建一个HcfMd对象的,其中包括以下步骤:
1. 检查输入参数是否合法;
2. 查找指定算法名对应的能力函数;
3. 分配HcfMdImpl对象的内存空间;
4. 将算法名复制到HcfMdImpl对象的algoName成员中;
5. 调用能力函数创建HcfMdSpi对象;
6. 如果创建失败,释放HcfMdImpl对象并返回错误码。
讲解代码 HcfResult HcfAsyKeyGeneratorCreate(const char *algoName, HcfAsyKeyGenerator **returnObj) { if ((!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN)) || (returnObj == NULL)) { return HCF_INVALID_PARAMS; } HcfAsyKeyGenParams params = { 0 }; if (ParseAndSetParameter(algoName, ¶ms, ParseAsyKeyGenParams) != HCF_SUCCESS) { LOGE("Failed to parser parmas!"); return HCF_INVALID_PARAMS; } HcfAsyKeyGeneratorSpiCreateFunc createSpiFunc = FindAbility(¶ms); if (createSpiFunc == NULL) { return HCF_NOT_SUPPORT; } HcfAsyKeyGeneratorImpl *returnGenerator = (HcfAsyKeyGeneratorImpl *)HcfMalloc(sizeof(HcfAsyKeyGeneratorImpl), 0); if (returnGenerator == NULL) { LOGE("Failed to allocate returnGenerator memory!"); return HCF_ERR_MALLOC; } if (strcpy_s(returnGenerator->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) { LOGE("Failed to copy algoName!"); HcfFree(returnGenerator); return HCF_ERR_COPY; } HcfAsyKeyGeneratorSpi *spiObj = NULL; int32_t res = HCF_SUCCESS; res = createSpiFunc(¶ms, &spiObj); if (res != HCF_SUCCESS) { LOGE("Failed to create spi object!"); HcfFree(returnGenerator); return res; } returnGenerator->base.base.destroy = DestroyAsyKeyGenerator; returnGenerator->base.base.getClass = GetAsyKeyGeneratorClass; returnGenerator->base.convertKey = ConvertKey; returnGenerator->base.generateKeyPair = GenerateKeyPair; returnGenerator->base.getAlgoName = GetAlgoName; returnGenerator->spiObj = spiObj; *returnObj = (HcfAsyKeyGenerator *)returnGenerator; return HCF_SUCCESS; }
这段代码是 HcfAsyKeyGeneratorCreate 函数的实现代码。该函数的作用是创建一个 HcfAsyKeyGenerator 实例。函数的第一个参数是 algoName,它表示加密算法的名称。第二个参数是 returnObj,它是一个指向 HcfAsyKeyGenerator 指针的指针,用于返回创建的 HcfAsyKeyGenerator 实例。
函数首先对传入的参数进行有效性检查,如果参数不合法则返回 HCF_INVALID_PARAMS 错误码。接下来,函数通过解析参数并设置参数来获取算法的相关信息。然后,函数查找并获取与算法相应的 HcfAsyKeyGeneratorSpiCreateFunc 函数。如果找不到相应的函数,则返回 HCF_NOT_SUPPORT 错误码。
接着,函数分配 HcfAsyKeyGeneratorImpl 内存,并将算法名称存储在其中。然后,函数调用 HcfAsyKeyGeneratorSpiCreateFunc 函数创建 HcfAsyKeyGeneratorSpi 实例,并将其存储在 HcfAsyKeyGeneratorImpl 中。如果创建失败,则返回错误码。
最后,函数设置 HcfAsyKeyGenerator 的函数指针,将 HcfAsyKeyGeneratorImpl 转换为 HcfAsyKeyGenerator,并将其存储在 *returnObj 中。函数返回 HCF_SUCCESS 表示成功创建了 HcfAsyKeyGenerator 实例。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)