HarmonyOS HarmonyOS签名验签问题

HarmonyOS 如何实现如下代码的签名验签,主要帮我们看看下面怎么实现呢

const sig = new jsrsasign.Signature({
  alg: 'MD5withRSA',
});
sig.init(key);
sig.updateString(md5);
return jsrsasign.hex2b64(sig.sign());
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

可以参考这个demo:

import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer, JSON, util } from '@kit.ArkTS';

@Entry
@Component
struct SignRSADemo {
  @State message: string = 'Hello World';

  aboutToAppear(): void {
    main()
  }

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('SignRSADemoHelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
    }
    .height('100%')
    .width('100%')
  }
}
// 完整的明文被拆分为input1和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|MD5";
  let signer = cryptoFramework.createSign(signAlg);
  await signer.init(priKey);
  await signer.update(input1); // 如果明文较短,可以直接调用sign接口一次性传入
  let signData = await signer.sign(input2);
  return signData;
}
async function verifyMessagePromise(signMessageBlob: cryptoFramework.DataBlob, pubKey: cryptoFramework.PubKey) {
  let verifyAlg = "RSA1024|PKCS1|MD5";
  let verifier = cryptoFramework.createVerify(verifyAlg);
  await verifier.init(pubKey);
  await verifier.update(input1); // 如果明文较短,可以直接调用verify接口一次性传入
  let res = await verifier.verify(input2, signMessageBlob);
  console.info("verify result is " + res);
  return res;
}
async function main() {
  try {
    let Base64 = new util.Base64Helper()
    let pubKey = Base64.decodeSync("此处为的公钥")
    let priKey = Base64.decodeSync("此处为的私钥")
    console.log("a == >" + pubKey)
    console.log("b == >" + priKey)
    let keyPair = await genKeyPairByData(pubKey, priKey)
    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');
    }
  } catch (e) {
    console.log(JSON.stringify(e))
  }
}
// 生成RSA密钥对
async function genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) {
  let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData };
  let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData };
  let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024');
  let keyPair = await rsaGenerator.convertKey(pubKeyBlob, priKeyBlob);
  console.info('convertKey success');
  return keyPair;
}

// 字节流转成可理解的字符串
function uint8ArrayToString(array: Uint8Array) {
  // 将UTF-8编码转换成Unicode编码
  let out: string = "";
  let index: number = 0;
  let len: number = array.length;
  while (index < len) {
    let character = array[index++];
    switch (character >> 4) {
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
        out += String.fromCharCode(character);
        break;
      case 12:
      case 13:
        out += String.fromCharCode(((character & 0x1F) << 6) | (array[index++] & 0x3F));
        break;
      case 14:
        out += String.fromCharCode(((character & 0x0F) << 12) | ((array[index++] & 0x3F) << 6) |
          ((array[index++] & 0x3F) << 0));
        break;
      default:
        break;
    }
  }
  return out;
}
分享
微博
QQ
微信
回复
22h前
相关问题
HarmonyOS 华为支付签名
232浏览 • 1回复 待解决
HarmonyOS RSA签名相关
0浏览 • 0回复 待解决
HarmonyOS 接口参数的RSA签名+
5浏览 • 0回复 待解决
RSA签名后,后端失败
201浏览 • 1回复 待解决
huks sm2签名失败
249浏览 • 1回复 待解决
HarmonyOS SM3
9浏览 • 0回复 待解决
HarmonyOS sm2失败
65浏览 • 1回复 待解决
签名算法不支持RSA|PKCS8|SHA1
267浏览 • 1回复 待解决
HarmonyOS SM2加SM3摘要算法使用
511浏览 • 1回复 待解决
4.1.0(11) fluterr 无法通过
951浏览 • 1回复 待解决
huks ECC指纹认证签名签报错
1457浏览 • 1回复 待解决
HarmonyOS应用签名问题
399浏览 • 1回复 待解决
HarmonyOS 签名固定问题
172浏览 • 1回复 待解决
HarmonyOS APP上架签名问题
327浏览 • 1回复 待解决