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// [Start message_digest_calculation_digest_algorithm_one_time_incoming_sync] 16import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 17import { buffer } from '@kit.ArkTS'; 18 19function doMdBySync() { 20 let mdAlgName = 'SHA256'; // 摘要算法名 21 let message = 'mdTestMessgae'; // 待摘要的数据 22 let md = cryptoFramework.createMd(mdAlgName); 23 // 数据量较少时,可以只做一次update,将数据全部传入,接口未对入参长度做限制 24 md.updateSync({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }); 25 let mdResult = md.digestSync(); 26 console.info('[Sync]:Md result:' + mdResult.data); 27 let mdLen = md.getMdLength(); 28 console.info('md len: ' + mdLen); 29} 30// [End message_digest_calculation_digest_algorithm_one_time_incoming_sync] 31@Entry 32@Component 33struct Index { 34 @State message: string = 'SingleTimeSync'; 35 36 build() { 37 Column({ space: 12 }) { 38 Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) 39 Button('Call SingleTimeSync') 40 .width('70%') 41 .onClick(() => { 42 try { 43 doMdBySync(); 44 this.message = 'SingleTimeSyncSuccess'; 45 } catch { 46 this.message = 'SingleTimeSyncFail'; 47 } 48 }) 49 } 50 .height('100%') 51 .width('100%') 52 } 53}