For details about the algorithm specifications, see Supported Algorithms and Specifications.
Starting from API version 12, lightweight smart wearable devices support message digest computation and operations.
During the MD operation, you can 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.
The following provides examples of MD operations with different data passing methods.
Call cryptoFramework.createMd with the MD algorithm SHA256 to create a message digest (Md) instance.
Call Md.update to pass in the full data. The data to be passed in by a single update() operation is not size bound.
Call Md.digest to generate an MD.
Call Md.getMdLength to obtain the MD length, in bytes.
Example: Pass in the full data to generate an MD using await.
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { buffer } from '@kit.ArkTS';
async function doMd() {
try {
let mdAlgName = 'SHA256'; // Digest algorithm name.
let message = 'mdTestMessage'; // Data to be digested.
let md = cryptoFramework.createMd(mdAlgName);
// When the amount of data is small, you can call update only once to pass in all the data at once. The API does not limit the length of the input parameter.
await md.update({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) });
let mdResult = await md.digest();
console.info('Md result: ' + mdResult.data);
let mdLen = md.getMdLength();
console.info('md len: ' + mdLen);
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error(`call failed: errCode: ${e.code}, errMsg: ${e.message}`);
}
} Example: Pass in the full data to generate an MD using a synchronous API.
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { buffer } from '@kit.ArkTS';
function doMdBySync() {
try {
let mdAlgName = 'SHA256'; // Digest algorithm name.
let message = 'mdTestMessage'; // Data to be digested.
let md = cryptoFramework.createMd(mdAlgName);
// When the amount of data is small, you can call update only once to pass in all the data at once. The API does not limit the length of the input parameter.
md.updateSync({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) });
let mdResult = md.digestSync();
console.info('[Sync]:Md result: ' + mdResult.data);
let mdLen = md.getMdLength();
console.info('md len: ' + mdLen);
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error(`call failed: errCode: ${e.code}, errMsg: ${e.message}`);
}
} Call cryptoFramework.createMd with the MD algorithm SHA256 to create a message digest (Md) instance.
Call Md.update multiple times to pass in 20 bytes each time.
Call Md.digest to generate an MD.
Call Md.getMdLength to obtain the MD length, in bytes.
Example: Pass in data by segment to generate an MD using await.
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { buffer } from '@kit.ArkTS';
async function doLoopMd() {
try {
let mdAlgName = 'SHA256'; // digest algorithm name
let md = cryptoFramework.createMd(mdAlgName);
// Assume the message is 43 bytes in total, and remains 43 bytes after UTF-8 decoding.
let messageText = 'aaaaa.....bbbbb.....ccccc.....ddddd.....eee';
let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer);
let updateLength = 20; // Assume the data is passed in 20-byte segments for update; there is no actual requirement for this.
for (let i = 0; i < messageData.length; i += updateLength) {
let updateMessage = messageData.subarray(i, i + updateLength);
let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
await md.update(updateMessageBlob);
}
let mdOutput = await md.digest();
console.info('md result: ' + mdOutput.data);
let mdLen = md.getMdLength();
console.info('md len: ' + mdLen);
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error(`call failed: errCode: ${e.code}, errMsg: ${e.message}`);
}
} Example: Pass in data by segment to generate an MD using a synchronous API.
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { buffer } from '@kit.ArkTS';
function doLoopMdBySync() {
try {
let mdAlgName = 'SHA256'; // Digest algorithm name.
let md = cryptoFramework.createMd(mdAlgName);
// Assume the message is 43 bytes in total, and it is also 43 bytes after UTF-8 decoding.
let messageText = 'aaaaa.....bbbbb.....ccccc.....ddddd.....eee';
let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer);
let updateLength = 20; // Assume update is performed in 20-byte segments. There is no actual requirement for this.
for (let i = 0; i < messageData.length; i += updateLength) {
let updateMessage = messageData.subarray(i, i + updateLength);
let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
md.updateSync(updateMessageBlob);
}
let mdOutput = md.digestSync();
console.info('[Sync]:md result: ' + mdOutput.data);
let mdLen = md.getMdLength();
console.info('md len: ' + mdLen);
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error(`call failed: errCode: ${e.code}, errMsg: ${e.message}`);
}
}