Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Xts Acts
提交
bcd649c5
X
Xts Acts
项目概览
OpenHarmony
/
Xts Acts
1 年多 前同步成功
通知
9
Star
22
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
X
Xts Acts
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
bcd649c5
编写于
7月 19, 2023
作者:
O
openharmony_ci
提交者:
Gitee
7月 19, 2023
浏览文件
操作
浏览文件
下载
差异文件
!9397 【OpenHarmony 4.0.9.2】【xts_acts】【安全子系统】【ToD】加解密算法库框架更新摘要算法部分测试套的逻辑
Merge pull request !9397 from 黄坤/qwink230719
上级
0bf882ff
58721f07
变更
3
展开全部
隐藏空白更改
内联
并排
Showing
3 changed file
with
756 addition
and
833 deletion
+756
-833
security/cryptoFramework/js_api_test_one/src/main/js/test/DigestAlgorithm.test.js
.../js_api_test_one/src/main/js/test/DigestAlgorithm.test.js
+413
-391
security/cryptoFramework/js_api_test_one/src/main/js/test/utils/digestalgorithm/publicDigestCallback.js
...ain/js/test/utils/digestalgorithm/publicDigestCallback.js
+229
-269
security/cryptoFramework/js_api_test_one/src/main/js/test/utils/digestalgorithm/publicDigestPromise.js
...main/js/test/utils/digestalgorithm/publicDigestPromise.js
+114
-173
未找到文件。
security/cryptoFramework/js_api_test_one/src/main/js/test/DigestAlgorithm.test.js
浏览文件 @
bcd649c5
此差异已折叠。
点击以展开。
security/cryptoFramework/js_api_test_one/src/main/js/test/utils/digestalgorithm/publicDigestCallback.js
浏览文件 @
bcd649c5
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import
{
expect
}
from
"
@ohos/hypium
"
;
import
cryptoFramework
from
"
@ohos.security.cryptoFramework
"
;
import
{
stringTouInt8Array
,
uInt8ArrayToShowStr
,
uInt8ArrayToString
,
}
from
"
../common/publicDoString
"
;
async
function
generateSymKey
(
symKeyGenerator
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
symKeyGenerator
.
generateSymKey
((
err
,
symKey
)
=>
{
if
(
err
)
{
console
.
error
(
"
[Callback]generateSymKey failed. error is
"
+
err
);
reject
(
err
);
}
else
{
console
.
log
(
"
[Callback]generateSymKey success. symKey is
"
+
symKey
);
console
.
warn
(
"
[Callback]key algName:
"
+
symKey
.
algName
);
console
.
warn
(
"
[Callback]key format:
"
+
symKey
.
format
);
var
encodeKey
=
symKey
.
getEncoded
();
console
.
warn
(
"
[Callback]key getEncoded hex:
"
+
uInt8ArrayToShowStr
(
encodeKey
.
data
)
);
resolve
(
symKey
);
}
});
});
}
async
function
initMac
(
macGenerator
,
symKey
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
macGenerator
.
init
(
symKey
,
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
"
[Callback]macGenerator init failed. error is
"
+
err
);
reject
(
err
);
}
else
{
console
.
log
(
"
[Callback]macGenerator init success!
"
);
resolve
(
"
init success
"
);
}
});
});
}
async
function
updateMac
(
macGenerator
,
dataBlob
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
macGenerator
.
update
(
dataBlob
,
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
"
[Callback]macGenerator update failed. error is
"
+
err
);
reject
(
err
);
}
else
{
console
.
log
(
"
[Callback]macGenerator update success!
"
);
resolve
(
"
update success
"
);
}
});
});
}
async
function
doFinalMac
(
macGenerator
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
macGenerator
.
doFinal
((
err
,
output
)
=>
{
if
(
err
)
{
console
.
error
(
"
[Callback]macGenerator doFinal failed. error is
"
+
err
);
reject
(
err
);
}
else
{
console
.
log
(
"
[Callback]macGenerator doFinal success! output is:
"
+
output
);
resolve
(
output
);
}
});
});
}
async
function
updateMd
(
mdGenerator
,
dataBlob
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
mdGenerator
.
update
(
dataBlob
,
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
"
[Callback]mdGenerator update failed. error is
"
+
err
);
reject
(
err
);
}
else
{
console
.
log
(
"
[Callback]mdGenerator update success!
"
);
resolve
(
"
update success
"
);
}
});
});
}
async
function
digestMd
(
mdGenerator
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
mdGenerator
.
digest
((
err
,
output
)
=>
{
if
(
err
)
{
console
.
error
(
"
[Callback]mdGenerator digest failed. error is
"
+
err
);
reject
(
err
);
}
else
{
console
.
log
(
"
[Callback]mdGenerator digest success! output is:
"
+
output
);
resolve
(
output
);
}
});
});
}
async
function
testMDDigestCallback
(
MDAlgoName
)
{
var
globalMd
;
var
globalText
=
"
my test data
"
;
var
inBlob
=
{
data
:
stringTouInt8Array
(
globalText
),
};
return
new
Promise
((
resolve
,
reject
)
=>
{
globalMd
=
cryptoFramework
.
createMd
(
MDAlgoName
);
expect
(
globalMd
!=
null
).
assertTrue
();
console
.
warn
(
"
md=
"
+
globalMd
);
console
.
warn
(
"
MD algName is:
"
+
globalMd
.
algName
);
updateMd
(
globalMd
,
inBlob
)
.
then
((
updateData
)
=>
{
expect
(
updateData
===
"
update success
"
).
assertTrue
();
return
digestMd
(
globalMd
);
})
.
then
((
digestBlob
)
=>
{
console
.
warn
(
"
[callback]: digest result:
"
+
uInt8ArrayToShowStr
(
digestBlob
.
data
)
);
let
mdLen
=
globalMd
.
getMdLength
();
console
.
warn
(
"
Md len:
"
+
mdLen
);
expect
(
digestBlob
!=
null
&&
mdLen
!=
0
&&
mdLen
!=
null
).
assertTrue
();
resolve
();
})
.
catch
((
err
)
=>
{
console
.
error
(
"
testMDDigestCallback catch error:
"
+
err
);
reject
(
err
);
});
});
}
async
function
testMDDigestCallbackLen
(
MDAlgoName
,
DatablobLen
)
{
var
globalMd
;
var
i
;
var
globalText
;
var
t
=
"
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz
"
,
n
=
t
.
length
,
s
=
""
;
for
(
i
=
0
;
i
<
DatablobLen
;
i
++
)
{
globalText
+=
t
.
charAt
(
Math
.
floor
(
Math
.
random
()
*
n
));
}
console
.
warn
(
"
Datablob =
"
+
globalText
);
var
ginBlob
=
{
data
:
stringTouInt8Array
(
globalText
),
};
return
new
Promise
((
resolve
,
reject
)
=>
{
globalMd
=
cryptoFramework
.
createMd
(
MDAlgoName
);
expect
(
globalMd
!=
null
).
assertTrue
();
console
.
warn
(
"
md=
"
+
globalMd
);
console
.
warn
(
"
MD algName is:
"
+
globalMd
.
algName
);
updateMd
(
globalMd
,
ginBlob
)
.
then
((
updateData
)
=>
{
expect
(
updateData
===
"
update success
"
).
assertTrue
();
return
digestMd
(
globalMd
);
})
.
then
((
digestBlob
)
=>
{
console
.
warn
(
"
[callback]: digest result:
"
+
uInt8ArrayToShowStr
(
digestBlob
.
data
)
);
let
mdLen
=
globalMd
.
getMdLength
();
console
.
warn
(
"
Md len:
"
+
mdLen
);
expect
(
digestBlob
!=
null
&&
mdLen
!=
0
&&
mdLen
!=
null
).
assertTrue
();
resolve
();
})
.
catch
((
err
)
=>
{
console
.
error
(
"
testMDDigestCallback catch error:
"
+
err
);
reject
(
err
);
});
});
}
async
function
testMDErrorAlgorithm
(
MDAlgoName
)
{
var
globalMd
;
return
new
Promise
((
resolve
,
reject
)
=>
{
console
.
warn
(
"
md= test begin
"
);
try
{
globalMd
=
cryptoFramework
.
createMd
(
MDAlgoName
);
reject
();
}
catch
(
error
)
{
console
.
error
(
"
[Promise]: error code:
"
+
error
.
code
+
"
, message is:
"
+
error
.
message
);
expect
(
error
.
code
==
801
).
assertTrue
();
resolve
();
}
});
}
async
function
testMDErrorAlgorithmNull
(
MDAlgoName
)
{
var
globalMd
;
return
new
Promise
((
resolve
,
reject
)
=>
{
try
{
globalMd
=
cryptoFramework
.
createMd
(
MDAlgoName
);
reject
();
}
catch
(
error
)
{
console
.
error
(
"
[Promise]: error code:
"
+
error
.
code
+
"
, message is:
"
+
error
.
message
);
expect
(
error
.
code
==
401
).
assertTrue
();
resolve
();
}
});
}
async
function
testHMACDigestCallback
(
HMACAlgoName
,
keyAlgoName
)
{
var
globalHMAC
;
var
globalText
=
"
my test data
"
;
var
globalsymKeyGenerator
;
var
ginBlob
=
{
data
:
stringTouInt8Array
(
globalText
),
};
return
new
Promise
((
resolve
,
reject
)
=>
{
globalHMAC
=
cryptoFramework
.
createMac
(
HMACAlgoName
);
expect
(
globalHMAC
!=
null
).
assertTrue
();
console
.
warn
(
"
mac=
"
+
globalHMAC
);
console
.
warn
(
"
HMAC algName is:
"
+
globalHMAC
.
algName
);
console
.
log
(
"
start to call createSymKeyGenerator()
"
);
globalsymKeyGenerator
=
cryptoFramework
.
createSymKeyGenerator
(
keyAlgoName
);
expect
(
globalsymKeyGenerator
!=
null
).
assertTrue
();
console
.
warn
(
"
symKeyGenerator algName:
"
+
globalsymKeyGenerator
.
algName
);
generateSymKey
(
globalsymKeyGenerator
)
.
then
((
symKey
)
=>
{
expect
(
symKey
!=
null
).
assertTrue
();
return
initMac
(
globalHMAC
,
symKey
);
})
.
then
((
initData
)
=>
{
expect
(
initData
===
"
init success
"
).
assertTrue
();
return
updateMac
(
globalHMAC
,
ginBlob
);
})
.
then
((
updateData
)
=>
{
expect
(
updateData
===
"
update success
"
).
assertTrue
();
return
doFinalMac
(
globalHMAC
);
})
.
then
((
macOutput
)
=>
{
console
.
warn
(
"
HMAC result:
"
+
macOutput
.
data
);
let
macLen
=
globalHMAC
.
getMacLength
();
console
.
warn
(
"
MAC len:
"
+
macLen
);
expect
(
macOutput
!=
null
&&
macLen
!=
0
&&
macLen
!=
null
).
assertTrue
();
resolve
();
})
.
catch
((
err
)
=>
{
console
.
error
(
"
testHMACDigestCallback catch error:
"
+
err
);
reject
(
err
);
});
});
}
export
{
testMDDigestCallback
,
testHMACDigestCallback
,
testMDErrorAlgorithm
,
testMDErrorAlgorithmNull
,
testMDDigestCallbackLen
};
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import
{
expect
}
from
"
@ohos/hypium
"
;
import
cryptoFramework
from
"
@ohos.security.cryptoFramework
"
;
import
{
stringTouInt8Array
,
uInt8ArrayToShowStr
,
uInt8ArrayToString
,
}
from
"
../common/publicDoString
"
;
async
function
generateSymKey
(
symKeyGenerator
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
symKeyGenerator
.
generateSymKey
((
err
,
symKey
)
=>
{
if
(
err
)
{
console
.
error
(
"
[Callback]generateSymKey failed. error is
"
+
err
);
reject
(
err
);
}
else
{
console
.
log
(
"
[Callback]generateSymKey success. symKey is
"
+
symKey
);
console
.
warn
(
"
[Callback]key algName:
"
+
symKey
.
algName
);
console
.
warn
(
"
[Callback]key format:
"
+
symKey
.
format
);
var
encodeKey
=
symKey
.
getEncoded
();
console
.
warn
(
"
[Callback]key getEncoded hex:
"
+
uInt8ArrayToShowStr
(
encodeKey
.
data
)
);
resolve
(
symKey
);
}
});
});
}
async
function
initMac
(
macGenerator
,
symKey
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
macGenerator
.
init
(
symKey
,
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
"
[Callback]macGenerator init failed. error is
"
+
err
);
reject
(
err
);
}
else
{
console
.
log
(
"
[Callback]macGenerator init success!
"
);
resolve
(
"
init success
"
);
}
});
});
}
async
function
updateMac
(
macGenerator
,
dataBlob
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
macGenerator
.
update
(
dataBlob
,
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
"
[Callback]macGenerator update failed. error is
"
+
err
);
reject
(
err
);
}
else
{
console
.
log
(
"
[Callback]macGenerator update success!
"
);
resolve
(
"
update success
"
);
}
});
});
}
async
function
doFinalMac
(
macGenerator
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
macGenerator
.
doFinal
((
err
,
output
)
=>
{
if
(
err
)
{
console
.
error
(
"
[Callback]macGenerator doFinal failed. error is
"
+
err
);
reject
(
err
);
}
else
{
console
.
log
(
"
[Callback]macGenerator doFinal success! output is:
"
+
output
);
resolve
(
output
);
}
});
});
}
async
function
updateMd
(
mdGenerator
,
dataBlob
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
mdGenerator
.
update
(
dataBlob
,
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
"
[Callback]mdGenerator update failed. error is
"
+
err
);
reject
(
err
);
}
else
{
console
.
log
(
"
[Callback]mdGenerator update success!
"
);
resolve
(
"
update success
"
);
}
});
});
}
async
function
digestMd
(
mdGenerator
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
mdGenerator
.
digest
((
err
,
output
)
=>
{
if
(
err
)
{
console
.
error
(
"
[Callback]mdGenerator digest failed. error is
"
+
err
);
reject
(
err
);
}
else
{
console
.
log
(
"
[Callback]mdGenerator digest success! output is:
"
+
output
);
resolve
(
output
);
}
});
});
}
async
function
testMDDigestCallback
(
MDAlgoName
)
{
var
globalMd
;
var
globalText
=
"
my test data
"
;
var
inBlob
=
{
data
:
stringTouInt8Array
(
globalText
),
};
return
new
Promise
((
resolve
,
reject
)
=>
{
globalMd
=
cryptoFramework
.
createMd
(
MDAlgoName
);
expect
(
globalMd
!=
null
).
assertTrue
();
console
.
warn
(
"
md=
"
+
globalMd
);
console
.
warn
(
"
MD algName is:
"
+
globalMd
.
algName
);
updateMd
(
globalMd
,
inBlob
)
.
then
((
updateData
)
=>
{
expect
(
updateData
===
"
update success
"
).
assertTrue
();
return
digestMd
(
globalMd
);
})
.
then
((
digestBlob
)
=>
{
console
.
warn
(
"
[callback]: digest result:
"
+
uInt8ArrayToShowStr
(
digestBlob
.
data
)
);
let
mdLen
=
globalMd
.
getMdLength
();
console
.
warn
(
"
Md len:
"
+
mdLen
);
expect
(
digestBlob
!=
null
&&
mdLen
!=
0
&&
mdLen
!=
null
).
assertTrue
();
resolve
();
})
.
catch
((
err
)
=>
{
console
.
error
(
"
testMDDigestCallback catch error:
"
+
err
);
reject
(
err
);
});
});
}
async
function
testMDDigestCallbackLen
(
MDAlgoName
,
DatablobLen
)
{
let
globalText
=
""
;
let
t
=
"
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz0123456789
"
;
for
(
let
i
=
0
;
i
<
DatablobLen
;
i
++
)
{
globalText
+=
t
.
charAt
(
Math
.
floor
(
Math
.
random
()
*
t
.
length
));
}
console
.
warn
(
"
Datablob =
"
+
globalText
);
var
ginBlob
=
{
data
:
stringTouInt8Array
(
globalText
),
};
try
{
let
mdObj
=
cryptoFramework
.
createMd
(
MDAlgoName
);
expect
(
mdObj
!=
null
).
assertTrue
();
let
updateData
=
await
updateMd
(
mdObj
,
ginBlob
);
expect
(
updateData
===
"
update success
"
).
assertTrue
();
let
digestBlob
=
await
digestMd
(
mdObj
);
console
.
warn
(
"
[callback]: digest result:
"
+
uInt8ArrayToShowStr
(
digestBlob
.
data
)
);
let
mdLen
=
mdObj
.
getMdLength
();
console
.
log
(
"
mdLen is:
"
+
mdLen
);
expect
(
digestBlob
!=
null
&&
mdLen
!=
0
&&
mdLen
!=
null
).
assertTrue
();
}
catch
(
err
)
{
console
.
error
(
"
testMDDigestCallback catch error:
"
+
err
);
throw
err
;
}
}
async
function
testHMACDigestCallback
(
HMACAlgoName
,
keyAlgoName
)
{
var
globalHMAC
;
var
globalText
=
"
my test data
"
;
var
globalsymKeyGenerator
;
var
ginBlob
=
{
data
:
stringTouInt8Array
(
globalText
),
};
return
new
Promise
((
resolve
,
reject
)
=>
{
globalHMAC
=
cryptoFramework
.
createMac
(
HMACAlgoName
);
expect
(
globalHMAC
!=
null
).
assertTrue
();
console
.
warn
(
"
mac=
"
+
globalHMAC
);
console
.
warn
(
"
HMAC algName is:
"
+
globalHMAC
.
algName
);
console
.
log
(
"
start to call createSymKeyGenerator()
"
);
globalsymKeyGenerator
=
cryptoFramework
.
createSymKeyGenerator
(
keyAlgoName
);
expect
(
globalsymKeyGenerator
!=
null
).
assertTrue
();
console
.
warn
(
"
symKeyGenerator algName:
"
+
globalsymKeyGenerator
.
algName
);
generateSymKey
(
globalsymKeyGenerator
)
.
then
((
symKey
)
=>
{
expect
(
symKey
!=
null
).
assertTrue
();
return
initMac
(
globalHMAC
,
symKey
);
})
.
then
((
initData
)
=>
{
expect
(
initData
===
"
init success
"
).
assertTrue
();
return
updateMac
(
globalHMAC
,
ginBlob
);
})
.
then
((
updateData
)
=>
{
expect
(
updateData
===
"
update success
"
).
assertTrue
();
return
doFinalMac
(
globalHMAC
);
})
.
then
((
macOutput
)
=>
{
console
.
warn
(
"
HMAC result:
"
+
macOutput
.
data
);
let
macLen
=
globalHMAC
.
getMacLength
();
console
.
warn
(
"
MAC len:
"
+
macLen
);
expect
(
macOutput
!=
null
&&
macLen
!=
0
&&
macLen
!=
null
).
assertTrue
();
resolve
();
})
.
catch
((
err
)
=>
{
console
.
error
(
"
testHMACDigestCallback catch error:
"
+
err
);
reject
(
err
);
});
});
}
export
{
testMDDigestCallback
,
testHMACDigestCallback
,
testMDDigestCallbackLen
,
};
security/cryptoFramework/js_api_test_one/src/main/js/test/utils/digestalgorithm/publicDigestPromise.js
浏览文件 @
bcd649c5
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import
{
expect
}
from
"
@ohos/hypium
"
;
import
cryptoFramework
from
"
@ohos.security.cryptoFramework
"
;
import
{
stringTouInt8Array
,
uInt8ArrayToShowStr
,
uInt8ArrayToString
,
}
from
"
../common/publicDoString
"
;
async
function
testMDDigestPromise
(
MDAlgoName
)
{
var
globalMd
;
var
globalText
=
"
my test data
"
;
return
new
Promise
((
resolve
,
reject
)
=>
{
globalMd
=
cryptoFramework
.
createMd
(
MDAlgoName
);
expect
(
globalMd
!=
null
).
assertTrue
();
console
.
warn
(
"
md=
"
+
globalMd
);
console
.
warn
(
"
MD algName is:
"
+
globalMd
.
algName
);
let
inBlob
=
{
data
:
stringTouInt8Array
(
globalText
),
};
globalMd
.
update
(
inBlob
)
.
then
(()
=>
{
console
.
warn
(
"
[Promise]: update finished
"
);
let
digestBlob
=
globalMd
.
digest
();
return
digestBlob
;
})
.
then
((
mdOutput
)
=>
{
console
.
warn
(
"
[Promise]: digest result1:
"
+
uInt8ArrayToShowStr
(
mdOutput
.
data
)
);
console
.
warn
(
"
[Promise]: digest result2:
"
+
mdOutput
.
data
);
let
mdLen
=
globalMd
.
getMdLength
();
console
.
warn
(
"
Md len:
"
+
mdLen
);
expect
(
mdOutput
!=
null
&&
mdLen
!=
0
&&
mdLen
!=
null
).
assertTrue
();
resolve
();
})
.
catch
((
err
)
=>
{
console
.
error
(
"
[promise]catch err:
"
+
err
);
reject
(
err
);
});
});
}
async
function
testHMACDigestPromise
(
HMACAlgoName
,
keyAlgoName
)
{
var
globalHMAC
;
var
globalText
=
"
my test data
"
;
var
globalsymKeyGenerator
;
var
inBlob
=
{
data
:
stringTouInt8Array
(
globalText
),
};
return
new
Promise
((
resolve
,
reject
)
=>
{
globalHMAC
=
cryptoFramework
.
createMac
(
HMACAlgoName
);
expect
(
globalHMAC
!=
null
).
assertTrue
();
console
.
warn
(
"
mac=
"
+
globalHMAC
);
console
.
warn
(
"
HMAC algName is:
"
+
globalHMAC
.
algName
);
console
.
log
(
"
start to call createSymKeyGenerator()
"
);
globalsymKeyGenerator
=
cryptoFramework
.
createSymKeyGenerator
(
keyAlgoName
);
console
.
warn
(
"
symKeyGenerator algName:
"
+
globalsymKeyGenerator
.
algName
);
expect
(
globalsymKeyGenerator
!=
null
).
assertTrue
();
console
.
log
(
"
createSymKeyGenerator ok
"
);
console
.
warn
(
"
symKeyGenerator algName:
"
+
globalsymKeyGenerator
.
algName
);
globalsymKeyGenerator
.
generateSymKey
()
.
then
((
key
)
=>
{
expect
(
key
!=
null
).
assertTrue
();
console
.
warn
(
"
generateSymKey ok
"
);
console
.
warn
(
"
key algName:
"
+
key
.
algName
);
console
.
warn
(
"
key format:
"
+
key
.
format
);
var
encodedKey
=
key
.
getEncoded
();
console
.
warn
(
"
key getEncoded hex:
"
+
uInt8ArrayToShowStr
(
encodedKey
.
data
)
);
var
promiseMacInit
=
globalHMAC
.
init
(
key
);
return
promiseMacInit
;
})
.
then
(()
=>
{
var
promiseMacUpdate
=
globalHMAC
.
update
(
inBlob
);
return
promiseMacUpdate
;
})
.
then
(()
=>
{
var
promiseMacdoFinal
=
globalHMAC
.
doFinal
();
return
promiseMacdoFinal
;
})
.
then
((
macOutput
)
=>
{
console
.
warn
(
"
HMAC result:
"
+
macOutput
.
data
);
let
macLen
=
globalHMAC
.
getMacLength
();
console
.
warn
(
"
MAC len:
"
+
macLen
);
expect
(
macOutput
!=
null
&&
macLen
!=
0
&&
macLen
!=
null
).
assertTrue
();
resolve
();
})
.
catch
((
err
)
=>
{
console
.
error
(
"
[promise]catch err:
"
+
err
);
reject
(
err
);
});
});
}
async
function
testHMACDigestPromiseDatablobLong
(
HMACAlgoName
,
keyAlgoName
,
DatablobLen
)
{
var
globalHMAC
;
var
globalsymKeyGenerator
;
var
i
;
var
globalText
;
var
t
=
"
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz
"
,
n
=
t
.
length
,
s
=
""
;
for
(
i
=
0
;
i
<
DatablobLen
;
i
++
){
globalText
+=
t
.
charAt
(
Math
.
floor
(
Math
.
random
()
*
n
));
}
console
.
warn
(
"
Datablob =
"
+
globalText
);
var
inBlob
=
{
data
:
stringTouInt8Array
(
globalText
),
};
return
new
Promise
((
resolve
,
reject
)
=>
{
globalHMAC
=
cryptoFramework
.
createMac
(
HMACAlgoName
);
resolve
();
expect
(
globalHMAC
!=
null
).
assertTrue
();
console
.
warn
(
"
mac=
"
+
globalHMAC
);
console
.
warn
(
"
HMAC algName is:
"
+
globalHMAC
.
algName
);
console
.
log
(
"
start to call createSymKeyGenerator()
"
);
globalsymKeyGenerator
=
cryptoFramework
.
createSymKeyGenerator
(
keyAlgoName
);
console
.
warn
(
"
symKeyGenerator algName:
"
+
globalsymKeyGenerator
.
algName
);
expect
(
globalsymKeyGenerator
!=
null
).
assertTrue
();
console
.
log
(
"
createSymKeyGenerator ok
"
);
globalsymKeyGenerator
.
generateSymKey
()
.
then
((
key
)
=>
{
expect
(
key
!=
null
).
assertTrue
();
console
.
warn
(
"
generateSymKey ok
"
);
console
.
warn
(
"
key algName:
"
+
key
.
algName
);
console
.
warn
(
"
key format:
"
+
key
.
format
);
var
encodedKey
=
key
.
getEncoded
();
console
.
warn
(
"
key getEncoded hex:
"
+
uInt8ArrayToShowStr
(
encodedKey
.
data
)
);
var
promiseMacInit
=
globalHMAC
.
init
(
key
);
return
promiseMacInit
;
})
.
then
(()
=>
{
try
{
var
promiseMacUpdate
=
globalHMAC
.
update
(
inBlob
);
console
.
log
(
"
promiseMacUpdate =
"
+
promiseMacUpdate
);
}
catch
(
error
)
{
console
.
error
(
"
[Promise]: error code:
"
+
error
.
code
+
"
, message is:
"
+
error
.
message
);
}
})
.
then
(()
=>
{
var
promiseMacdoFinal
=
globalHMAC
.
doFinal
();
console
.
log
(
"
promiseMacdoFinal =
"
+
promiseMacdoFinal
);
return
promiseMacdoFinal
;
})
.
catch
((
err
)
=>
{
console
.
error
(
"
[promise]catch err:
"
+
err
);
reject
(
err
);
});
});
}
export
{
testMDDigestPromise
,
testHMACDigestPromise
,
testHMACDigestPromiseDatablobLong
};
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import
{
expect
}
from
"
@ohos/hypium
"
;
import
cryptoFramework
from
"
@ohos.security.cryptoFramework
"
;
import
{
stringTouInt8Array
,
uInt8ArrayToShowStr
,
uInt8ArrayToString
,
}
from
"
../common/publicDoString
"
;
async
function
testMDDigestPromise
(
MDAlgoName
)
{
var
globalMd
;
var
globalText
=
"
my test data
"
;
return
new
Promise
((
resolve
,
reject
)
=>
{
globalMd
=
cryptoFramework
.
createMd
(
MDAlgoName
);
expect
(
globalMd
!=
null
).
assertTrue
();
console
.
warn
(
"
md=
"
+
globalMd
);
console
.
warn
(
"
MD algName is:
"
+
globalMd
.
algName
);
let
inBlob
=
{
data
:
stringTouInt8Array
(
globalText
),
};
globalMd
.
update
(
inBlob
)
.
then
(()
=>
{
console
.
warn
(
"
[Promise]: update finished
"
);
let
digestBlob
=
globalMd
.
digest
();
return
digestBlob
;
})
.
then
((
mdOutput
)
=>
{
console
.
warn
(
"
[Promise]: digest result1:
"
+
uInt8ArrayToShowStr
(
mdOutput
.
data
)
);
console
.
warn
(
"
[Promise]: digest result2:
"
+
mdOutput
.
data
);
let
mdLen
=
globalMd
.
getMdLength
();
console
.
warn
(
"
Md len:
"
+
mdLen
);
expect
(
mdOutput
!=
null
&&
mdLen
!=
0
&&
mdLen
!=
null
).
assertTrue
();
resolve
();
})
.
catch
((
err
)
=>
{
console
.
error
(
"
[promise]catch err:
"
+
err
);
reject
(
err
);
});
});
}
async
function
testHMACDigestPromise
(
HMACAlgoName
,
keyAlgoName
)
{
var
globalHMAC
;
var
globalText
=
"
my test data
"
;
var
globalsymKeyGenerator
;
var
inBlob
=
{
data
:
stringTouInt8Array
(
globalText
),
};
return
new
Promise
((
resolve
,
reject
)
=>
{
globalHMAC
=
cryptoFramework
.
createMac
(
HMACAlgoName
);
expect
(
globalHMAC
!=
null
).
assertTrue
();
console
.
warn
(
"
mac=
"
+
globalHMAC
);
console
.
warn
(
"
HMAC algName is:
"
+
globalHMAC
.
algName
);
console
.
log
(
"
start to call createSymKeyGenerator()
"
);
globalsymKeyGenerator
=
cryptoFramework
.
createSymKeyGenerator
(
keyAlgoName
);
console
.
warn
(
"
symKeyGenerator algName:
"
+
globalsymKeyGenerator
.
algName
);
expect
(
globalsymKeyGenerator
!=
null
).
assertTrue
();
console
.
log
(
"
createSymKeyGenerator ok
"
);
console
.
warn
(
"
symKeyGenerator algName:
"
+
globalsymKeyGenerator
.
algName
);
globalsymKeyGenerator
.
generateSymKey
()
.
then
((
key
)
=>
{
expect
(
key
!=
null
).
assertTrue
();
console
.
warn
(
"
generateSymKey ok
"
);
console
.
warn
(
"
key algName:
"
+
key
.
algName
);
console
.
warn
(
"
key format:
"
+
key
.
format
);
var
encodedKey
=
key
.
getEncoded
();
console
.
warn
(
"
key getEncoded hex:
"
+
uInt8ArrayToShowStr
(
encodedKey
.
data
)
);
var
promiseMacInit
=
globalHMAC
.
init
(
key
);
return
promiseMacInit
;
})
.
then
(()
=>
{
var
promiseMacUpdate
=
globalHMAC
.
update
(
inBlob
);
return
promiseMacUpdate
;
})
.
then
(()
=>
{
var
promiseMacdoFinal
=
globalHMAC
.
doFinal
();
return
promiseMacdoFinal
;
})
.
then
((
macOutput
)
=>
{
console
.
warn
(
"
HMAC result:
"
+
macOutput
.
data
);
let
macLen
=
globalHMAC
.
getMacLength
();
console
.
warn
(
"
MAC len:
"
+
macLen
);
expect
(
macOutput
!=
null
&&
macLen
!=
0
&&
macLen
!=
null
).
assertTrue
();
resolve
();
})
.
catch
((
err
)
=>
{
console
.
error
(
"
[promise]catch err:
"
+
err
);
reject
(
err
);
});
});
}
export
{
testMDDigestPromise
,
testHMACDigestPromise
};
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录