1# Generating an MD Using SHA-256 (ArkTS) 2 3For details about the algorithm specifications, see [Supported Algorithms and Specifications](crypto-generate-message-digest-overview.md#supported-algorithms-and-specifications). 4 5> **NOTE** 6> 7> Since API version 12, wearable devices support MD operations. 8 9## How to Develop 10 11During 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. 12 13The following provides examples of MD operations with different data passing methods. 14 15 16### Generating an MD by Passing In Full Data 17 181. 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. 19 202. 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. 21 223. Call [Md.digest](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#digest) to generate an MD. 23 244. Call [Md.getMdLength](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getmdlength) to obtain the MD length, in bytes. 25 26- Example: Pass in the full data to generate an MD using **await**. 27 28 ```ts 29 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 30 import { buffer } from '@kit.ArkTS'; 31 32 async function doMd() { 33 let mdAlgName = "SHA256"; // Algorithm to use. 34 let message = 'mdTestMessage'; // Message to be digested. 35 let md = cryptoFramework.createMd(mdAlgName); 36 // 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. 37 await md.update({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }); 38 let mdResult = await md.digest(); 39 console.info('Md result:' + mdResult.data); 40 let mdLen = md.getMdLength(); 41 console.info("md len: " + mdLen); 42 } 43 ``` 44 45- Example: Pass in the full data to generate an MD using a synchronous API. 46 47 ```ts 48 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 49 import { buffer } from '@kit.ArkTS'; 50 51 function doMdBySync() { 52 let mdAlgName = "SHA256"; // Algorithm to use. 53 let message = 'mdTestMessage'; // Message to be digested. 54 let md = cryptoFramework.createMd(mdAlgName); 55 // 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. 56 md.updateSync({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }); 57 let mdResult = md.digestSync(); 58 console.info('[Sync]:Md result:' + mdResult.data); 59 let mdLen = md.getMdLength(); 60 console.info("md len: " + mdLen); 61 } 62 ``` 63 64### Generating an MD by Passing In Data by Segment 65 661. 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. 67 682. Call [Md.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-7) multiple times to pass in 20 bytes each time. 69 703. Call [Md.digest](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#digest-1) to generate an MD. 71 724. Call [Md.getMdLength](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getmdlength) to obtain the MD length, in bytes. 73 74- Example: Pass in data by segment to generate an MD using **await**. 75 76 ```ts 77 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 78 import { buffer } from '@kit.ArkTS'; 79 80 async function doLoopMd() { 81 let mdAlgName = "SHA256"; // Algorithm to use. 82 let md = cryptoFramework.createMd(mdAlgName); 83 // In this example, the message is of 43 bytes. After decoded in UTF-8 format, the message is also of 43 bytes. 84 let messageText = "aaaaa.....bbbbb.....ccccc.....ddddd.....eee"; 85 let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer); 86 let updateLength = 20; // Pass in 20 bytes each time. You can set this parameter as required. 87 for (let i = 0; i < messageData.length; i += updateLength) { 88 let updateMessage = messageData.subarray(i, i + updateLength); 89 let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; 90 await md.update(updateMessageBlob); 91 } 92 let mdOutput = await md.digest(); 93 console.info("md result: " + mdOutput.data); 94 let mdLen = md.getMdLength(); 95 console.info("md len: " + mdLen); 96 } 97 ``` 98 99- Example: Pass in data by segment to generate an MD using a synchronous API. 100 101 ```ts 102 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 103 import { buffer } from '@kit.ArkTS'; 104 105 function doLoopMdBySync() { 106 let mdAlgName = "SHA256"; // Algorithm to use. 107 let md = cryptoFramework.createMd(mdAlgName); 108 // In this example, the message is of 43 bytes. After decoded in UTF-8 format, the message is also of 43 bytes. 109 let messageText = "aaaaa.....bbbbb.....ccccc.....ddddd.....eee"; 110 let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer); 111 let updateLength = 20; // Pass in 20 bytes each time. You can set this parameter as required. 112 for (let i = 0; i < messageData.length; i += updateLength) { 113 let updateMessage = messageData.subarray(i, i + updateLength); 114 let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; 115 md.updateSync(updateMessageBlob); 116 } 117 let mdOutput = md.digestSync(); 118 console.info("[Sync]:md result: " + mdOutput.data); 119 let mdLen = md.getMdLength(); 120 console.info("md len: " + mdLen); 121 } 122 ``` 123