• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 specify_ecc_uncompressed_point_get_keypair]
17import { cryptoFramework } from '@kit.CryptoArchitectureKit';
18
19async function eccPointCompressedToPoint() {
20  let pkData =
21    new Uint8Array([2, 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]);
23  let returnPoint = cryptoFramework.ECCKeyUtil.convertPoint('NID_brainpoolP256r1', pkData);
24  console.info('convertPoint success');
25  let eccCommonParamsSpec = cryptoFramework.ECCKeyUtil.genECCCommonParamsSpec('NID_brainpoolP256r1');
26  let eccPubKeySpec: cryptoFramework.ECCPubKeySpec = {
27    algName: 'ECC',
28    specType: cryptoFramework.AsyKeySpecType.PUBLIC_KEY_SPEC,
29    params: eccCommonParamsSpec,
30    pk: returnPoint
31  };
32  let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(eccPubKeySpec);
33  let pubKey = await generatorBySpec.generatePubKey();
34  let returnData = cryptoFramework.ECCKeyUtil.getEncodedPoint('NID_brainpoolP256r1', returnPoint, 'UNCOMPRESSED');
35  console.info('returnData: ' +
36    returnData); // 4,143,39,57,249,145,50,63,222,35,70,178,121,202,154,21,146,129,75,76,63,8,195,157,111,
37  // 40,217,215,148,120,224,205,82,83,92,185,21,211,184,5,19,114,33,86,85,228,123,242,206,200,98,178,184,
38  // 130,35,232,45,5,202,189,11,46,163,156,152
39  let eccPkX = pubKey.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_PK_X_BN);
40  console.info('returnPoint x data: ' +
41  returnPoint.x); // 64750044510792891439269945828433327517677381559622384455951527515863444933970
42  console.info('ECC_PK_X_BN:' +
43    eccPkX); // 64750044510792891439269945828433327517677381559622384455951527515863444933970
44}
45// [End specify_ecc_uncompressed_point_get_keypair]
46
47@Entry
48@Component
49struct Index {
50  @State message: string = 'GetKeyObject';
51
52  build() {
53    Column({ space: 12 }) {
54      Text(this.message).fontSize(20).fontWeight(FontWeight.Bold)
55      Button($r('app.string.call_getKeyObject'))
56        .width('70%')
57        .onClick(async () => {
58          try {
59            await eccPointCompressedToPoint();
60            this.message = 'GetKeyObject Success';
61          } catch {
62            this.message = 'GetKeyObject Fail';
63          }
64        })
65    }
66    .height('100%')
67    .width('100%')
68  }
69}