提交 26b2ae77 编写于 作者: S shuyi

API_format

Signed-off-by: Nshuyi <shuyi4@huawei.com>
上级 794474ac
...@@ -766,7 +766,7 @@ getKeyProperties(keyAlias: string, options: HuksOptions) : Promise\<HuksResult> ...@@ -766,7 +766,7 @@ getKeyProperties(keyAlias: string, options: HuksOptions) : Promise\<HuksResult>
| 类型 | 说明 | | 类型 | 说明 |
| ------------------ | ------------------------------------------------------------ | | ------------------ | ------------------------------------------------------------ |
| Promise\<[HuksResult](#huksoptions)> | errorCode:返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。properties:返回值为从生成key时传入的别名。中导出的生成密钥时所需参数。 | | Promise\<[HuksResult](#huksoptions)> | errorCode:返回HUKS_SUCCESS时表示接口使用成功,其他时为错误。properties:返回值为生成密钥时所需参数。 |
**示例:** **示例:**
...@@ -969,132 +969,206 @@ abort操作密钥接口,使用Callback回调异步返回结果 。 ...@@ -969,132 +969,206 @@ abort操作密钥接口,使用Callback回调异步返回结果 。
* 以下以RSA1024密钥的callback操作使用为例 * 以下以RSA1024密钥的callback操作使用为例
*/ */
import router from '@system.router'; import router from '@system.router';
import huks from "@ohos.security.huks"; import huks from '@ohos.security.huks';
import display from '@ohos.display';
async function routePage() {
export default { let options = {
data: { uri: 'pages/second'
title: "hmac", }
genHuksOptions: {}, try {
HuksOptions: { await router.push(options)
"properties": "", } catch (err) {
"inData": new Uint8Array() console.error(`fail callback, code: ${err.code}, msg: ${err.msg}`)
}, }
keyAlias: 'HuksDemoHMAC', }
inData: 'huksHmacTest', var alias = "HuksDemoRSA";
handle: {}, var properties = new Array();
}, var options = {
properties: properties,
onInit() { inData: new Uint8Array(0)
this.title = this.$t('strings.world'); };
}, var handle = {};
var resultMessage = "";
onCreate() { async function generateKey() {
console.info("Application onCreate"); properties[0] = {
}, tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
value: huks.HuksKeyAlg.HUKS_ALG_RSA
onDestroy() { };
console.info("Application onDestroy"); properties[1] = {
}, tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_1024
stringToUint8Array: function (str) { };
var arr = []; properties[2] = {
for (var i = 0, j = str.length; i < j; ++i) { tag: huks.HuksTag.HUKS_TAG_PURPOSE,
arr.push(str.charCodeAt(i)); value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT
} };
var tmpUint8Array = new Uint8Array(arr); properties[3] = {
return tmpUint8Array; tag: huks.HuksTag.HUKS_TAG_PADDING,
}, value: huks.HuksKeyPadding.HUKS_PADDING_OAEP
};
huksGenerateKey: function () { properties[4] = {
var alias = this.keyAlias; tag: huks.HuksTag.HUKS_TAG_DIGEST,
var properties = new Array(); value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
properties[0] = { };
tag: huks.HuksTag.HUKS_TAG_ALGORITHM, huks.generateKey(alias, options);
value: huks.HuksKeyAlg.HUKS_ALG_RSA }
}; function stringToUint8Array(str) {
properties[1] = { var arr = [];
tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, for (var i = 0, j = str.length; i < j; ++i) {
value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_1024 arr.push(str.charCodeAt(i));
}; }
properties[2] = { var tmpUint8Array = new Uint8Array(arr);
tag: huks.HuksTag.HUKS_TAG_PURPOSE, return tmpUint8Array;
value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT }
}; async function huksInit() {
properties[3] = { await huks.init(alias, options).then((data) => {
tag: huks.HuksTag.HUKS_TAG_PADDING, console.log(`test init data: ${JSON.stringify(data)}`);
value: huks.HuksKeyPadding.HUKS_PADDING_OAEP handle = {
}; "handle1": data.handle1,
properties[4] = { "handle2": data.handle2
tag: huks.HuksTag.HUKS_TAG_DIGEST, };
value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 }).catch((err) => {
}; console.log("test init err information: " + JSON.stringify(err))
var options = { })
properties: properties }
}; async function huksUpdate() {
this.HuksOptions.properties = properties; let count = 2;
this.genHuksOptions = options; for (let i = 0; i < count; i++) {
huks.generateKey(alias, options, function (err, data) { }); options.inData = stringToUint8Array("huksHmacTest");
}, await huks.update(handle, options).then((data) => {
if (data.errorCode === 0) {
async huksInit() { resultMessage += "update success!";
var alias = this.keyAlias; } else {
var that = this; resultMessage += "update fail!";
return new Promise((resolve, reject) => { }
huks.init(alias, this.genHuksOptions, async function (err, data) { }).catch((err) => {
console.log(`the init err is :${JSON.stringify(err)}`); resultMessage += "update times: " + count + (i + 1) + " fail catch errorMessage:" + JSON.stringify(err) + " "
console.log(`the init data is :${JSON.stringify(data)}`); });
if (data.errorCode === 0) { console.log(resultMessage);
that.resultMessage = "init 执行成功!" }
that.handle = { }
"handle1": data.handle1, function huksFinish() {
"handle2": data.handle2 options.inData = stringToUint8Array("HuksDemoHMAC");
} huks.finish(handle, options).then((data) => {
} else { if (data.errorCode === 0) {
that.resultMessage = "init 执行失败 errorCode: " + data.errorCode resultMessage = "finish success!";
} } else {
}); resultMessage = "finish fail errorCode: " + data.errorCode;
}); }
}, }).catch((err) => {
resultMessage = "finish fail, catch errorMessage:" + JSON.stringify(err)
async huksUpdate() { });
let count = 2; console.log(resultMessage);
for (let i = 0; i < count; i++) { }
this.HuksOptions.inData = this.stringToUint8Array(this.inData); async function huksAbort() {
console.log(`Huks_Demo hmac before update HuksOptions ${JSON.stringify(this.HuksOptions)}`); huks.abort(handle, options).then((data) => {
new Promise((resolve, reject) => { if (data.errorCode === 0) {
huks.update(this.handle, this.HuksOptions, function (err, data) { resultMessage = "abort success!";
if (data.errorCode === 0) { } else {
this.resultMessage += "update 共执行 " + count + "" + "" + (i + 1) + " 次执行成功! " resultMessage = "abort fail errorCode: " + data.errorCode;
} else { }
this.resultMessage += "update 共执行 " + count + "" + "" + (i + 1) + " 次执行失败 errorCode: " + data.errorCode + " " }).catch((err) => {
} resultMessage = "abort fail, catch errorMessage:" + JSON.stringify(err)
}); });
}); console.log(resultMessage);
} }
},
@Entry
huksFinish: function () { @Component
this.HuksOptions.inData = this.stringToUint8Array("0"); struct Index {
new Promise((resolve, reject) => { build() {
huks.finish(this.handle, this.HuksOptions, function (err, data) { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
if (data.errorCode === 0) { Text('Hello World')
that.resultMessage = "finish 执行成功!"; .fontSize(50)
} else { .fontWeight(FontWeight.Bold)
that.resultMessage = "finish 执行失败 errorCode: " + data.errorCode; Button() {
} Text('Tocallback')
}); .fontSize(25)
}); .fontWeight(FontWeight.Bold)
}, }.type(ButtonType.Capsule)
.margin({
huksAbort: function () { top: 20
new Promise((resolve, reject) => { })
huks.abort(this.handle, this.genHuksOptions, function (err, data) { .width('50%')
console.log(`Huks_Demo hmac huksAbort1 data ${JSON.stringify(data)}`); .height('10%')
console.log(`Huks_Demo hmac huksAbort1 err ${JSON.stringify(err)}`); .backgroundColor('#0D9FFB')
}); .onClick(() => {
}); routePage()
})
Button() {
Text('generateKey')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}.type(ButtonType.Capsule)
.margin({
top: 20
})
.width('50%')
.height('10%')
.backgroundColor('#0D9FFB')
.onClick(() => {
generateKey()
})
Button() {
Text('huksInit')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}.type(ButtonType.Capsule)
.margin({
top: 20
})
.width('50%')
.height('10%')
.backgroundColor('#0D9FFB')
.onClick(() => {
huksInit()
})
Button() {
Text('huksUpdate')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}.type(ButtonType.Capsule)
.margin({
top: 20
})
.width('50%')
.height('10%')
.backgroundColor('#0D9FFB')
.onClick(() => {
huksUpdate()
})
Button() {
Text('huksFinish')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}.type(ButtonType.Capsule)
.margin({
top: 20
})
.width('50%')
.height('10%')
.backgroundColor('#0D9FFB')
.onClick(() => {
huksFinish()
})
Button() {
Text('huksAbort')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}.type(ButtonType.Capsule)
.margin({
top: 20
})
.width('50%')
.height('10%')
.backgroundColor('#0D9FFB')
.onClick(() => {
huksAbort()
})
} }
.width('100%')
.height('100%')
}
} }
``` ```
...@@ -1123,129 +1197,209 @@ abort操作密钥接口,使用Promise方式异步返回结果。 ...@@ -1123,129 +1197,209 @@ abort操作密钥接口,使用Promise方式异步返回结果。
* 以下以RSA1024密钥的promise操作使用为例 * 以下以RSA1024密钥的promise操作使用为例
*/ */
import router from '@system.router'; import router from '@system.router';
import huks from "@ohos.security.huks"; import huks from '@ohos.security.huks';
import display from '@ohos.display';
async function routePage() {
export default { let options = {
data: { uri: 'pages/second'
title: "hmac", }
genHuksOptions: {}, try {
HuksOptions: { await router.push(options)
"properties": "", } catch (err) {
"inData": new Uint8Array() console.error(`fail callback, code: ${err.code}, msg: ${err.msg}`)
}, }
keyAlias: 'HuksDemoHMAC', }
inData: 'huksHmacTest',
handle: {}, var alias = "HuksDemoRSA";
}, var properties = new Array();
var options = {
onInit() { properties: properties,
this.title = this.$t('strings.world'); inData: new Uint8Array(0)
}, };
var handle = {};
onCreate() { var resultMessage = "";
console.info("Application onCreate"); function stringToUint8Array(str) {
}, var arr = [];
onDestroy() { for (var i = 0, j = str.length; i < j; ++i) {
console.info("Application onDestroy"); arr.push(str.charCodeAt(i));
}, }
var tmpUint8Array = new Uint8Array(arr);
stringToUint8Array: function (str) { return tmpUint8Array;
var arr = []; }
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i)); async function generateKey() {
properties[0] = {
tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
value: huks.HuksKeyAlg.HUKS_ALG_RSA
};
properties[1] = {
tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_1024
};
properties[2] = {
tag: huks.HuksTag.HUKS_TAG_PURPOSE,
value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT
};
properties[3] = {
tag: huks.HuksTag.HUKS_TAG_PADDING,
value: huks.HuksKeyPadding.HUKS_PADDING_OAEP
};
properties[4] = {
tag: huks.HuksTag.HUKS_TAG_DIGEST,
value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
};
huks.generateKey(alias, options, function (err, data) { });
}
async function huksInit() {
return new Promise((resolve, reject) => {
huks.init(alias, options, async function (err, data) {
if (data.errorCode === 0) {
resultMessage = "init success!"
handle = {
"handle1": data.handle1,
"handle2": data.handle2
} }
var tmpUint8Array = new Uint8Array(arr); } else {
return tmpUint8Array; resultMessage = "init fail errorCode: " + data.errorCode
}, }
});
huksGenerateKey: function () { });
console.log("huksGenerate2 is start!!!"); }
var alias = this.keyAlias;
var properties = new Array(); async function huksUpdate() {
properties[0] = { let count = 2;
tag: huks.HuksTag.HUKS_TAG_ALGORITHM, for (let i = 0; i < count; i++) {
value: huks.HuksKeyAlg.HUKS_ALG_RSA options.inData = stringToUint8Array("huksHmacTest");
}; new Promise((resolve, reject) => {
properties[1] = { huks.update(handle, options, function (err, data) {
tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, if (data.errorCode === 0) {
value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_1024 resultMessage += "update success!";
}; } else {
properties[2] = { resultMessage += "update fail!";
tag: huks.HuksTag.HUKS_TAG_PURPOSE,
value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT
};
properties[3] = {
tag: huks.HuksTag.HUKS_TAG_PADDING,
value: huks.HuksKeyPadding.HUKS_PADDING_OAEP
};
properties[4] = {
tag: huks.HuksTag.HUKS_TAG_DIGEST,
value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
};
var options = {
properties: properties
};
this.HuksOptions.properties = properties;
this.genHuksOptions = options;
var result = huks.generateKey(alias, this.genHuksOptions);
console.log("huksgenerate2 is ok!!!")
},
async huksInit() {
var alias = this.keyAlias;
var that = this;
await huks.init(alias, this.genHuksOptions).then((data) => {
console.log(`test init data: ${JSON.stringify(data)}`);
that.handle = {
"handle1": data.handle1,
"handle2": data.handle2
};
}).catch((err) => {
console.log("test init err information: " + JSON.stringify(err))
})
},
async huksUpdate() {
let count = 2;
for (let i = 0; i < count; i++) {
this.HuksOptions.inData = this.stringToUint8Array(this.inData);
await huks.update(this.handle, this.HuksOptions).then(async (data) => {
if (data.errorCode === 0) {
this.resultMessage += "update 共执行 " + count + "" + "" + (i + 1) + " 次执行成功! "
} else {
this.resultMessage += "update 共执行 " + count + "" + "" + (i + 1) + " 次执行失败 errorCode: " + data.errorCode + " "
}
}).catch((err) => {
this.resultMessage += "update 共执行 " + count + "" + "" + (i + 1) + " 次执行失败 catch 错误信息:" + JSON.stringify(err) + " "
});
} }
}, });
});
huksFinish: function () { console.log(resultMessage);
this.HuksOptions.inData = this.stringToUint8Array("HuksDemoHMAC"); }
huks.finish(this.handle, this.HuksOptions).then((data) => { }
if (data.errorCode === 0) {
this.resultMessage = "finish 执行成功!"; async function huksFinish() {
} else { options.inData = stringToUint8Array("0");
this.resultMessage = "finish 执行失败 errorCode: " + data.errorCode; new Promise((resolve, reject) => {
} huks.finish(handle, options, function (err, data) {
}).catch((err) => { if (data.errorCode === 0) {
this.resultMessage = "finish 执行失败, catch 错误信息:" + JSON.stringify(err) resultMessage = "finish success!";
}); } else {
}, resultMessage = "finish fail errorCode: " + data.errorCode;
}
huksAbort: function () { });
huks.abort(this.handle, this.HuksOptions).then((data) => { });
if (data.errorCode === 0) { }
this.resultMessage = "abort 执行成功!";
} else { function huksAbort() {
this.resultMessage = "abort 执行失败 errorCode: " + data.errorCode; new Promise((resolve, reject) => {
} huks.abort(handle, options, function (err, data) {
}).catch((err) => { console.log(`Huks_Demo hmac huksAbort1 data ${JSON.stringify(data)}`);
this.resultMessage = "abort 执行失败, catch 错误信息:" + JSON.stringify(err) console.log(`Huks_Demo hmac huksAbort1 err ${JSON.stringify(err)}`);
}); });
});
}
@Entry
@Component
struct Index {
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text('Hello World')
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button() {
Text('to Promise')
.fontSize(20)
.fontWeight(FontWeight.Bold)
}.type(ButtonType.Capsule)
.margin({
top: 20
})
.width('50%')
.height('10%')
.backgroundColor('#0D9FFB')
.onClick(() => {
router.back()
})
Button() {
Text('generateKey')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}.type(ButtonType.Capsule)
.margin({
top: 20
})
.width('50%')
.height('10%')
.backgroundColor('#0D9FFB')
.onClick(() => {
generateKey()
})
Button() {
Text('huksInit')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}.type(ButtonType.Capsule)
.margin({
top: 20
})
.width('50%')
.height('10%')
.backgroundColor('#0D9FFB')
.onClick(() => {
huksInit()
})
Button() {
Text('huksUpdate')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}.type(ButtonType.Capsule)
.margin({
top: 20
})
.width('50%')
.height('10%')
.backgroundColor('#0D9FFB')
.onClick(() => {
huksUpdate()
})
Button() {
Text('huksFinish')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}.type(ButtonType.Capsule)
.margin({
top: 20
})
.width('50%')
.height('10%')
.backgroundColor('#0D9FFB')
.onClick(() => {
huksFinish()
})
Button() {
Text('huksAbort')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}.type(ButtonType.Capsule)
.margin({
top: 20
})
.width('50%')
.height('10%')
.backgroundColor('#0D9FFB')
.onClick(() => {
huksAbort()
})
} }
.width('100%')
.height('100%')
}
} }
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册