1e41f4b71Sopenharmony_ci# MD Operation (ArkTS)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciThe message digest (MD) algorithm allows a fixed-length digest to be generated from data of arbitrary size by using the hash algorithm. The MD algorithm is also referred to as a hash algorithm or a one-way hash algorithm.
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciWhen the same digest algorithm is used, the generated digest (hash value) has the following features:
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci- The same message always results in the same hash value.
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci- The digest generated is of the fixed length no matter the length of messages. (The digest length is determined by the algorithm used).
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci- It is almost impossible to find two different messages with the same hash value. (The probability still exists, depending on the length of the digest.)
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## Supported Algorithms and Specifications
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciThe **Supported Type** column in the following table lists the algorithm to be used when a **Md** instance is created.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci| MD Algorithm | Supported Type | API Version | 
22e41f4b71Sopenharmony_ci| -------- | -------- | -------- |
23e41f4b71Sopenharmony_ci| HASH | SHA1 | 9+ | 
24e41f4b71Sopenharmony_ci| HASH | SHA224 | 9+ | 
25e41f4b71Sopenharmony_ci| HASH | SHA256 | 9+ | 
26e41f4b71Sopenharmony_ci| HASH | SHA384 | 9+ | 
27e41f4b71Sopenharmony_ci| HASH | SHA512 | 9+ | 
28e41f4b71Sopenharmony_ci| HASH | MD5 | 9+ | 
29e41f4b71Sopenharmony_ci| HASH | SM3 | 10+ | 
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci## How to Develop
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ciDuring the MD operation, you can use **update()** to 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.
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ciThe following provides examples of MD operations with different data passing methods.
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci### MD (Passing In Full Data)
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci1. Use [cryptoFramework.createMd](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatemd) with the MD algorithm **SHA256** to create a message digest (**Md**) instance.
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci2. Use [Md.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-6) to pass in the full data. The amount of data to be passed in by a single **update()** operation is not limited.
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci3. Use [Md.digest](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#digest) to generate an MD.
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci4. Use [Md.getMdLength](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getmdlength) to obtain the MD length, in bytes.
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci- Example: Pass in the full data to calculate an MD using **await**.
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci  ```ts
52e41f4b71Sopenharmony_ci  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
53e41f4b71Sopenharmony_ci  import { buffer } from '@kit.ArkTS';
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci  async function doMd() {
56e41f4b71Sopenharmony_ci    let mdAlgName = "SHA256"; // Algorithm to use.
57e41f4b71Sopenharmony_ci    let message = "mdTestMessgae"; // Message to be digested.
58e41f4b71Sopenharmony_ci    let md = cryptoFramework.createMd(mdAlgName);
59e41f4b71Sopenharmony_ci    // If the data to be processed is short, use update() to pass in the full data at a time. The amount of data to be passed in by a single **update()** operation is not limited.
60e41f4b71Sopenharmony_ci    await md.update({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) });
61e41f4b71Sopenharmony_ci    let mdResult = await md.digest();
62e41f4b71Sopenharmony_ci    console.info('Md result:' + mdResult.data);
63e41f4b71Sopenharmony_ci    let mdLen = md.getMdLength();
64e41f4b71Sopenharmony_ci    console.info("md len: " + mdLen);
65e41f4b71Sopenharmony_ci  }
66e41f4b71Sopenharmony_ci  ```
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci- Example: Pass in the full data to calculate an MD using a synchronous API.
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci  ```ts
71e41f4b71Sopenharmony_ci  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
72e41f4b71Sopenharmony_ci  import { buffer } from '@kit.ArkTS';
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci  function doMdBySync() {
75e41f4b71Sopenharmony_ci    let mdAlgName = "SHA256"; // Algorithm to use.
76e41f4b71Sopenharmony_ci    let message = "mdTestMessgae"; // Message to be digested.
77e41f4b71Sopenharmony_ci    let md = cryptoFramework.createMd(mdAlgName);
78e41f4b71Sopenharmony_ci    // If the data to be processed is short, use update() to pass in the full data at a time. The amount of data to be passed in by a single **update()** operation is not limited.
79e41f4b71Sopenharmony_ci    md.updateSync({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) });
80e41f4b71Sopenharmony_ci    let mdResult = md.digestSync();
81e41f4b71Sopenharmony_ci    console.info('[Sync]:Md result:' + mdResult.data);
82e41f4b71Sopenharmony_ci    let mdLen = md.getMdLength();
83e41f4b71Sopenharmony_ci    console.info("md len: " + mdLen);
84e41f4b71Sopenharmony_ci  }
85e41f4b71Sopenharmony_ci  ```
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci### MD (Passing In Data by Segment)
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci1. Use [cryptoFramework.createMd](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatemd) with the MD algorithm **SHA256** to create a message digest (**Md**) instance.
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci2. Call [Md.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-7) multiple times to pass in 20 bytes each time.
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci3. Use [Md.digest](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#digest-1) to generate an MD.
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci4. Use [Md.getMdLength](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getmdlength) to obtain the MD length, in bytes.
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci- Example: Pass in data by segment to calculate an MD using **await**.
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci  ```ts
100e41f4b71Sopenharmony_ci  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
101e41f4b71Sopenharmony_ci  import { buffer } from '@kit.ArkTS';
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci  async function doLoopMd() {
104e41f4b71Sopenharmony_ci    let mdAlgName = "SHA256"; // Algorithm to use.
105e41f4b71Sopenharmony_ci    let md = cryptoFramework.createMd(mdAlgName);
106e41f4b71Sopenharmony_ci    // In this example, the message is of 43 bytes. After decoded in UTF-8 format, the message is also of 43 bytes.
107e41f4b71Sopenharmony_ci    let messageText = "aaaaa.....bbbbb.....ccccc.....ddddd.....eee";
108e41f4b71Sopenharmony_ci    let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer);
109e41f4b71Sopenharmony_ci    let updateLength = 20; // Pass in 20 bytes each time. You can set this parameter as required.
110e41f4b71Sopenharmony_ci    for (let i = 0; i < messageData.length; i += updateLength) {
111e41f4b71Sopenharmony_ci      let updateMessage = messageData.subarray(i, i + updateLength);
112e41f4b71Sopenharmony_ci      let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
113e41f4b71Sopenharmony_ci      await md.update(updateMessageBlob);
114e41f4b71Sopenharmony_ci    }
115e41f4b71Sopenharmony_ci    let mdOutput = await md.digest();
116e41f4b71Sopenharmony_ci    console.info("md result: " + mdOutput.data);
117e41f4b71Sopenharmony_ci    let mdLen = md.getMdLength();
118e41f4b71Sopenharmony_ci    console.info("md len: " + mdLen);
119e41f4b71Sopenharmony_ci  }
120e41f4b71Sopenharmony_ci  ```
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci- Example: Pass in data by segment to calculate an MD using a synchronous API.
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci  ```ts
125e41f4b71Sopenharmony_ci  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
126e41f4b71Sopenharmony_ci  import { buffer } from '@kit.ArkTS';
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci  function doLoopMdBySync() {
129e41f4b71Sopenharmony_ci    let mdAlgName = "SHA256"; // Algorithm to use.
130e41f4b71Sopenharmony_ci    let md = cryptoFramework.createMd(mdAlgName);
131e41f4b71Sopenharmony_ci    // In this example, the message is of 43 bytes. After decoded in UTF-8 format, the message is also of 43 bytes.
132e41f4b71Sopenharmony_ci    let messageText = "aaaaa.....bbbbb.....ccccc.....ddddd.....eee";
133e41f4b71Sopenharmony_ci    let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer);
134e41f4b71Sopenharmony_ci    let updateLength = 20; // Pass in 20 bytes each time. You can set this parameter as required.
135e41f4b71Sopenharmony_ci    for (let i = 0; i < messageData.length; i += updateLength) {
136e41f4b71Sopenharmony_ci      let updateMessage = messageData.subarray(i, i + updateLength);
137e41f4b71Sopenharmony_ci      let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
138e41f4b71Sopenharmony_ci      md.updateSync(updateMessageBlob);
139e41f4b71Sopenharmony_ci    }
140e41f4b71Sopenharmony_ci    let mdOutput = md.digestSync();
141e41f4b71Sopenharmony_ci    console.info("[Sync]:md result: " + mdOutput.data);
142e41f4b71Sopenharmony_ci    let mdLen = md.getMdLength();
143e41f4b71Sopenharmony_ci    console.info("md len: " + mdLen);
144e41f4b71Sopenharmony_ci  }
145e41f4b71Sopenharmony_ci  ```
146