Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
adda4627
D
Docs
项目概览
OpenHarmony
/
Docs
大约 1 年 前同步成功
通知
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,发现更多精彩内容 >>
未验证
提交
adda4627
编写于
7月 12, 2023
作者:
W
wkyrong
提交者:
Gitee
7月 12, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update js demo
Signed-off-by:
N
wkyrong
<
wangkairong@huawei.com
>
上级
4849f1c7
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
96 addition
and
89 deletion
+96
-89
zh-cn/device-dev/subsystems/subsys-security-huks-guide.md
zh-cn/device-dev/subsystems/subsys-security-huks-guide.md
+96
-89
未找到文件。
zh-cn/device-dev/subsystems/subsys-security-huks-guide.md
浏览文件 @
adda4627
...
@@ -1347,99 +1347,106 @@ JS测试代码示例如下(仅供参考),如果整个流程能够正常运
...
@@ -1347,99 +1347,106 @@ JS测试代码示例如下(仅供参考),如果整个流程能够正常运
2.
使用generateKey接口生成密钥。
2.
使用generateKey接口生成密钥。
```
js
```
js
var
alias
=
'
testAlias
'
;
let
aesKeyAlias
=
'
test_aesKeyAlias
'
;
let
handle
;
let
IV
=
'
001122334455
'
;
let
cipherData
:
Uint8Array
;
let
plainData
:
Uint8Array
;
function
GetAesGenerateProperties
()
{
var
properties
=
new
Array
();
var
properties
=
new
Array
();
properties
[
0
]
=
{
var
index
=
0
;
properties
[
index
++
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_ALGORITHM
,
tag
:
huks
.
HuksTag
.
HUKS_TAG_ALGORITHM
,
value
:
huks
.
HuksKeyAlg
.
HUKS_ALG_ECC
value
:
huks
.
HuksKeyAlg
.
HUKS_ALG_AES
};
};
properties
[
1
]
=
{
properties
[
index
++
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_KEY_SIZE
,
tag
:
huks
.
HuksTag
.
HUKS_TAG_KEY_SIZE
,
value
:
huks
.
HuksKeySize
.
HUKS_ECC_KEY_SIZE_224
value
:
huks
.
HuksKeySize
.
HUKS_AES_KEY_SIZE_128
};
};
properties
[
2
]
=
{
properties
[
index
++
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_PURPOSE
,
tag
:
huks
.
HuksTag
.
HUKS_TAG_PURPOSE
,
value
:
huks
.
HuksKeyPurpose
.
HUKS_KEY_PURPOSE_AGREE
value
:
huks
.
HuksKeyPurpose
.
HUKS_KEY_PURPOSE_ENCRYPT
|
};
huks
.
HuksKeyPurpose
.
HUKS_KEY_PURPOSE_DECRYPT
properties
[
3
]
=
{
}
tag
:
huks
.
HuksTag
.
HUKS_TAG_DIGEST
,
return
properties
;
value
:
huks
.
HuksKeyDigest
.
HUKS_DIGEST_NONE
};
var
options
=
{
properties
:
properties
}
}
var
resultA
=
huks
.
generateKey
(
alias
,
options
);
```
3.
使用Init接口进行init操作。
```
js
async
function
GenerateAesKey
()
{
var
alias
=
'
test001
'
var
genProperties
=
GetAesGenerateProperties
();
var
properties
=
new
Array
();
properties
[
0
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_ALGORITHM
,
value
:
huks
.
HuksKeyAlg
.
HUKS_ALG_DH
};
properties
[
1
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_PURPOSE
,
value
:
huks
.
HuksKeyPurpose
.
HUKS_KEY_PURPOSE_AGREE
};
properties
[
2
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_KEY_SIZE
,
value
:
huks
.
HuksKeySize
.
HUKS_DH_KEY_SIZE_4096
};
var
options
=
{
var
options
=
{
properties
:
properties
properties
:
genProperties
};
huks
.
init
(
alias
,
options
,
function
(
err
,
data
)
{
if
(
err
.
code
!==
0
)
{
console
.
log
(
"
test init err information:
"
+
JSON
.
stringify
(
err
));
}
else
{
console
.
log
(
`test init data:
${
JSON
.
stringify
(
data
)}
`
);
}
}
await
huks
.
generateKeyItem
(
aesKeyAlias
,
options
).
then
((
data
)
=>
{
console
.
log
(
"
generateKeyItem success
"
);
}).
catch
((
err
)
=>
{
console
.
log
(
"
generateKeyItem failed
"
);
})
})
}
```
```
4.
使用Update接口进行update操作
。
3.
使用huks.initSession,huks.finishSession进行加密
。
```
js
```
js
var
properties
=
new
Array
();
let
plainText
=
'
123456
'
;
properties
[
0
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_ALGORITHM
,
value
:
huks
.
HuksKeyAlg
.
HUKS_ALG_DH
};
properties
[
1
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_PURPOSE
,
value
:
huks
.
HuksKeyPurpose
.
HUKS_KEY_PURPOSE_AGREE
};
properties
[
2
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_KEY_SIZE
,
value
:
huks
.
HuksKeySize
.
HUKS_DH_KEY_SIZE_4096
};
var
options
=
{
properties
:
properties
};
var
result
=
huks
.
update
(
handle
,
options
)
```
5.
使用Finish接口进行finish操作。
function
StringToUint8Array
(
str
)
{
let
arr
=
[];
for
(
let
i
=
0
,
j
=
str
.
length
;
i
<
j
;
++
i
)
{
arr
.
push
(
str
.
charCodeAt
(
i
));
}
return
new
Uint8Array
(
arr
);
}
```
js
function
GetAesEncryptProperties
()
{
var
properties
=
new
Array
();
var
properties
=
new
Array
();
properties
[
0
]
=
{
var
index
=
0
;
properties
[
index
++
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_ALGORITHM
,
tag
:
huks
.
HuksTag
.
HUKS_TAG_ALGORITHM
,
value
:
huks
.
HuksKeyAlg
.
HUKS_ALG_DH
value
:
huks
.
HuksKeyAlg
.
HUKS_ALG_AES
};
};
properties
[
1
]
=
{
properties
[
index
++
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_PURPOSE
,
value
:
huks
.
HuksKeyPurpose
.
HUKS_KEY_PURPOSE_AGREE
};
properties
[
2
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_KEY_SIZE
,
tag
:
huks
.
HuksTag
.
HUKS_TAG_KEY_SIZE
,
value
:
huks
.
HuksKeySize
.
HUKS_DH_KEY_SIZE_4096
value
:
huks
.
HuksKeySize
.
HUKS_AES_KEY_SIZE_128
};
};
properties
[
index
++
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_PURPOSE
,
value
:
huks
.
HuksKeyPurpose
.
HUKS_KEY_PURPOSE_ENCRYPT
}
properties
[
index
++
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_PADDING
,
value
:
huks
.
HuksKeyPadding
.
HUKS_PADDING_PKCS7
}
properties
[
index
++
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_BLOCK_MODE
,
value
:
huks
.
HuksCipherMode
.
HUKS_MODE_CBC
}
properties
[
index
++
]
=
{
tag
:
huks
.
HuksTag
.
HUKS_TAG_IV
,
value
:
StringToUint8Array
(
IV
)
}
return
properties
;
}
async
function
EncryptData
()
{
var
encryptProperties
=
GetAesEncryptProperties
();
var
options
=
{
var
options
=
{
properties
:
properties
properties
:
encryptProperties
,
};
inData
:
StringToUint8Array
(
plainText
)
var
result
=
huks
.
finish
(
handle
,
options
)
}
await
huks
.
initSession
(
aesKeyAlias
,
options
).
then
((
data
)
=>
{
handle
=
data
.
handle
;
}).
catch
((
err
)
=>
{
console
.
log
(
"
initSession failed
"
);
})
await
huks
.
finishSession
(
handle
,
options
).
then
((
data
)
=>
{
console
.
log
(
"
finishSession success
"
);
cipherData
=
data
.
outData
}).
catch
((
err
)
=>
{
console
.
log
(
"
finishSession failed
"
);
})
}
```
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录