• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Converting SM2 Signature Formats (ArkTS)
2
3<!--Kit: Crypto Architecture Kit-->
4<!--Subsystem: Security-->
5<!--Owner: @zxz--3-->
6<!--Designer: @lanming-->
7<!--Tester: @PAFT-->
8<!--Adviser: @zengyawen-->
9
10HarmonyOS supports the conversion between the DER and R|S formats.
11You can specify SM2 signature parameters and convert them to DER ciphertext. You can also extract SM2 signature data from DER ciphertext.
12
13**Converting Ciphertext Parameters to DER Ciphertext**
141. Create an [EccSignatureSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#eccsignaturespec20) instance to specify SM2 ciphertext parameters.
15
162. Call [genEccSignature](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#geneccsignature20) to pass the **EccSignatureSpec** object to convert it to SM2 ciphertext in DER format.
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**Converting DER Ciphertext to the R and S Parameters**
401. Specifies the SM2 ciphertext parameters in DER format.
41
422. Call [genEccSignatureSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#geneccsignaturespec20) to pass data in DER format and convert the data into SM2 ciphertext in R|S format.
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