讲解代码 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; }
时间: 2024-04-28 11:20:50 浏览: 157
这段代码是 HcfAsyKeyGeneratorCreate 函数的实现代码。该函数的作用是创建一个 HcfAsyKeyGenerator 实例。函数的第一个参数是 algoName,它表示加密算法的名称。第二个参数是 returnObj,它是一个指向 HcfAsyKeyGenerator 指针的指针,用于返回创建的 HcfAsyKeyGenerator 实例。
函数首先对传入的参数进行有效性检查,如果参数不合法则返回 HCF_INVALID_PARAMS 错误码。接下来,函数通过解析参数并设置参数来获取算法的相关信息。然后,函数查找并获取与算法相应的 HcfAsyKeyGeneratorSpiCreateFunc 函数。如果找不到相应的函数,则返回 HCF_NOT_SUPPORT 错误码。
接着,函数分配 HcfAsyKeyGeneratorImpl 内存,并将算法名称存储在其中。然后,函数调用 HcfAsyKeyGeneratorSpiCreateFunc 函数创建 HcfAsyKeyGeneratorSpi 实例,并将其存储在 HcfAsyKeyGeneratorImpl 中。如果创建失败,则返回错误码。
最后,函数设置 HcfAsyKeyGenerator 的函数指针,将 HcfAsyKeyGeneratorImpl 转换为 HcfAsyKeyGenerator,并将其存储在 *returnObj 中。函数返回 HCF_SUCCESS 表示成功创建了 HcfAsyKeyGenerator 实例。
相关问题
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对象并返回错误码。
static char *GetAlgoName(HcfSymKeyGeneratorSpiOpensslImpl *impl) { char keySizeChar[MAX_KEY_STR_SIZE] = { 0 }; if (sprintf_s(keySizeChar, MAX_KEY_STR_SIZE, "%d", impl->attr.keySize) < 0) { LOGE("Invalid input parameter!"); return NULL; } char *algoName = (char *)HcfMalloc(MAX_KEY_STR_SIZE, 0); if (algoName == NULL) { LOGE("algoName malloc failed!"); return NULL; } HcfAlgValue type = impl->attr.algo; if (type == HCF_ALG_AES) { int32_t aesSize = strlen(AES_ALG_NAME); if (strcpy_s(algoName, MAX_KEY_STR_SIZE, AES_ALG_NAME) != EOK) { LOGE("aes algoName strcpy_s failed!"); goto clearup; } if (strcpy_s(algoName + aesSize, MAX_KEY_STR_SIZE - aesSize, keySizeChar) != EOK) { LOGE("aes algoName size strcpy_s failed!"); goto clearup; } } else if (type == HCF_ALG_SM4) { int32_t sm4Size = strlen(SM4_ALG_NAME); if (strcpy_s(algoName, MAX_KEY_STR_SIZE, SM4_ALG_NAME) != EOK) { LOGE("sm4 algoName strcpy_s failed!"); goto clearup; } if (strcpy_s(algoName + sm4Size, MAX_KEY_STR_SIZE - sm4Size, keySizeChar) != EOK) { LOGE("sm4 algoName size strcpy_s failed!"); goto clearup; } } else if (type == HCF_ALG_DES) { int32_t desSize = strlen(DES_ALG_NAME); if (strcpy_s(algoName, MAX_KEY_STR_SIZE, DES_ALG_NAME) != EOK) { LOGE("des algoName strcpy_s failed!"); goto clearup; } if (strcpy_s(algoName + desSize, MAX_KEY_STR_SIZE - desSize, keySizeChar) != EOK) { LOGE("des algoName size strcpy_s failed!"); goto clearup; } } else { LOGE("unsupport algo!"); goto clearup; } return algoName; clearup: HcfFree(algoName); return NULL; }简化代码
这段代码的作用是根据输入的加密算法类型和密钥长度生成算法名称,并返回该名称的字符串。如果生成失败,会返回NULL。
简化后的代码如下:
```
static char *GetAlgoName(HcfSymKeyGeneratorSpiOpensslImpl *impl) {
char keySizeChar[MAX_KEY_STR_SIZE] = { 0 };
if (sprintf_s(keySizeChar, MAX_KEY_STR_SIZE, "%d", impl->attr.keySize) < 0) {
return NULL;
}
char *algoName = (char *)HcfMalloc(MAX_KEY_STR_SIZE, 0);
if (algoName == NULL) {
return NULL;
}
HcfAlgValue type = impl->attr.algo;
int32_t algSize;
const char *algName;
if (type == HCF_ALG_AES) {
algName = AES_ALG_NAME;
} else if (type == HCF_ALG_SM4) {
algName = SM4_ALG_NAME;
} else if (type == HCF_ALG_DES) {
algName = DES_ALG_NAME;
} else {
HcfFree(algoName);
return NULL;
}
algSize = strlen(algName);
if (strcpy_s(algoName, MAX_KEY_STR_SIZE, algName) != EOK) {
HcfFree(algoName);
return NULL;
}
if (strcpy_s(algoName + algSize, MAX_KEY_STR_SIZE - algSize, keySizeChar) != EOK) {
HcfFree(algoName);
return NULL;
}
return algoName;
}
```
阅读全文