关于私钥SHA256加签方法

原生iOS代码如下:

if (!privateKeyRef) { DLog(@"添加私钥失败"); return  nil; } 
 
NSData *plainTextBytes = [content dataUsingEncoding:NSUTF8StringEncoding]; 
NSData *signData = [self sha256WithRSA:plainTextBytes privateKey:privateKeyRef]; 
 
NSString *ret = [signData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; 
 
//干掉字符串中的\r和\n 
NSMutableString *mutString = [NSMutableString stringWithString:ret]; 
NSString *oneString = [mutString stringByReplacingOccurrencesOfString:@"\r" withString:@""]; 
NSString *twoString = [oneString stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
NSString *finalString = [NSString stringWithString:twoString]; 
return finalString;

想问下对应的HarmonyOS方法是什么?

HarmonyOS
6天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

RSA sha256 的加签验签参考代码:

import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 
import { buffer, util } from '@kit.ArkTS'; 
 
@Entry 
@Component 
struct CryptPage { 
  @State message: string = '点击开始'; 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            convertAsyKey() 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
export async function signMessagePromise(content: cryptoFramework.DataBlob, priKey: cryptoFramework.PriKey) { 
  let signAlg = "RSA1024PKCS1|SHA256"; // 签名算法规格 
  let signer = cryptoFramework.createSign(signAlg); 
  await signer.init(priKey); 
  let signData = await signer.sign(content); 
  return signData; 
} 
export async function verifyMessagePromise(content: cryptoFramework.DataBlob, signMessageBlob: cryptoFramework.DataBlob, pubKey: cryptoFramework.PubKey) { 
  let verifyAlg = "RSA1024|PKCS1|SHA256"; // 签名算法规格 
  let verifier = cryptoFramework.createVerify(verifyAlg); 
  await verifier.init(pubKey); 
  let res = await verifier.verify(content, signMessageBlob); 
  console.info("verify result is " + res); 
  return res; 
} 
export async function convertAsyKey() { 
  let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA2048'); 
  // 随机生成的公私钥对,Base64格式 
  let pubKeyVal = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAn7UujTR0CX1ujnNn70MtLlSESk/Y6fBsSw3s9/XqkuiXbGPUzkNbP0rldmTfzLEw/bhu43/v4mbeq18OAoIdpW/I4OhNSMhAO+c0Urjmqp0/p/0xEPO8r42g1OExaGdioLj9CUw18ODEG8nNhW/Py0NN002G87wJm/bYwG0OhwIDAQAB"; 
  let priKeyVal = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMCftS6NNHQJfW6Oc2fvQy0uVIRKT9jp8GxLDez39eqS6JdsY9TOQ1s/SuV2ZN/MsTD9uG7jf+/iZt6rXw4Cgh2lb8jg6E1IyEA75zRSuOaqnT+n/TEQ87yvjaDU4TFoZ2KguP0JTDXw4MQbyc2Fb8/LQ03TTYbzvAmb9tjAbQ6HAgMBAAECgYBV7GfztgU55anQETclm7MJimsk/8kSZ3hZtjkmBY3q71FVioIVwVSGdXtBVEi3FCXORr9op47AQOvGVAd4xmGndJSyNIKnZAMJ7k4J1vl7ENS2+t19dRJ9qdHwdIS0Au1iMiutfv9dezDKSb46cscAYVB94EOeZb3p+Xw/iaARQQJBAPbiLp6NbCglOCt4ErJwzIf7fSAgznsKl4zSNVkbFPcbojdNaSuNbqxzl2VohdeXiOa1DjprBicwVu6quYjLjXECQQDHvJpWEW1a5tQAUlZZ+C+fPq+IJ40+kdCGBYk8SyRGRQiyBClFXf4dpf6BlraM7HFX9t2itufEv9JY5WloN793AkEAoZTlaDYtPJUnjyO2MP2sj69kdHukSFllWPBXpCsabKS0ZbePhsa5UbsRsnhXaQQvwLdtSYS0cT+tFeW9Kq/2sQJBALpr3eDXAyoEp3plXxM6Abiv2UVLJRlxHx73q88KkyDC7gq4t4x23ebuN0zcTiXZyTBAyTxFWqzYHIu8E1WiMqECQBwr0pRDTXwamaJ+kevljztFPru/wDbxCPxwUsiPogVyIPv/Aoc/6L50+tBCveYqUoxLPJ6MmYtjabZkvvg+vEk=" 
  let pkBlob: cryptoFramework.DataBlob = { data: stringToUnit8Array(pubKeyVal) }; 
  let prBlob: cryptoFramework.DataBlob = { data: stringToUnit8Array(priKeyVal) }; 
  rsaGenerator.convertKey(pkBlob, prBlob, async (err, keyPair) => { 
    if (err) { 
      console.error(convertKey failed, ${err.code}, ${err.message}); 
      return; 
    } 
    console.info('convertKey success!'); 
    let plainText: string = "test"; 
    let res = await signMessagePromise({ data: new Uint8Array(buffer.from(plainText, 'utf-8').buffer) }, keyPair.priKey) 
    console.log("convertKey sign result:" + new util.Base64Helper().encodeToStringSync(res.data)) 
    let verifyResult = await verifyMessagePromise({ 
      data: new Uint8Array(buffer.from(plainText, 'utf-8').buffer) 
    }, res, keyPair.pubKey); 
    if (verifyResult == true) { 
      console.info('convertKey verify success'); 
    } else { 
      console.error('convertKey verify failed'); 
    } 
  }); 
} 
export function stringToUnit8Array(str: string): Uint8Array { 
  return new util.Base64Helper().decodeSync(str) 
}
分享
微博
QQ
微信
回复
6天前
相关问题
HarmonyOS HmacSHA1算法实现
533浏览 • 1回复 待解决
HarmonyOS SM2SM3摘要算法使用
249浏览 • 1回复 待解决
签名验算法不支持RSA|PKCS8|SHA1
109浏览 • 1回复 待解决
HarmonyOS 关于AOP具体使用方法
282浏览 • 1回复 待解决
如何获取HarmonyOS 项目的SHA1?
5070浏览 • 1回复 待解决
HarmonyOS 华为支付签名验
79浏览 • 1回复 待解决
Payment Kit的签名公私钥怎么获取?
1348浏览 • 1回复 待解决
RSA加密,使用自有私钥解密报错
112浏览 • 1回复 待解决
RSA签名后,后端验失败
83浏览 • 1回复 待解决
图片处理(动态水印)
232浏览 • 1回复 待解决