• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Generating an MD Using SHA-256 (C/C++)
2
3<!--Kit: Crypto Architecture Kit-->
4<!--Subsystem: Security-->
5<!--Owner: @zxz--3-->
6<!--Designer: @lanming-->
7<!--Tester: @PAFT-->
8<!--Adviser: @zengyawen-->
9
10For details about the algorithm specifications, see [Supported Algorithms and Specifications](crypto-generate-message-digest-overview.md#supported-algorithms-and-specifications).
11
12## Adding the Dynamic Library in the CMake Script
13```txt
14target_link_libraries(entry PUBLIC libohcrypto.so)
15```
16
17## How to Develop
18
19During the MD operation, you can [pass in all the data at a time](#generating-an-md-by-passing-in-full-data) or [pass in data by segment](#generating-an-md-by-passing-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.
20
21The following provides examples of MD operations with different data passing methods.
22
23### Generating an MD by Passing In Full Data
24
251. Call [OH_CryptoDigest_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-digest-h.md#oh_cryptodigest_create) with the MD algorithm **SHA256** to generate an MD operation instance (**OH_CryptoDigest**).
26
272. Call [OH_CryptoDigest_Update](../../reference/apis-crypto-architecture-kit/capi-crypto-digest-h.md#oh_cryptodigest_update) to pass in the data for generating an MD. The amount of data to be passed in by a single **OH_CryptoDigest_Update()** operation is not limited.
28
293. Call [OH_CryptoDigest_Final](../../reference/apis-crypto-architecture-kit/capi-crypto-digest-h.md#oh_cryptodigest_final) to generate an MD.
30
314. Call [OH_CryptoDigest_GetLength](../../reference/apis-crypto-architecture-kit/capi-crypto-digest-h.md#oh_cryptodigest_getlength) to obtain the MD length, in bytes.
32
335. Call [OH_DigestCrypto_Destroy](../../reference/apis-crypto-architecture-kit/capi-crypto-digest-h.md#oh_digestcrypto_destroy) to destroy the **OH_CryptoDigest** instance.
34
35- **Example**
36
37```c++
38#include "CryptoArchitectureKit/crypto_common.h"
39#include "CryptoArchitectureKit/crypto_digest.h"
40#include <string.h>
41
42static OH_Crypto_ErrCode doTestMd()
43{
44    OH_Crypto_ErrCode ret;
45    OH_CryptoDigest *ctx = nullptr;
46    char *testData = const_cast<char *>("0123456789");
47    Crypto_DataBlob in = {.data = (uint8_t *)(testData), .len = strlen(testData)};
48    Crypto_DataBlob out = {.data = nullptr, .len = 0};
49    int mdLen = 0;
50    ret = OH_CryptoDigest_Create("SHA256", &ctx);
51    if (ret != CRYPTO_SUCCESS) {
52        return ret;
53    }
54    do {
55        ret = OH_CryptoDigest_Update(ctx, &in);
56        if (ret != CRYPTO_SUCCESS) {
57            break;
58        }
59        ret = OH_CryptoDigest_Final(ctx, &out);
60        if (ret != CRYPTO_SUCCESS) {
61            break;
62        }
63        mdLen = OH_CryptoDigest_GetLength(ctx);
64    } while (0);
65    OH_Crypto_FreeDataBlob(&out);
66    OH_DigestCrypto_Destroy(ctx);
67    return ret;
68}
69```
70
71### Generating an MD by Passing In Data by Segment
72
731. Call [OH_CryptoDigest_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-digest-h.md#oh_cryptodigest_create) with the MD algorithm **SHA256** to generate an MD operation instance (**OH_CryptoDigest**).
74
752. Call [OH_CryptoDigest_Update](../../reference/apis-crypto-architecture-kit/capi-crypto-digest-h.md#oh_cryptodigest_update) multiple times to pass in 20 bytes each time.
76
773. Call [OH_CryptoDigest_Final](../../reference/apis-crypto-architecture-kit/capi-crypto-digest-h.md#oh_cryptodigest_final) to generate an MD.
78
794. Call [OH_CryptoDigest_GetLength](../../reference/apis-crypto-architecture-kit/capi-crypto-digest-h.md#oh_cryptodigest_getlength) to obtain the MD length, in bytes.
80
815. Call [OH_DigestCrypto_Destroy](../../reference/apis-crypto-architecture-kit/capi-crypto-digest-h.md#oh_digestcrypto_destroy) to destroy the **OH_CryptoDigest** instance.
82
83- **Example**
84
85```c++
86#include <stdlib.h>
87#include "CryptoArchitectureKit/crypto_common.h"
88#include "CryptoArchitectureKit/crypto_digest.h"
89#define OH_CRYPTO_DIGEST_DATA_MAX (1024 * 1024 * 100)
90
91static OH_Crypto_ErrCode doLoopMd()
92{
93    OH_Crypto_ErrCode ret;
94    OH_CryptoDigest *ctx = nullptr;
95    uint8_t *testData = (uint8_t *)malloc(OH_CRYPTO_DIGEST_DATA_MAX);
96    if (testData == nullptr) {
97        return CRYPTO_MEMORY_ERROR;
98    }
99    Crypto_DataBlob out = {.data = nullptr, .len = 0};
100    int mdLen = 0;
101    int isBlockSize = 20;
102    int offset = 0;
103
104    ret = OH_CryptoDigest_Create("SHA256", &ctx);
105    if (ret != CRYPTO_SUCCESS) {
106        return ret;
107    }
108    do {
109        for (int i = 0; i < 640 / isBlockSize; i++) {
110            Crypto_DataBlob in = {.data = reinterpret_cast<uint8_t *>(testData + offset),
111                                .len = static_cast<size_t>(isBlockSize)};
112            ret = OH_CryptoDigest_Update(ctx, &in);
113            if (ret != CRYPTO_SUCCESS) {
114                break;
115            }
116            offset += isBlockSize;
117        }
118        ret = OH_CryptoDigest_Final(ctx, &out);
119        if (ret != CRYPTO_SUCCESS) {
120            break;
121        }
122        mdLen = OH_CryptoDigest_GetLength(ctx);
123    } while (0);
124    OH_Crypto_FreeDataBlob(&out);
125    OH_DigestCrypto_Destroy(ctx);
126    free(testData);
127    return ret;
128}
129```
130