电子说
以生成DH密钥为例,生成随机密钥。具体的场景介绍及支持的算法规格
注意: 密钥别名中禁止包含个人数据等敏感信息。
开发前请熟悉鸿蒙开发指导文档 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
说明: 如果业务再次使用相同别名调用HUKS生成密钥,HUKS将生成新密钥并直接覆盖历史的密钥文件。
HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿
/* 以下以生成DH密钥为例 */
import { huks } from "@kit.UniversalKeystoreKit";
/* 1.确定密钥别名 */
let keyAlias = 'dh_key';
/* 2.初始化密钥属性集 */
let properties1: Array< huks.HuksParam > = [
{
tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
value: huks.HuksKeyAlg.HUKS_ALG_DH
},
{
tag: huks.HuksTag.HUKS_TAG_PURPOSE,
value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_AGREE
},
{
tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
value: huks.HuksKeySize.HUKS_DH_KEY_SIZE_2048
},
{
tag: huks.HuksTag.HUKS_TAG_DIGEST,
value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
}
];
let huksOptions: huks.HuksOptions = {
properties: properties1,
inData: new Uint8Array(new Array())
}
/* 3.生成密钥 */
function generateKeyItem(keyAlias: string, huksOptions: huks.HuksOptions){
return new Promise< void >((resolve, reject) = > {
try {
huks.generateKeyItem(keyAlias, huksOptions, (error, data) = > {
if (error) {
reject(error);
} else {
resolve(data);
}
});
} catch (error) {
throw (error as Error);
}
});
}
async function publicGenKeyFunc(keyAlias: string, huksOptions: huks.HuksOptions) {
console.info(`enter promise generateKeyItem`);
try {
await generateKeyItem(keyAlias, huksOptions)
.then((data) = > {
console.info(`promise: generateKeyItem success, data = ${JSON.stringify(data)}`);
})
.catch((error: Error) = > {
console.error(`promise: generateKeyItem failed, ${JSON.stringify(error)}`);
});
} catch (error) {
console.error(`promise: generateKeyItem input arg invalid` + JSON.stringify(error));
}
}
async function TestGenKey() {
await publicGenKeyFunc(keyAlias, huksOptions);
}
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !