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 convert_ecc_uncompressed_point] 17import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 18 19function eccPointUncompressedToCompressed() { 20 let pkData = 21 new Uint8Array([4, 143, 39, 57, 249, 145, 50, 63, 222, 35, 70, 178, 121, 202, 154, 21, 146, 129, 75, 76, 63, 8, 195, 22 157, 111, 40, 217, 215, 148, 120, 224, 205, 82, 83, 92, 185, 21, 211, 184, 5, 19, 114, 33, 86, 85, 228, 123, 242, 23 206, 200, 98, 178, 184, 130, 35, 232, 45, 5, 202, 189, 11, 46, 163, 156, 152]); 24 let returnPoint = cryptoFramework.ECCKeyUtil.convertPoint('NID_brainpoolP256r1', pkData); 25 console.info('convertPoint success'); 26 let returnData = cryptoFramework.ECCKeyUtil.getEncodedPoint('NID_brainpoolP256r1', returnPoint, 'COMPRESSED'); 27 console.info('returnData: ' + 28 returnData); // (因为y为偶数,所以压缩点数据的前缀是02)returnData: 2,143,39,57,249,145,50,63,222,35,70,178,121,202,154,21, 29 // 146,129,75,76,63,8,195,157,111,40,217,215,148,120,224,205,82 30} 31// [End convert_ecc_uncompressed_point] 32 33@Entry 34@Component 35struct Index { 36 @State message: string = 'CompressedPointData'; 37 38 build() { 39 Column({ space: 12 }) { 40 Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) 41 Button($r('app.string.call_compressedPointData')) 42 .width('70%') 43 .onClick(() => { 44 try { 45 eccPointUncompressedToCompressed(); 46 this.message = 'CompressedPointData Success'; 47 } catch { 48 this.message = 'CompressedPointData Fail'; 49 } 50 }) 51 } 52 .height('100%') 53 .width('100%') 54 } 55}