1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16// [Start message_digest_calculation_segmented_digest_algorithm_sync] 17 18import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 19import { buffer } from '@kit.ArkTS'; 20 21function doLoopMdBySync() { 22 let mdAlgName = 'SHA256'; // 摘要算法名 23 let md = cryptoFramework.createMd(mdAlgName); 24 // 假设信息总共43字节,根据utf-8解码后,也是43字节 25 let messageText = 'aaaaa.....bbbbb.....ccccc.....ddddd.....eee'; 26 let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer); 27 let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 28 for (let i = 0; i < messageData.length; i += updateLength) { 29 let updateMessage = messageData.subarray(i, i + updateLength); 30 let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; 31 md.updateSync(updateMessageBlob); 32 } 33 let mdOutput = md.digestSync(); 34 console.info('[Sync]:md result: ' + mdOutput.data); 35 let mdLen = md.getMdLength(); 36 console.info('md len: ' + mdLen); 37} 38// [End message_digest_calculation_segmented_digest_algorithm_sync] 39@Entry 40@Component 41struct Index { 42 @State message: string = 'SegmentationSync'; 43 44 build() { 45 Column({ space: 12 }) { 46 Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) 47 Button($r('app.string.call_segmentation_sync')) 48 .width('70%') 49 .onClick(() => { 50 try { 51 doLoopMdBySync(); 52 this.message = 'SegmentationSyncSuccess'; 53 } catch { 54 this.message = 'SegmentationSyncFail'; 55 } 56 }) 57 } 58 .height('100%') 59 .width('100%') 60 } 61}