消息摘要计算SHA256(C/C++)
对应的算法规格请查看消息摘要计算算法规格。
在CMake脚本中链接相关动态库
target_link_libraries(entry PUBLIC libohcrypto.so)
开发步骤
在调用update接口传入数据时,可以一次性传入所有数据,也可以把数据人工分段,然后分段update。对于同一段数据而言,计算结果没有差异。对于数据量较大的数据,开发者可以根据实际需求选择是否分段传入。
下面分别提供两种方式的示例代码。
摘要算法(一次性传入)
- 调用OH_CryptoDigest_Create,指定摘要算法SHA256,生成摘要实例(OH_CryptoDigest)。
- 调用OH_CryptoDigest_Update,传入自定义消息,进行摘要更新计算。单次update长度没有限制。
- 调用OH_CryptoDigest_Final,获取摘要计算结果。
- 调用OH_CryptoDigest_GetLength,获取摘要计算长度,单位为字节。
- 调用OH_DigestCrypto_Destroy,销毁摘要实例(OH_CryptoDigest)。
-
以下使用单次传入数据,获取摘要计算结果为例:
#include "CryptoArchitectureKit/crypto_common.h"#include "CryptoArchitectureKit/crypto_digest.h"#include <cstring>OH_Crypto_ErrCode doTestSha256Md(){OH_Crypto_ErrCode ret;OH_CryptoDigest *ctx = nullptr;char *testData = const_cast<char *>("0123456789");Crypto_DataBlob in = {.data = (uint8_t *)(testData), .len = strlen(testData)};Crypto_DataBlob out = {.data = nullptr, .len = 0};int mdLen = 0;ret = OH_CryptoDigest_Create("SHA256", &ctx);if (ret != CRYPTO_SUCCESS) {return ret;}do {ret = OH_CryptoDigest_Update(ctx, &in);if (ret != CRYPTO_SUCCESS) {break;}ret = OH_CryptoDigest_Final(ctx, &out);if (ret != CRYPTO_SUCCESS) {break;}mdLen = OH_CryptoDigest_GetLength(ctx);} while (0);OH_Crypto_FreeDataBlob(&out);OH_DigestCrypto_Destroy(ctx);return ret;}
分段摘要算法
- 调用OH_CryptoDigest_Create,指定摘要算法SHA256,生成摘要实例(OH_CryptoDigest)。
- 传入自定义消息,将一次传入数据量设置为20字节,多次调用OH_CryptoDigest_Update,进行摘要更新计算。
- 调用OH_CryptoDigest_Final,获取摘要计算结果。
- 调用OH_CryptoDigest_GetLength,获取摘要计算长度,单位为字节。
- 调用OH_DigestCrypto_Destroy,销毁摘要实例(OH_CryptoDigest)。
-
以下使用分段传入数据,获取摘要计算结果为例:
#include <cstdlib>#include "CryptoArchitectureKit/crypto_common.h"#include "CryptoArchitectureKit/crypto_digest.h"#define OH_CRYPTO_DIGEST_DATA_MAX (1024 * 1024 * 100)static constexpr int INT_640 = 640;OH_Crypto_ErrCode doLoopSha256Md(){OH_Crypto_ErrCode ret;OH_CryptoDigest *ctx = nullptr;uint8_t *testData = (uint8_t *)malloc(OH_CRYPTO_DIGEST_DATA_MAX);if (testData == nullptr) {return CRYPTO_MEMORY_ERROR;}Crypto_DataBlob out = {.data = nullptr, .len = 0};int mdLen = 0;int isBlockSize = 20;int offset = 0;ret = OH_CryptoDigest_Create("SHA256", &ctx);if (ret != CRYPTO_SUCCESS) {return ret;}do {for (int i = 0; i < INT_640 / isBlockSize; i++) {Crypto_DataBlob in = {.data = reinterpret_cast<uint8_t *>(testData + offset),.len = static_cast<size_t>(isBlockSize)};ret = OH_CryptoDigest_Update(ctx, &in);if (ret != CRYPTO_SUCCESS) {break;}offset += isBlockSize;}ret = OH_CryptoDigest_Final(ctx, &out);if (ret != CRYPTO_SUCCESS) {break;}mdLen = OH_CryptoDigest_GetLength(ctx);} while (0);OH_Crypto_FreeDataBlob(&out);OH_DigestCrypto_Destroy(ctx);return ret;}