harmony 鸿蒙Signing and Signature Verification with an SM2 Key Pair (ArkTS)
Signing and Signature Verification with an SM2 Key Pair (ArkTS)
For details about the algorithm specifications, see SM2.
Signing
- Call cryptoFramework.createAsyKeyGenerator and AsyKeyGenerator.generateKeyPair to generate a 256-bit key pair (KeyPair) using SM2.
In addition to the example in this topic, SM2 and Randomly Generating an Asymmetric Key Pair may help you better understand how to generate an SM2 asymmetric key pair. Note that the input parameters in the reference documents may be different from those in the example below.
Call cryptoFramework.createSign with the string parameter ‘SM2_256|SM3’ to create a Sign instance. The key type is SM2_256, and the MD algorithm is SM3.
Call Sign.init to initialize the Sign instance with the private key (PriKey).
Call Sign.update to pass in the data to be signed.
Currently, the amount of data to be passed in by a single Sign.update() is not limited. You can determine how to pass in data based on the data volume.Call Sign.sign to generate a signature.
Signature Verification
Call cryptoFramework.createVerify with the string parameter ‘SM2_256|SM3’ to create a Verify instance. The key type is SM2_256, and MD algorithm is SM3.
Call Verify.init to initialize the Verify instance using the public key (PubKey).
Call Verify.update to pass in the data to be verified.
Currently, the amount of data to be passed in by a single Verify.update() is not limited. You can determine how to pass in data based on the data volume.Call Verify.verify to verify the data signature.
Example (using asynchronous APIs):
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer } from '@kit.ArkTS';
// The plaintext is split into input1 and input2.
let input1: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) };
let input2: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) };
async function signMessagePromise(priKey: cryptoFramework.PriKey) {
let signAlg = "SM2_256|SM3";
let signer = cryptoFramework.createSign(signAlg);
await signer.init(priKey);
await signer.update(input1); // If the plaintext is short, you can use sign() to pass in the full data at a time.
let signData = await signer.sign(input2);
return signData;
}
async function verifyMessagePromise(signMessageBlob: cryptoFramework.DataBlob, pubKey: cryptoFramework.PubKey) {
let verifyAlg = "SM2_256|SM3";
let verifier = cryptoFramework.createVerify(verifyAlg);
await verifier.init(pubKey);
await verifier.update(input1); // If the plaintext is short, you can use verify() to pass in the full data at a time.
let res = await verifier.verify(input2, signMessageBlob);
console.info("verify result is " + res);
return res;
}
async function main() {
let keyGenAlg = "SM2_256";
let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg);
let keyPair = await generator.generateKeyPair();
let signData = await signMessagePromise(keyPair.priKey);
let verifyResult = await verifyMessagePromise(signData, keyPair.pubKey);
if (verifyResult === true) {
console.info('verify success');
} else {
console.error('verify failed');
}
}
- Example (using synchronous APIs):
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer } from '@kit.ArkTS';
// The plaintext is split into input1 and input2.
let input1: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) };
let input2: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) };
function signMessagePromise(priKey: cryptoFramework.PriKey) {
let signAlg = "SM2_256|SM3";
let signer = cryptoFramework.createSign(signAlg);
signer.initSync(priKey);
signer.updateSync(input1); // If the plaintext is short, you can use sign() to pass in the full data at a time.
let signData = signer.signSync(input2);
return signData;
}
function verifyMessagePromise(signMessageBlob: cryptoFramework.DataBlob, pubKey: cryptoFramework.PubKey) {
let verifyAlg = "SM2_256|SM3";
let verifier = cryptoFramework.createVerify(verifyAlg);
verifier.initSync(pubKey);
verifier.updateSync(input1); // If the plaintext is short, you can use verify() to pass in the full data at a time.
let res = verifier.verifySync(input2, signMessageBlob);
console.info("verify result is " + res);
return res;
}
function main() {
let keyGenAlg = "SM2_256";
let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg);
let keyPair = generator.generateKeyPairSync();
let signData = signMessagePromise(keyPair.priKey);
let verifyResult = verifyMessagePromise(signData, keyPair.pubKey);
if (verifyResult === true) {
console.info('verify success');
} else {
console.error('verify failed');
}
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Crypto Architecture Kit
harmony 鸿蒙Encryption and Decryption with a 3DES Symmetric Key (ECB Mode) (C/C++)
harmony 鸿蒙Encryption and Decryption with a 3DES Symmetric Key (ECB Mode) (ArkTS)
harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CBC Mode) (C/C++)
harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CBC Mode) (ArkTS)
harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CCM Mode) (C/C++)
harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CCM Mode) (ArkTS)
harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (ECB Mode) (C/C++)
harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (ECB Mode) (ArkTS)
harmony 鸿蒙Encryption and Decryption by Segment with an AES Symmetric Key (GCM Mode) (C/C++)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
7、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦