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; }简化代码
时间: 2024-02-14 19:19:59 浏览: 125
这段代码的作用是根据输入的加密算法类型和密钥长度生成算法名称,并返回该名称的字符串。如果生成失败,会返回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;
}
```
阅读全文