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

For details about the algorithm specifications, see RSA.

Signing

  1. Use 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.

  2. Use 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.

  3. Use Sign.init to initialize the Sign instance with the private key (PriKey).

  4. Set the data length to be passed in each time to 64 bytes, and call Sign.update multiple times to pass in the data to be signed. Currently, the data to be passed in by a single update() is not size bound. You can determine how to pass in data based on the data volume.

  5. Use Sign.sign to generate a signature.

Signature Verification

  1. Use 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. Use Verify.init to initialize the Verify instance using the public key (PubKey).

  3. Use Verify.update to pass in the data to be verified. Currently, the data to be passed in by a single update() is not size bound. You can determine how to pass in data based on the data volume.

  4. Use Verify.verify to verify the data signature.

import cryptoFramework from '@ohos.security.cryptoFramework';
import buffer from '@ohos.buffer';

async function signMessageBySegment(priKey: cryptoFramework.PriKey, plainText: Uint8Array) {
  let signAlg = "RSA1024|PKCS1|SHA256";
  let signer = cryptoFramework.createSign(signAlg);
  await signer.init(priKey);
  let textSplitLen = 64; // Set the length of the data to be passed in each time. In this example, the value is 64.
  for (let i = 0; i < plainText.length; i += textSplitLen) {
    let updateMessage = plainText.subarray(i, i + textSplitLen);
    let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
    // Call update() multiple times to pass in data by segment.
    await signer.update(updateMessageBlob);
  }
  // Pass in null here because all the plaintext has been passed in by segment.
  let signData = await signer.sign(null);
  return signData;
}
async function verifyMessagBySegment(pubKey: cryptoFramework.PubKey, plainText: Uint8Array, signMessageBlob: cryptoFramework.DataBlob) {
  let verifyAlg = "RSA1024|PKCS1|SHA256";
  let verifier = cryptoFramework.createVerify(verifyAlg);
  await verifier.init(pubKey);
  let textSplitLen = 64; // Set the length of the data to be passed in each time. In this example, the value is 64.
  for (let i = 0; i < plainText.length; i += textSplitLen) {
    let updateMessage = plainText.subarray(i, i + textSplitLen);
    let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
    // Call update() multiple times to pass in data by segment.
    await verifier.update(updateMessageBlob);
  }
  // Pass in null in the first parameter of verify() because all the plaintext has been passed in by segment.
  let res = await verifier.verify(null, signMessageBlob);
  console.info("verify result is " + res);
  return res;
}
async function rsaSignatureBySegment() {
  let message = "This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
    "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
    "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
    "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
    "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
    "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
    "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!" +
    "This is a long plainTest! This is a long plainTest! This is a long plainTest! This is a long plainTest!";
  let keyGenAlg = "RSA1024";
  let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg);
  let keyPair = await generator.generateKeyPair();
  let messageData = new Uint8Array(buffer.from(message, 'utf-8').buffer);
  let signData = await signMessageBySegment(keyPair.priKey, messageData);
  let verifyResult = await verifyMessagBySegment(keyPair.pubKey, messageData, signData);
  if (verifyResult == true) {
    console.info('verify success');
  } else {
    console.error('verify failed');
  }
}