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