HarmonyOS 鸿蒙Next如何获取X509证书公钥,并转化成string

发布于 1周前 作者 songsunli 来自 鸿蒙OS

HarmonyOS 鸿蒙Next如何获取X509证书公钥,并转化成string
找HarmonyOS工作还需要会Flutter技术的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:https://www.bilibili.com/video/BV1S4411E7LY/?p=17

关于HarmonyOS 鸿蒙Next如何获取X509证书公钥,并转化成string的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。

4 回复

参考代码如下:

function certSample(): void { 
  let textEncoder = new util.TextEncoder(); 
  let encodingBlob: certFramework.EncodingBlob = { 
    // 将证书数据从string类型转换成Unit8Array 
    data: textEncoder.encodeInto(certData), 
    // 证书格式,仅支持PEM和DER。在此示例中,证书为PEM格式 
    encodingFormat: certFramework.EncodingFormat.FORMAT_PEM 
  }; 

// 创建X509Cert实例 certFramework.createX509Cert(encodingBlob, (err, x509Cert) => { if (err != null) { // Failed to create the X509Cert instance. console.error(createX509Cert failed, errCode:${err.code}, errMsg:${err.message}); return; } // X509Cert实例创建成功 console.log(‘createX509Cert success’);

<span class="hljs-comment"><span class="hljs-comment">// 获取证书版本 </span></span>
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> version = x509Cert.getVersion(); 
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> serial = x509Cert.getCertSerialNumber(); 
console.log(`X509 version: ${version} , X509 serial:${serial}`); 

<span class="hljs-comment"><span class="hljs-comment">// 使用上级证书对象的getPublicKey()方法或本(自签名)证书对象获取公钥对象 </span></span>
<span class="hljs-keyword"><span class="hljs-keyword">try</span></span> { 
  <span class="hljs-keyword"><span class="hljs-keyword">let</span></span> pubKey: cryptoFramework.PubKey = x509Cert.getPublicKey(); <span class="hljs-comment"><span class="hljs-comment">//获取公钥 </span></span>
  <span class="hljs-keyword"><span class="hljs-keyword">let</span></span> pubk = uint8ArrayToHexStr(pubKey.getEncoded().data) 
  console.log(<span class="hljs-string"><span class="hljs-string">"test---"</span></span> + pubk) 
  <span class="hljs-comment"><span class="hljs-comment">// 验证证书签名 </span></span>
  x509Cert.verify(pubKey, (err, data) =&gt; { 
    <span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (err == <span class="hljs-literal"><span class="hljs-literal">null</span></span>) { 
      <span class="hljs-comment"><span class="hljs-comment">// 签名验证成功 </span></span>
      console.log(<span class="hljs-string"><span class="hljs-string">'verify success'</span></span>); 
    } <span class="hljs-keyword"><span class="hljs-keyword">else</span></span> { 
      <span class="hljs-comment"><span class="hljs-comment">// 签名验证失败 </span></span>
      console.error(`verify failed, errCode: ${err.code} , errMsg:${err.message}`); 
    } 
  }); 
} <span class="hljs-keyword"><span class="hljs-keyword">catch</span></span> (error) { 
  <span class="hljs-keyword"><span class="hljs-keyword">let</span></span> e: BusinessError = error as BusinessError; 
  console.error(`getPublicKey failed, errCode: ${e.code} , errMsg:${e.message}`); 
} 

<span class="hljs-comment"><span class="hljs-comment">// 用一个字符串代表时间 </span></span>
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> date = <span class="hljs-string"><span class="hljs-string">'20230930000001Z'</span></span>; 

<span class="hljs-comment"><span class="hljs-comment">// 验证证书的有效期 </span></span>
<span class="hljs-keyword"><span class="hljs-keyword">try</span></span> { 
  x509Cert.checkValidityWithDate(date); 
} <span class="hljs-keyword"><span class="hljs-keyword">catch</span></span> (error) { 
  <span class="hljs-keyword"><span class="hljs-keyword">let</span></span> e: BusinessError = error as BusinessError; 
  console.error(`checkValidityWithDate failed, errCode: ${e.code}, errMsg:${e.message}`); 
} 

}); } <button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

大佬,把导包的代码也发一下呗

api11 import { cert } from ‘@kit.DeviceCertificateKit’; import { cryptoFramework } from ‘@kit.CryptoArchitectureKit’;

如何把证书加入本地信任链中呢?
回到顶部