• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# SM2签名数据格式转换(ArkTS)
2
3<!--Kit: Crypto Architecture Kit-->
4<!--Subsystem: Security-->
5<!--Owner: @zxz--3-->
6<!--Designer: @lanming-->
7<!--Tester: @PAFT-->
8<!--Adviser: @zengyawen-->
9
10当前支持DER格式与(r、s)格式互转的能力。
11开发者可指定SM2签名数据,将其转换成DER格式密文。反之,也可以从DER格式密文中取出具体的SM2签名数据。
12
13**指定密文参数,转换为DER格式**
141. 构造[EccSignatureSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#eccsignaturespec20)对象,用于指定SM2密文参数。
15
162. 调用[genEccSignature](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#geneccsignature20),将EccSignatureSpec对象传入,转换为DER格式的SM2密文。
17
18```ts
19import { cryptoFramework } from '@kit.CryptoArchitectureKit';
20import { BusinessError } from '@kit.BasicServicesKit';
21
22function testGenEccSignature() {
23  try {
24    let spec: cryptoFramework.EccSignatureSpec = {
25      r: BigInt('97726608965854271693043443511967021777934035174185659091642456228829830775155'),
26      s: BigInt('23084224202834231287427338597254751764391338275617140205467537273296855150376'),
27    };
28
29    let data = cryptoFramework.SignatureUtils.genEccSignature(spec);
30    console.info('genEccSignature success');
31    console.info('data is ' + data);
32  } catch (err) {
33    let e: BusinessError = err as BusinessError;
34    console.error(`ecc error, ${e.code}, ${e.message}`);
35  }
36}
37```
38
39**指定DER格式,转换为(r、s)格式**
401. 指定DER格式的SM2密文参数。
41
422. 调用[genEccSignatureSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#geneccsignaturespec20),将DER格式数据传入,转换为(r、s)格式的SM2密文。
43
44```ts
45import { cryptoFramework } from '@kit.CryptoArchitectureKit';
46import { BusinessError } from '@kit.BasicServicesKit';
47
48function testGenEccSignatureSpec() {
49  try {
50    let data =
51      new Uint8Array([48, 69, 2, 33, 0, 216, 15, 76, 238, 158, 165, 108, 76, 72, 63, 115, 52, 255, 51, 149, 54, 224,
52        179, 49, 225, 70, 36, 117, 88, 154, 154, 27, 194, 161, 3, 1, 115, 2, 32, 51, 9, 53, 55, 248, 82, 7, 159, 179,
53        144, 57, 151, 195, 17, 31, 106, 123, 32, 139, 219, 6, 253, 62, 240, 181, 134, 214, 107, 27, 230, 175, 40]);
54    let spec: cryptoFramework.EccSignatureSpec = cryptoFramework.SignatureUtils.genEccSignatureSpec(data);
55    console.info('genEccSignatureSpec success');
56  } catch (err) {
57    let e: BusinessError = err as BusinessError;
58    console.error(`ecc error, ${e.code}, ${e.message}`);
59  }
60}
61```
62