harmony 鸿蒙Signing and Signature Verification with an RSA Key Pair (PKCS1 Mode) (ArkTS)

  • 2025-06-12
  • 浏览 (75)

Signing and Signature Verification with an RSA Key Pair (PKCS1 Mode) (ArkTS)

For details about the algorithm specifications, see RSA.

Signing

  1. Call cryptoFramework.createAsyKeyGenerator and AsyKeyGenerator.generateKeyPair to generate a 1024-bit RSA key pair (KeyPair) with two primes. The KeyPair instance consists of a public key (PubKey) and a private key (PriKey).

In addition to the example in this topic, RSA and Randomly Generating an Asymmetric Key Pair may help you better understand how to generate an RSA asymmetric key pair. Note that the input parameters in the reference documents may be different from those in the example below.

  1. Call cryptoFramework.createSign with the string parameter ‘RSA1024|PKCS1|SHA256’ to create a Sign instance. The key type is RSA1024, the padding mode is PKCS1, and the MD algorithm is SHA256.

  2. Call Sign.init to initialize the Sign instance with the private key (PriKey).

  3. 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.

  • If a small amount of data is to be signed, call Sign.sign() immediately after Sign.init().

  • If a large amount of data is to be signed, call Sign.update() multiple times to pass in data by segment.

  • Call Sign.sign to generate a signature.

Signature Verification

  1. Call cryptoFramework.createVerify with the string parameter ‘RSA1024|PKCS1|SHA256’ to create a Verify instance. The string parameter must be the same as that used to create the Sign instance.

  2. Call Verify.init to initialize the Verify instance using the public key (PubKey).

  3. 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 call is not limited. You can determine how to pass in data based on the data volume.

    • If a small amount of data is to be verified, call Verify.verify() immediately after Verify.init().
    • If a large amount of data is to be verified, call Verify.update() multiple times to pass in data by segment.
  4. Call Verify.verify to verify the data signature.

  5. 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 = "RSA1024|PKCS1|SHA256";
    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 = "RSA1024|PKCS1|SHA256";
    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 = "RSA1024";
    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 = "RSA1024|PKCS1|SHA256";
    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 = "RSA1024|PKCS1|SHA256";
    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 = "RSA1024";
    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++)

0  赞