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