Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
4d3330e8
D
Docs
项目概览
OpenHarmony
/
Docs
12 个月 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4d3330e8
编写于
4月 23, 2022
作者:
S
shuyi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
API_format
Signed-off-by:
N
shuyi
<
shuyi4@huawei.com
>
上级
ac7e0ecf
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
400 addition
and
246 deletion
+400
-246
zh-cn/application-dev/reference/apis/js-apis-huks.md
zh-cn/application-dev/reference/apis/js-apis-huks.md
+400
-246
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-huks.md
浏览文件 @
4d3330e8
...
...
@@ -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回调异步返回结果 。
* 以下以RSA1024密钥的callback操作使用为例
*/
import
router
from
'
@system.router
'
;
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
;
},
import
huks
from
'
@ohos.security.huks
'
;
huksGenerateKey
:
function
()
{
var
alias
=
this
.
keyAlias
;
var
properties
=
new
Array
();
async
function
routePage
()
{
let
options
=
{
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
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_ALGORITHM
,
value
:
huks
.
HuksKeyAlg
.
HUKS_ALG_RSA
...
...
@@ -1029,71 +1010,164 @@ export default {
tag
:
huks
.
HuksTag
.
HUKS_TAG_DIGEST
,
value
:
huks
.
HuksKeyDigest
.
HUKS_DIGEST_SHA256
};
var
options
=
{
properties
:
properties
};
this
.
HuksOptions
.
properties
=
properties
;
this
.
genHuksOptions
=
options
;
huks
.
generateKey
(
alias
,
options
,
function
(
err
,
data
)
{
});
},
async
huksInit
()
{
var
alias
=
this
.
keyAlias
;
var
that
=
this
;
return
new
Promise
((
resolve
,
reject
)
=>
{
huks
.
init
(
alias
,
this
.
genHuksOptions
,
async
function
(
err
,
data
)
{
console
.
log
(
`the init err is :
${
JSON
.
stringify
(
err
)}
`
);
console
.
log
(
`the init data is :
${
JSON
.
stringify
(
data
)}
`
);
if
(
data
.
errorCode
===
0
)
{
that
.
resultMessage
=
"
init 执行成功!
"
that
.
handle
=
{
huks
.
generateKey
(
alias
,
options
);
}
function
stringToUint8Array
(
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
;
}
async
function
huksInit
()
{
await
huks
.
init
(
alias
,
options
).
then
((
data
)
=>
{
console
.
log
(
`test init data:
${
JSON
.
stringify
(
data
)}
`
);
handle
=
{
"
handle1
"
:
data
.
handle1
,
"
handle2
"
:
data
.
handle2
}
}
else
{
that
.
resultMessage
=
"
init 执行失败 errorCode:
"
+
data
.
errorCode
}
});
});
},
async
huksUpdate
()
{
};
}).
catch
((
err
)
=>
{
console
.
log
(
"
test init err information:
"
+
JSON
.
stringify
(
err
))
})
}
async
function
huksUpdate
()
{
let
count
=
2
;
for
(
let
i
=
0
;
i
<
count
;
i
++
)
{
this
.
HuksOptions
.
inData
=
this
.
stringToUint8Array
(
this
.
inData
);
console
.
log
(
`Huks_Demo hmac before update HuksOptions
${
JSON
.
stringify
(
this
.
HuksOptions
)}
`
);
new
Promise
((
resolve
,
reject
)
=>
{
huks
.
update
(
this
.
handle
,
this
.
HuksOptions
,
function
(
err
,
data
)
{
options
.
inData
=
stringToUint8Array
(
"
huksHmacTest
"
);
await
huks
.
update
(
handle
,
options
).
then
((
data
)
=>
{
if
(
data
.
errorCode
===
0
)
{
this
.
resultMessage
+=
"
update 共执行
"
+
count
+
"
次
"
+
"
第
"
+
(
i
+
1
)
+
"
次执行成功!
"
resultMessage
+=
"
update success!
"
;
}
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
);
}
},
huksFinish
:
function
()
{
this
.
HuksOptions
.
inData
=
this
.
stringToUint8Array
(
"
0
"
);
new
Promise
((
resolve
,
reject
)
=>
{
huks
.
finish
(
this
.
handle
,
this
.
HuksOptions
,
function
(
err
,
data
)
{
}
function
huksFinish
()
{
options
.
inData
=
stringToUint8Array
(
"
HuksDemoHMAC
"
);
huks
.
finish
(
handle
,
options
).
then
((
data
)
=>
{
if
(
data
.
errorCode
===
0
)
{
that
.
resultMessage
=
"
finish 执行成功!
"
;
resultMessage
=
"
finish success!
"
;
}
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
()
{
new
Promise
((
resolve
,
reject
)
=>
{
huks
.
abort
(
this
.
handle
,
this
.
genHuksOptions
,
function
(
err
,
data
)
{
console
.
log
(
`Huks_Demo hmac huksAbort1 data
${
JSON
.
stringify
(
data
)}
`
);
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
(
'
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方式异步返回结果。
* 以下以RSA1024密钥的promise操作使用为例
*/
import
router
from
'
@system.router
'
;
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
)
{
import
huks
from
'
@ohos.security.huks
'
;
async
function
routePage
()
{
let
options
=
{
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
=
""
;
function
stringToUint8Array
(
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
()
{
console
.
log
(
"
huksGenerate2 is start!!!
"
);
var
alias
=
this
.
keyAlias
;
var
properties
=
new
Array
();
async
function
generateKey
()
{
properties
[
0
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_ALGORITHM
,
value
:
huks
.
HuksKeyAlg
.
HUKS_ALG_RSA
...
...
@@ -1183,68 +1248,157 @@ export default {
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
=
{
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
};
}).
catch
((
err
)
=>
{
console
.
log
(
"
test init err information:
"
+
JSON
.
stringify
(
err
))
})
},
}
}
else
{
resultMessage
=
"
init fail errorCode:
"
+
data
.
errorCode
}
});
});
}
async
huksUpdate
()
{
async
function
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
)
=>
{
options
.
inData
=
stringToUint8Array
(
"
huksHmacTest
"
);
new
Promise
((
resolve
,
reject
)
=>
{
huks
.
update
(
handle
,
options
,
function
(
err
,
data
)
{
if
(
data
.
errorCode
===
0
)
{
this
.
resultMessage
+=
"
update 共执行
"
+
count
+
"
次
"
+
"
第
"
+
(
i
+
1
)
+
"
次执行成功!
"
resultMessage
+=
"
update success!
"
;
}
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
()
{
this
.
HuksOptions
.
inData
=
this
.
stringToUint8Array
(
"
HuksDemoHMAC
"
);
huks
.
finish
(
this
.
handle
,
this
.
HuksOptions
).
then
((
data
)
=>
{
async
function
huksFinish
()
{
options
.
inData
=
stringToUint8Array
(
"
0
"
);
new
Promise
((
resolve
,
reject
)
=>
{
huks
.
finish
(
handle
,
options
,
function
(
err
,
data
)
{
if
(
data
.
errorCode
===
0
)
{
this
.
resultMessage
=
"
finish 执行成功!
"
;
resultMessage
=
"
finish success!
"
;
}
else
{
this
.
resultMessage
=
"
finish 执行失败
errorCode:
"
+
data
.
errorCode
;
resultMessage
=
"
finish fail
errorCode:
"
+
data
.
errorCode
;
}
}).
catch
((
err
)
=>
{
this
.
resultMessage
=
"
finish 执行失败, catch 错误信息:
"
+
JSON
.
stringify
(
err
)
});
},
});
}
huksAbort
:
function
()
{
huks
.
abort
(
this
.
handle
,
this
.
HuksOptions
).
then
((
data
)
=>
{
if
(
data
.
errorCode
===
0
)
{
this
.
resultMessage
=
"
abort 执行成功!
"
;
}
else
{
this
.
resultMessage
=
"
abort 执行失败 errorCode:
"
+
data
.
errorCode
;
}
}).
catch
((
err
)
=>
{
this
.
resultMessage
=
"
abort 执行失败, catch 错误信息:
"
+
JSON
.
stringify
(
err
)
function
huksAbort
()
{
new
Promise
((
resolve
,
reject
)
=>
{
huks
.
abort
(
handle
,
options
,
function
(
err
,
data
)
{
console
.
log
(
`Huks_Demo hmac huksAbort1 data
${
JSON
.
stringify
(
data
)}
`
);
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录