openharmony 鸿蒙 crypto-generate-message-digest-sha3-ndk

2026-08-25 浏览 (1)

Generating an MD Using SHA3-256 (C/C++)

From API version 22, the crypto algorithm library supports digest operation with SHA3-256.

For details about the algorithm specifications, see Supported Algorithms and Specifications.

Adding the Dynamic Library in the CMake Script

target_link_libraries(entry PUBLIC libohcrypto.so)

How to Develop

During the MD operation, you can pass in all the data at a time or pass in data by segment. For the same piece of data, the result will be the same no matter how the data is passed. Use the appropriate method based on the data size.

The following provides examples with different data passing methods.

Generating an MD by Passing In Full Data

  1. Call OH_CryptoDigest_Create with the MD algorithm SHA3-256 to generate an MD operation instance (OH_CryptoDigest).

  2. Call OH_CryptoDigest_Update to pass in the data for generating an MD. The amount of data to be passed in by a single Mac.update() call is not limited.

  3. Call OH_CryptoDigest_Final to generate an MD.

  4. Call OH_CryptoDigest_GetLength to obtain the MD length, in bytes.

  5. Call OH_DigestCrypto_Destroy to destroy the OH_CryptoDigest instance.

The following describes how to obtain the digest calculation result when data is imported at a time.


#include "CryptoArchitectureKit/crypto_common.h"
#include "CryptoArchitectureKit/crypto_digest.h"
#include <cstring>

OH_Crypto_ErrCode doTestSha3Md()
{
    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("SHA3-256", &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;
}

Generating an MD by Passing In Data by Segment

  1. Call OH_CryptoDigest_Create with the MD algorithm SHA3-256 to generate an MD operation instance (OH_CryptoDigest).

  2. Call OH_CryptoDigest_Update multiple times to pass in 20 bytes each time.

  3. Call OH_CryptoDigest_Final to generate an MD.

  4. Call OH_CryptoDigest_GetLength to obtain the MD length, in bytes.

  5. Call OH_DigestCrypto_Destroy to destroy the OH_CryptoDigest instance.

Example


#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 doLoopSha3Md()
{
    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("SHA3-256", &ctx);
    if (ret != CRYPTO_SUCCESS) {
        free(testData);
        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);
    free(testData);
    return ret;
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 crypto-rsa-asym-encrypt-decrypt-pkcs1-ndk

openharmony 鸿蒙 crypto-key-agreement-using-ecdh

openharmony 鸿蒙 crypto-aes-sym-encrypt-decrypt-gcm-ndk

openharmony 鸿蒙 crypto-sm4-sym-encrypt-decrypt-gcm-ndk

openharmony 鸿蒙 crypto-sm2-sign-sig-verify-pkcs1-ndk

openharmony 鸿蒙 crypto-key-derivation-using-scrypt-ndk

openharmony 鸿蒙 crypto-sym-encrypt-decrypt-spec

openharmony 鸿蒙 crypto-rsa-sign-sig-verify-pkcs1-by-segment

openharmony 鸿蒙 crypto-sm4-sym-encrypt-decrypt-cbc

openharmony 鸿蒙 crypto-prikey-to-get-pubkey-ndk

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/JA53SRbw