未验证 提交 45185452 编写于 作者: O openharmony_ci 提交者: Gitee

!3430 API格式整改为ETS

Merge pull request !3430 from shuyi/myfeature
...@@ -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,46 +969,27 @@ abort操作密钥接口,使用Callback回调异步返回结果 。 ...@@ -969,46 +969,27 @@ 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';
export default {
data: {
title: "hmac",
genHuksOptions: {},
HuksOptions: {
"properties": "",
"inData": new Uint8Array()
},
keyAlias: 'HuksDemoHMAC',
inData: 'huksHmacTest',
handle: {},
},
onInit() {
this.title = this.$t('strings.world');
},
onCreate() {
console.info("Application onCreate");
},
onDestroy() {
console.info("Application onDestroy");
},
stringToUint8Array: function (str) {
var arr = [];
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i));
}
var tmpUint8Array = new Uint8Array(arr);
return tmpUint8Array;
},
huksGenerateKey: function () { async function routePage() {
var alias = this.keyAlias; let options = {
var properties = new Array(); uri: 'pages/second'
}
try {
await router.push(options)
} catch (err) {
console.error(`fail callback, code: ${err.code}, msg: ${err.msg}`)
}
}
var alias = "HuksDemoRSA";
var properties = new Array();
var options = {
properties: properties,
inData: new Uint8Array(0)
};
var handle = {};
var resultMessage = "";
async function generateKey() {
properties[0] = { properties[0] = {
tag: huks.HuksTag.HUKS_TAG_ALGORITHM, tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
value: huks.HuksKeyAlg.HUKS_ALG_RSA value: huks.HuksKeyAlg.HUKS_ALG_RSA
...@@ -1029,71 +1010,164 @@ export default { ...@@ -1029,71 +1010,164 @@ export default {
tag: huks.HuksTag.HUKS_TAG_DIGEST, tag: huks.HuksTag.HUKS_TAG_DIGEST,
value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
}; };
var options = { huks.generateKey(alias, options);
properties: properties }
}; function stringToUint8Array(str) {
this.HuksOptions.properties = properties; var arr = [];
this.genHuksOptions = options; for (var i = 0, j = str.length; i < j; ++i) {
huks.generateKey(alias, options, function (err, data) { }); arr.push(str.charCodeAt(i));
}, }
var tmpUint8Array = new Uint8Array(arr);
async huksInit() { return tmpUint8Array;
var alias = this.keyAlias; }
var that = this; async function huksInit() {
return new Promise((resolve, reject) => { await huks.init(alias, options).then((data) => {
huks.init(alias, this.genHuksOptions, async function (err, data) { console.log(`test init data: ${JSON.stringify(data)}`);
console.log(`the init err is :${JSON.stringify(err)}`); handle = {
console.log(`the init data is :${JSON.stringify(data)}`);
if (data.errorCode === 0) {
that.resultMessage = "init 执行成功!"
that.handle = {
"handle1": data.handle1, "handle1": data.handle1,
"handle2": data.handle2 "handle2": data.handle2
} };
} else { }).catch((err) => {
that.resultMessage = "init 执行失败 errorCode: " + data.errorCode console.log("test init err information: " + JSON.stringify(err))
} })
}); }
}); async function huksUpdate() {
},
async huksUpdate() {
let count = 2; let count = 2;
for (let i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
this.HuksOptions.inData = this.stringToUint8Array(this.inData); options.inData = stringToUint8Array("huksHmacTest");
console.log(`Huks_Demo hmac before update HuksOptions ${JSON.stringify(this.HuksOptions)}`); await huks.update(handle, options).then((data) => {
new Promise((resolve, reject) => {
huks.update(this.handle, this.HuksOptions, function (err, data) {
if (data.errorCode === 0) { if (data.errorCode === 0) {
this.resultMessage += "update 共执行 " + count + "" + "" + (i + 1) + " 次执行成功! " resultMessage += "update success!";
} else { } else {
this.resultMessage += "update 共执行 " + count + "" + "" + (i + 1) + " 次执行失败 errorCode: " + data.errorCode + " " resultMessage += "update fail!";
} }
}).catch((err) => {
resultMessage += "update times: " + count + (i + 1) + " fail catch errorMessage:" + JSON.stringify(err) + " "
}); });
}); console.log(resultMessage);
} }
}, }
function huksFinish() {
huksFinish: function () { options.inData = stringToUint8Array("HuksDemoHMAC");
this.HuksOptions.inData = this.stringToUint8Array("0"); huks.finish(handle, options).then((data) => {
new Promise((resolve, reject) => {
huks.finish(this.handle, this.HuksOptions, function (err, data) {
if (data.errorCode === 0) { if (data.errorCode === 0) {
that.resultMessage = "finish 执行成功!"; resultMessage = "finish success!";
} else { } else {
that.resultMessage = "finish 执行失败 errorCode: " + data.errorCode; resultMessage = "finish fail errorCode: " + data.errorCode;
} }
}).catch((err) => {
resultMessage = "finish fail, catch errorMessage:" + JSON.stringify(err)
}); });
console.log(resultMessage);
}
async function huksAbort() {
huks.abort(handle, options).then((data) => {
if (data.errorCode === 0) {
resultMessage = "abort success!";
} else {
resultMessage = "abort fail errorCode: " + data.errorCode;
}
}).catch((err) => {
resultMessage = "abort fail, catch errorMessage:" + JSON.stringify(err)
}); });
}, console.log(resultMessage);
}
huksAbort: function () { @Entry
new Promise((resolve, reject) => { @Component
huks.abort(this.handle, this.genHuksOptions, function (err, data) { struct Index {
console.log(`Huks_Demo hmac huksAbort1 data ${JSON.stringify(data)}`); build() {
console.log(`Huks_Demo hmac huksAbort1 err ${JSON.stringify(err)}`); Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
}); Text('Hello World')
}); .fontSize(50)
.fontWeight(FontWeight.Bold)
Button() {
Text('Tocallback')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}.type(ButtonType.Capsule)
.margin({
top: 20
})
.width('50%')
.height('10%')
.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,46 +1197,37 @@ abort操作密钥接口,使用Promise方式异步返回结果。 ...@@ -1123,46 +1197,37 @@ 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) {
},
onDestroy() {
console.info("Application onDestroy");
},
stringToUint8Array: function (str) {
var arr = []; var arr = [];
for (var i = 0, j = str.length; i < j; ++i) { for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i)); arr.push(str.charCodeAt(i));
} }
var tmpUint8Array = new Uint8Array(arr); var tmpUint8Array = new Uint8Array(arr);
return tmpUint8Array; return tmpUint8Array;
}, }
huksGenerateKey: function () { async function generateKey() {
console.log("huksGenerate2 is start!!!");
var alias = this.keyAlias;
var properties = new Array();
properties[0] = { properties[0] = {
tag: huks.HuksTag.HUKS_TAG_ALGORITHM, tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
value: huks.HuksKeyAlg.HUKS_ALG_RSA value: huks.HuksKeyAlg.HUKS_ALG_RSA
...@@ -1183,68 +1248,157 @@ export default { ...@@ -1183,68 +1248,157 @@ export default {
tag: huks.HuksTag.HUKS_TAG_DIGEST, tag: huks.HuksTag.HUKS_TAG_DIGEST,
value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256
}; };
var options = { huks.generateKey(alias, options, function (err, data) { });
properties: properties }
}; async function huksInit() {
this.HuksOptions.properties = properties; return new Promise((resolve, reject) => {
this.genHuksOptions = options; huks.init(alias, options, async function (err, data) {
var result = huks.generateKey(alias, this.genHuksOptions); if (data.errorCode === 0) {
console.log("huksgenerate2 is ok!!!") resultMessage = "init success!"
}, handle = {
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, "handle1": data.handle1,
"handle2": data.handle2 "handle2": data.handle2
}; }
}).catch((err) => { } else {
console.log("test init err information: " + JSON.stringify(err)) resultMessage = "init fail errorCode: " + data.errorCode
}) }
}, });
});
}
async huksUpdate() { async function huksUpdate() {
let count = 2; let count = 2;
for (let i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
this.HuksOptions.inData = this.stringToUint8Array(this.inData); options.inData = stringToUint8Array("huksHmacTest");
await huks.update(this.handle, this.HuksOptions).then(async (data) => { new Promise((resolve, reject) => {
huks.update(handle, options, function (err, data) {
if (data.errorCode === 0) { if (data.errorCode === 0) {
this.resultMessage += "update 共执行 " + count + "" + "" + (i + 1) + " 次执行成功! " resultMessage += "update success!";
} else { } else {
this.resultMessage += "update 共执行 " + count + "" + "" + (i + 1) + " 次执行失败 errorCode: " + data.errorCode + " " resultMessage += "update fail!";
} }
}).catch((err) => {
this.resultMessage += "update 共执行 " + count + "" + "" + (i + 1) + " 次执行失败 catch 错误信息:" + JSON.stringify(err) + " "
}); });
});
console.log(resultMessage);
} }
}, }
huksFinish: function () { async function huksFinish() {
this.HuksOptions.inData = this.stringToUint8Array("HuksDemoHMAC"); options.inData = stringToUint8Array("0");
huks.finish(this.handle, this.HuksOptions).then((data) => { new Promise((resolve, reject) => {
huks.finish(handle, options, function (err, data) {
if (data.errorCode === 0) { if (data.errorCode === 0) {
this.resultMessage = "finish 执行成功!"; resultMessage = "finish success!";
} else { } else {
this.resultMessage = "finish 执行失败 errorCode: " + data.errorCode; resultMessage = "finish fail errorCode: " + data.errorCode;
} }
}).catch((err) => {
this.resultMessage = "finish 执行失败, catch 错误信息:" + JSON.stringify(err)
}); });
}, });
}
huksAbort: function () { function huksAbort() {
huks.abort(this.handle, this.HuksOptions).then((data) => { new Promise((resolve, reject) => {
if (data.errorCode === 0) { huks.abort(handle, options, function (err, data) {
this.resultMessage = "abort 执行成功!"; console.log(`Huks_Demo hmac huksAbort1 data ${JSON.stringify(data)}`);
} else { console.log(`Huks_Demo hmac huksAbort1 err ${JSON.stringify(err)}`);
this.resultMessage = "abort 执行失败 errorCode: " + data.errorCode;
}
}).catch((err) => {
this.resultMessage = "abort 执行失败, catch 错误信息:" + 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.
先完成此消息的编辑!
想要评论请 注册