Converting SM2 Signature Formats (ArkTS)
HarmonyOS supports the conversion between the DER and R|S formats.
You can specify SM2 signature parameters and convert them to DER ciphertext. You can also extract SM2 signature data from DER ciphertext.
Converting Ciphertext Parameters to DER Ciphertext
-
Create an EccSignatureSpec instance to specify SM2 ciphertext parameters.
-
Call genEccSignature to pass the EccSignatureSpec object to convert it to SM2 ciphertext in DER format.
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
function testSm2SignDataRsToDer() {
try {
let spec: cryptoFramework.EccSignatureSpec = {
r: BigInt('97726608965854271693043443511967021777934035174185659091642456228829830775155'),
s: BigInt('23084224202834231287427338597254751764391338275617140205467537273296855150376'),
};
let data = cryptoFramework.SignatureUtils.genEccSignature(spec);
console.info('genEccSignature result: success.');
console.info('data = ' + data);
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error(`ecc failed: errCode: ${e.code}, message: ${e.message}`);
}
}
Converting DER Ciphertext to the R and S Parameters
-
Specifies the SM2 ciphertext parameters in DER format.
-
Call genEccSignatureSpec to pass data in DER format and convert the data into SM2 ciphertext in R|S format.
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
function testSm2SignDataDerToRs() {
try {
let data =
new Uint8Array([48, 69, 2, 33, 0, 216, 15, 76, 238, 158, 165, 108, 76, 72, 63, 115, 52, 255, 51, 149, 54, 224,
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,
144, 57, 151, 195, 17, 31, 106, 123, 32, 139, 219, 6, 253, 62, 240, 181, 134, 214, 107, 27, 230, 175, 40]);
let spec: cryptoFramework.EccSignatureSpec = cryptoFramework.SignatureUtils.genEccSignatureSpec(data);
console.info('genEccSignatureSpec result: success.');
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error(`ecc failed: errCode: ${e.code}, message: ${e.message}`);
}
}
你可能感兴趣的鸿蒙文章
openharmony 鸿蒙 crypto-rsa-asym-encrypt-decrypt-pkcs1-ndk
openharmony 鸿蒙 crypto-key-agreement-using-ecdh
openharmony 鸿蒙 crypto-aes-sym-encrypt-decrypt-gcm-ndk
openharmony 鸿蒙 crypto-sm4-sym-encrypt-decrypt-gcm-ndk
openharmony 鸿蒙 crypto-sm2-sign-sig-verify-pkcs1-ndk
openharmony 鸿蒙 crypto-key-derivation-using-scrypt-ndk
openharmony 鸿蒙 crypto-sym-encrypt-decrypt-spec
openharmony 鸿蒙 crypto-rsa-sign-sig-verify-pkcs1-by-segment