• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Generating an MD Using SHA-256 (ArkTS)
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> **NOTE**
13>
14> Since API version 12, wearable devices support MD operations.
15
16## How to Develop
17
18During 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.
19
20The following provides examples of MD operations with different data passing methods.
21
22### Generating an MD by Passing In Full Data
23
241. Call [cryptoFramework.createMd](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatemd) with the MD algorithm **SHA256** to create a message digest (**Md**) instance.
25
262. Call [Md.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-6) to pass in the full data. The data to be passed in by a single **update()** operation is not size bound.
27
283. Call [Md.digest](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#digest) to generate an MD.
29
304. Call [Md.getMdLength](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getmdlength) to obtain the MD length, in bytes.
31
32- Example: Pass in the full data to generate an MD using **await**.
33
34  ```ts
35  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
36  import { buffer } from '@kit.ArkTS';
37
38  async function doSha256() {
39    let mdAlgName = "SHA256"; // Algorithm to use.
40    let message = 'mdTestMessage'; // Message to be digested.
41    let md = cryptoFramework.createMd(mdAlgName);
42    // If there is a small amount of data to be processed, call update() to pass in all the data at a time. The amount of data to be passed in by a single update() call is not limited.
43    await md.update({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) });
44    let mdResult = await md.digest();
45    console.info('Md result:' + mdResult.data);
46    let mdLen = md.getMdLength();
47    console.info("md len: " + mdLen);
48  }
49  ```
50
51- Example: Pass in the full data to generate an MD using a synchronous API.
52
53  ```ts
54  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
55  import { buffer } from '@kit.ArkTS';
56
57  function doSha256BySync() {
58    let mdAlgName = "SHA256"; // Algorithm to use.
59    let message = 'mdTestMessage'; // Message to be digested.
60    let md = cryptoFramework.createMd(mdAlgName);
61    // If there is a small amount of data to be processed, call update() to pass in all the data at a time. The amount of data to be passed in by a single update() call is not limited.
62    md.updateSync({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) });
63    let mdResult = md.digestSync();
64    console.info('[Sync]:Md result:' + mdResult.data);
65    let mdLen = md.getMdLength();
66    console.info("md len: " + mdLen);
67  }
68  ```
69
70### Generating an MD by Passing In Data by Segment
71
721. Call [cryptoFramework.createMd](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatemd) with the MD algorithm **SHA256** to create a message digest (**Md**) instance.
73
742. Call [Md.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-7) multiple times to pass in 20 bytes each time.
75
763. Call [Md.digest](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#digest-1) to generate an MD.
77
784. Call [Md.getMdLength](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getmdlength) to obtain the MD length, in bytes.
79
80- Example: Pass in data by segment to generate an MD using **await**.
81
82  ```ts
83  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
84  import { buffer } from '@kit.ArkTS';
85
86  async function doLoopSha256() {
87    let mdAlgName = "SHA256"; // Algorithm to use.
88    let md = cryptoFramework.createMd(mdAlgName);
89    // In this example, the message is of 43 bytes. After decoded in UTF-8 format, the message is also of 43 bytes.
90    let messageText = "aaaaa.....bbbbb.....ccccc.....ddddd.....eee";
91    let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer);
92    let updateLength = 20; // Pass in 20 bytes each time. You can set this parameter as required.
93    for (let i = 0; i < messageData.length; i += updateLength) {
94      let updateMessage = messageData.subarray(i, i + updateLength);
95      let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
96      await md.update(updateMessageBlob);
97    }
98    let mdOutput = await md.digest();
99    console.info("md result: " + mdOutput.data);
100    let mdLen = md.getMdLength();
101    console.info("md len: " + mdLen);
102  }
103  ```
104
105- Example: Pass in data by segment to generate an MD using a synchronous API.
106
107  ```ts
108  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
109  import { buffer } from '@kit.ArkTS';
110
111  function doLoopSha256BySync() {
112    let mdAlgName = "SHA256"; // Algorithm to use.
113    let md = cryptoFramework.createMd(mdAlgName);
114    // In this example, the message is of 43 bytes. After decoded in UTF-8 format, the message is also of 43 bytes.
115    let messageText = "aaaaa.....bbbbb.....ccccc.....ddddd.....eee";
116    let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer);
117    let updateLength = 20; // Pass in 20 bytes each time. You can set this parameter as required.
118    for (let i = 0; i < messageData.length; i += updateLength) {
119      let updateMessage = messageData.subarray(i, i + updateLength);
120      let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
121      md.updateSync(updateMessageBlob);
122    }
123    let mdOutput = md.digestSync();
124    console.info("[Sync]:md result: " + mdOutput.data);
125    let mdLen = md.getMdLength();
126    console.info("md len: " + mdLen);
127  }
128  ```
129