diff --git a/CryptoArchitectureKit/KeyNegotiation/.gitignore b/CryptoArchitectureKit/KeyNegotiation/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/AppScope/app.json5 b/CryptoArchitectureKit/KeyNegotiation/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..09a0c5dcaecceb27446d187a5e75856d67277d12 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "app": { + "bundleName": "com.samples.keynegotiation", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/CryptoArchitectureKit/KeyNegotiation/AppScope/resources/base/element/string.json b/CryptoArchitectureKit/KeyNegotiation/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..3c9c7b57678a813697ff4469d47366e745d4d44f --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "KeyNegotiation" + } + ] +} diff --git a/CryptoArchitectureKit/KeyNegotiation/AppScope/resources/base/media/app_icon.png b/CryptoArchitectureKit/KeyNegotiation/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/CryptoArchitectureKit/KeyNegotiation/AppScope/resources/base/media/app_icon.png differ diff --git a/CryptoArchitectureKit/KeyNegotiation/README.md b/CryptoArchitectureKit/KeyNegotiation/README.md new file mode 100644 index 0000000000000000000000000000000000000000..eafc26dd0a6d7bb66301541d67f8f49becf5bb9a --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/README.md @@ -0,0 +1,80 @@ +# 密钥协商 + +### 介绍 + +本示例主要展示了使用ECDH进行密钥协商(await方式和同步方式)、使用X25519进行密钥协商(await方式和同步方式)、使用DH进行密钥协商(await方式和同步方式)场景 。该工程中展示的代码详细描述可查如下链接。 + +- [使用ECDH进行密钥协商](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-key-agreement-using-ecdh.md) +- [使用X25519进行密钥协商](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-key-agreement-using-x25519.md) +- [使用DH进行密钥协商](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-key-agreement-using-dh.md) + +### 效果预览 + +| 首页效果图 | 执行结果图 | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| | | + +### 使用说明 + +1. 运行Index主界面。 +2. 页面呈现上述执行结果图效果,点击不同按钮可以跳转到不同功能页面,点击跳转页面中按钮可以执行对应操作,并更新文本内容。 +3. 运行测试用例KeyNegotiation.test.ets文件对页面代码进行测试可以全部通过。 + +### 工程目录 + +``` +entry/src/ + ├── main + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // 密钥协商示例代码 + │ │ ├── DH + │ │ | ├── DHAsync.ets + │ │ | ├── DHSync.ets + │ │ ├── ECDH + │ │ | ├── EDCHAsync.ets + │ │ | ├── EDCHSync.ets + │ │ ├── X25519 + │ │ | ├── X25519Async.ets + │ │ | ├── X25519Sync.ets + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ └── test + │ │ ├── Ability.test.ets + │ │ ├── KeyNegotiation.test.ets // 自动化测试代码 + │ │ └── List.test.ets +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 + +2. HarmonyOS系统:HarmonyOS 5.0.2 Release及以上。 + +3. DevEco Studio版本:DevEco Studio 5.0.2 Release及以上。 + +4. HarmonyOS SDK版本:HarmonyOS 5.0.2 Release及以上。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +```` +git init +git config core.sparsecheckout true +echo CryptoArchitectureKit/KeyNegotiation > .git/info/sparse-checkout +git remote add origin https://gitee.com/harmonyos_samples/guide-snippets.git +git pull origin master +```` \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/build-profile.json5 b/CryptoArchitectureKit/KeyNegotiation/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fb5a1ab654ae6715a0ab69a31900ad26dcf2487f --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/build-profile.json5 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.2(14)", + "targetSdkVersion": "5.0.2(14)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/code-linter.json5 b/CryptoArchitectureKit/KeyNegotiation/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/.gitignore b/CryptoArchitectureKit/KeyNegotiation/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/build-profile.json5 b/CryptoArchitectureKit/KeyNegotiation/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e7569e3056e27af38e9991b7ea73ec10f3ba8a05 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/build-profile.json5 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/hvigorfile.ts b/CryptoArchitectureKit/KeyNegotiation/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/oh-package.json5 b/CryptoArchitectureKit/KeyNegotiation/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c9cb6c8174858277c9b0d465a51547dcab16d5ff --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/entryability/EntryAbility.ets b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..edc2839f203ba057c186e19b0cbbbf80c8faa8b3 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 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 { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 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 { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/DH/DHAsync.ets b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/DH/DHAsync.ets new file mode 100644 index 0000000000000000000000000000000000000000..dacfb015f3f7092a361f23868b8781499a1cbb35 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/DH/DHAsync.ets @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2025 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. + */ +// [Start use_dh_for_key_negotiation_async] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; + +async function dhAwait() { + let keyGen = cryptoFramework.createAsyKeyGenerator('DH_modp1536'); + // 随机生成公私钥对A + let keyPairA = await keyGen.generateKeyPair(); + // 随机生成规格一致的公私钥对B + let keyPairB = await keyGen.generateKeyPair(); + let keyAgreement = cryptoFramework.createKeyAgreement('DH_modp1536'); + // 使用A的公钥和B的私钥进行密钥协商 + let secret1 = await keyAgreement.generateSecret(keyPairB.priKey, keyPairA.pubKey); + // 使用A的私钥和B的公钥进行密钥协商 + let secret2 = await keyAgreement.generateSecret(keyPairA.priKey, keyPairB.pubKey); + // 两种协商的结果应当一致 + if (secret1.data.toString() === secret2.data.toString()) { + console.info('DH success'); + console.info('DH output is ' + secret1.data); + } else { + console.error('DH result is not equal'); + } +} +// [End use_dh_for_key_negotiation_async] + +@Entry +@Component +struct Index { + @State message: string = 'DH Async'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_dh_async')) + .width('70%') + .onClick(async () => { + try { + await dhAwait(); + this.message = 'DH Async Success'; + } catch { + this.message = 'DH Async Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/DH/DHSync.ets b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/DH/DHSync.ets new file mode 100644 index 0000000000000000000000000000000000000000..069442d878df1812e54750c214213d7d7b86477f --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/DH/DHSync.ets @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2025 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. + */ + + +// [Start use_dh_for_key_negotiation_sync] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; + +function dhAwait() { + let keyGen = cryptoFramework.createAsyKeyGenerator('DH_modp1536'); + // 随机生成公私钥对A + let keyPairA = keyGen.generateKeyPairSync(); + // 随机生成规格一致的公私钥对B + let keyPairB = keyGen.generateKeyPairSync(); + let keyAgreement = cryptoFramework.createKeyAgreement('DH_modp1536'); + // 使用A的公钥和B的私钥进行密钥协商 + let secret1 = keyAgreement.generateSecretSync(keyPairB.priKey, keyPairA.pubKey); + // 使用A的私钥和B的公钥进行密钥协商 + let secret2 = keyAgreement.generateSecretSync(keyPairA.priKey, keyPairB.pubKey); + // 两种协商的结果应当一致 + if (secret1.data.toString() === secret2.data.toString()) { + console.info('DH success'); + console.info('DH output is ' + secret1.data); + } else { + console.error('DH result is not equal'); + } +} +// [End use_dh_for_key_negotiation_sync] +@Entry +@Component +struct Index { + @State message: string = 'DH Sync'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_dh_sync')) + .width('70%') + .onClick(() => { + try { + dhAwait(); + this.message = 'DH Sync Success'; + } catch { + this.message = 'DH Async Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/ECDH/EDCHAsync.ets b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/ECDH/EDCHAsync.ets new file mode 100644 index 0000000000000000000000000000000000000000..3431de645b7f994cfa451f866f88d31d48fc45ad --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/ECDH/EDCHAsync.ets @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2025 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. + */ +/// [Start use_ecdh_for_key_negotiation_async] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; + +async function ecdhAwait() { + // 假设此公私钥对数据为外部传入 + let pubKeyArray = + new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, + 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 162, 215, 71, 81, 58, 202, 121, 26, + 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, + 235, 215, 159, 239, 28, 106, 124, 171, 34, 145, 124, 174, 57, 92]); + let priKeyArray = + new Uint8Array([48, 49, 2, 1, 1, 4, 32, 115, 56, 137, 35, 207, 0, 60, 191, 90, 61, 136, 105, 210, 16, 27, 4, 171, + 57, 10, 61, 123, 40, 189, 28, 34, 207, 236, 22, 45, 223, 10, 189, 160, 10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7]); + let eccGen = cryptoFramework.createAsyKeyGenerator('ECC256'); + // 外部传入的公私钥对A + let keyPairA = await eccGen.convertKey({ data: pubKeyArray }, { data: priKeyArray }); + // 内部生成的公私钥对B + let keyPairB = await eccGen.generateKeyPair(); + let eccKeyAgreement = cryptoFramework.createKeyAgreement('ECC256'); + // 使用A的公钥和B的私钥进行密钥协商 + let secret1 = await eccKeyAgreement.generateSecret(keyPairB.priKey, keyPairA.pubKey); + // 使用A的私钥和B的公钥进行密钥协商 + let secret2 = await eccKeyAgreement.generateSecret(keyPairA.priKey, keyPairB.pubKey); + // 两种协商的结果应当一致 + if (secret1.data.toString() === secret2.data.toString()) { + console.info('ecdh success'); + console.info('ecdh output is ' + secret1.data); + } else { + console.error('ecdh result is not equal'); + } +} + +// [End use_ecdh_for_key_negotiation_async] +@Entry +@Component +struct Index { + @State message: string = 'EDCH Async'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_edch_async')) + .width('70%') + .onClick(async () => { + try { + await ecdhAwait(); + this.message = 'EDCH Async Success'; + } catch { + this.message = 'EDCH Async Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/ECDH/EDCHSync.ets b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/ECDH/EDCHSync.ets new file mode 100644 index 0000000000000000000000000000000000000000..0d9af7002498b070b4e1c587b9fccf1dbd99a755 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/ECDH/EDCHSync.ets @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2025 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. + */ + +// [Start use_ecdh_for_key_negotiation_sync] + +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; + +function ecdhAwait() { + // 假设此公私钥对数据为外部传入 + let pubKeyArray = + new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, + 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 162, 215, 71, 81, 58, 202, 121, 26, + 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, + 235, 215, 159, 239, 28, 106, 124, 171, 34, 145, 124, 174, 57, 92]); + let priKeyArray = + new Uint8Array([48, 49, 2, 1, 1, 4, 32, 115, 56, 137, 35, 207, 0, 60, 191, 90, 61, 136, 105, 210, 16, 27, 4, 171, + 57, 10, 61, 123, 40, 189, 28, 34, 207, 236, 22, 45, 223, 10, 189, 160, 10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7]); + let eccGen = cryptoFramework.createAsyKeyGenerator('ECC256'); + // 外部传入的公私钥对A + let keyPairA = eccGen.convertKeySync({ data: pubKeyArray }, { data: priKeyArray }); + // 内部生成的公私钥对B + let keyPairB = eccGen.generateKeyPairSync(); + let eccKeyAgreement = cryptoFramework.createKeyAgreement('ECC256'); + // 使用A的公钥和B的私钥进行密钥协商 + let secret1 = eccKeyAgreement.generateSecretSync(keyPairB.priKey, keyPairA.pubKey); + // 使用A的私钥和B的公钥进行密钥协商 + let secret2 = eccKeyAgreement.generateSecretSync(keyPairA.priKey, keyPairB.pubKey); + // 两种协商的结果应当一致 + if (secret1.data.toString() === secret2.data.toString()) { + console.info('ecdh success'); + console.info('ecdh output is ' + secret1.data); + } else { + console.error('ecdh result is not equal'); + } +} + +// [End use_ecdh_for_key_negotiation_sync] + +@Entry +@Component +struct Index { + @State message: string = 'EDCH Sync'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_edch_sync')) + .width('70%') + .onClick(() => { + try { + ecdhAwait(); + this.message = 'EDCH Sync Success'; + } catch { + this.message = 'EDCH Sync Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/Index.ets b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..b442f1e2603f9c582e2896a8123bd2245f8f8e23 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2025 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 { router } from '@kit.ArkUI'; + +@Entry +@Component +struct Index { + @State message: string = 'KeyNegotiation'; + + goToSample(url: string): void { + router.pushUrl({ + url: url, + }, router.RouterMode.Single, (err) => { + if (err) { + console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); + return; + } + console.info('pushUrl success'); + }) + } + + build() { + Column({ space: 5 }) { + Text(this.message) + .fontSize(25) + .fontWeight(FontWeight.Bold) + Button($r('app.string.ecdh_async')) + .width('70%') + .onClick(() => { + this.goToSample('pages/ECDH/EDCHAsync') + }) + Button($r('app.string.ecdh_sync')) + .width('70%') + .onClick(() => { + this.goToSample('pages/ECDH/EDCHSync') + }) + Button($r('app.string.x25519_async')) + .width('70%') + .onClick(() => { + this.goToSample('pages/X25519/X25519Async') + }) + Button($r('app.string.x25519_sync')) + .width('70%') + .onClick(() => { + this.goToSample('pages/X25519/X25519Sync') + }) + Button($r('app.string.dh_async')) + .width('70%') + .onClick(() => { + this.goToSample('pages/DH/DHAsync') + }) + Button($r('app.string.dh_sync')) + .width('70%') + .onClick(() => { + this.goToSample('pages/DH/DHSync') + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/X25519/X25519Async.ets b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/X25519/X25519Async.ets new file mode 100644 index 0000000000000000000000000000000000000000..bde3bbf28e030da5987bfdd973c0602ef45cef4c --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/X25519/X25519Async.ets @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2025 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. + */ +// [Start use_x25519a_for_key_negotiation_async] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; + +async function x25519Await() { + // 假设此公私钥对数据为外部传入 + let pubKeyArray = + new Uint8Array([48, 42, 48, 5, 6, 3, 43, 101, 110, 3, 33, 0, 36, 98, 216, 106, 74, 99, 179, 203, 81, 145, 147, 101, + 139, 57, 74, 225, 119, 196, 207, 0, 50, 232, 93, 147, 188, 21, 225, 228, 54, 251, 230, 52]); + let priKeyArray = + new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 110, 4, 34, 4, 32, 112, 65, 156, 73, 65, 89, 183, 39, 119, + 229, 110, 12, 192, 237, 186, 153, 21, 122, 28, 176, 248, 108, 22, 242, 239, 179, 106, 175, 85, 65, 214, 90]); + let keyGen = cryptoFramework.createAsyKeyGenerator('X25519'); + // 外部传入的公私钥对A + let keyPairA = await keyGen.convertKey({ data: pubKeyArray }, { data: priKeyArray }); + // 内部生成的公私钥对B + let keyPairB = await keyGen.generateKeyPair(); + let keyAgreement = cryptoFramework.createKeyAgreement('X25519'); + // 使用A的公钥和B的私钥进行密钥协商 + let secret1 = await keyAgreement.generateSecret(keyPairB.priKey, keyPairA.pubKey); + // 使用A的私钥和B的公钥进行密钥协商 + let secret2 = await keyAgreement.generateSecret(keyPairA.priKey, keyPairB.pubKey); + // 两种协商的结果应当一致 + if (secret1.data.toString() === secret2.data.toString()) { + console.info('x25519 success'); + console.info('x25519 output is ' + secret1.data); + } else { + console.error('x25519 result is not equal'); + } +} +// [End use_x25519a_for_key_negotiation_async] +@Entry +@Component +struct Index { + @State message: string = 'X25519 Async'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_x25519_async')) + .width('70%') + .onClick(async () => { + try { + await x25519Await(); + this.message = 'X25519 Async Success'; + } catch { + this.message = 'X25519 Async Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/X25519/X25519Sync.ets b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/X25519/X25519Sync.ets new file mode 100644 index 0000000000000000000000000000000000000000..d4f82b10595400877d580cc0a0cf05e1c8e17ae5 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/ets/pages/X25519/X25519Sync.ets @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2025 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. + */ +// [Start use_x25519a_for_key_negotiation_sync] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; + +function x25519Await() { + // 假设此公私钥对数据为外部传入 + let pubKeyArray = + new Uint8Array([48, 42, 48, 5, 6, 3, 43, 101, 110, 3, 33, 0, 36, 98, 216, 106, 74, 99, 179, 203, 81, 145, 147, 101, + 139, 57, 74, 225, 119, 196, 207, 0, 50, 232, 93, 147, 188, 21, 225, 228, 54, 251, 230, 52]); + let priKeyArray = + new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 110, 4, 34, 4, 32, 112, 65, 156, 73, 65, 89, 183, 39, 119, + 229, 110, 12, 192, 237, 186, 153, 21, 122, 28, 176, 248, 108, 22, 242, 239, 179, 106, 175, 85, 65, 214, 90]); + let keyGen = cryptoFramework.createAsyKeyGenerator('X25519'); + // 外部传入的公私钥对A + let keyPairA = keyGen.convertKeySync({ data: pubKeyArray }, { data: priKeyArray }); + // 内部生成的公私钥对B + let keyPairB = keyGen.generateKeyPairSync(); + let keyAgreement = cryptoFramework.createKeyAgreement('X25519'); + // 使用A的公钥和B的私钥进行密钥协商 + let secret1 = keyAgreement.generateSecretSync(keyPairB.priKey, keyPairA.pubKey); + // 使用A的私钥和B的公钥进行密钥协商 + let secret2 = keyAgreement.generateSecretSync(keyPairA.priKey, keyPairB.pubKey); + // 两种协商的结果应当一致 + if (secret1.data.toString() === secret2.data.toString()) { + console.info('x25519 success'); + console.info('x25519 output is ' + secret1.data); + } else { + console.error('x25519 result is not equal'); + } +} +// [End use_x25519a_for_key_negotiation_sync] +@Entry +@Component +struct Index { + @State message: string = 'X25519 Sync'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_x25519_sync')) + .width('70%') + .onClick(() => { + try { + x25519Await(); + this.message = 'X25519 Sync Success'; + } catch { + this.message = 'X25519 Sync Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/module.json5 b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4144486d1af4c03b0d767cce1cda86fc0d697f91 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/element/color.json b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/element/string.json b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..f59c4772c2529c78062eeb4195ae015c360bef0e --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/element/string.json @@ -0,0 +1,64 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "KeyNegotiation" + }, + { + "name": "ecdh_async", + "value": "ECDH Async" + }, + { + "name": "ecdh_sync", + "value": "ECDH Sync" + }, + { + "name": "x25519_async", + "value": "X25519 Async" + }, + { + "name": "x25519_sync", + "value": "X25519 Sync" + }, + { + "name": "dh_async", + "value": "DH Async" + }, + { + "name": "dh_sync", + "value": "DH Sync" + }, + { + "name": "call_dh_async", + "value": "Call DH Async" + }, + { + "name": "call_dh_sync", + "value": "Call DH Sync" + }, + { + "name": "call_edch_async", + "value": "Call EDCH Async" + }, + { + "name": "call_edch_sync", + "value": "Call EDCH Sync" + }, + { + "name": "call_x25519_async", + "value": "Call X25519 Async" + }, + { + "name": "call_x25519_sync", + "value": "Call X25519 Sync" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/media/background.png b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/media/background.png differ diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/media/foreground.png b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/media/foreground.png differ diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/media/layered_image.json b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/media/startIcon.png b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/media/startIcon.png differ diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/profile/backup_config.json b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/profile/main_pages.json b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..b1fc5cb1e85d1a5f47b04ae534a4edfaaa5422bb --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,11 @@ +{ + "src": [ + "pages/Index", + "pages/DH/DHAsync", + "pages/DH/DHSync", + "pages/ECDH/EDCHAsync", + "pages/ECDH/EDCHSync", + "pages/X25519/X25519Async", + "pages/X25519/X25519Sync" + ] +} diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/dark/element/color.json b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/ohosTest/ets/test/Ability.test.ets b/CryptoArchitectureKit/KeyNegotiation/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0f8ce9a2c012f8fe36114cef65216ef0b6254f41 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 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 { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/ohosTest/ets/test/KeyNegotiation.test.ets b/CryptoArchitectureKit/KeyNegotiation/entry/src/ohosTest/ets/test/KeyNegotiation.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..cece2ce32b52c65d1788244bf3dd75ab77f00fd2 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/ohosTest/ets/test/KeyNegotiation.test.ets @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2025 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 { describe, it, expect, beforeAll } from '@ohos/hypium'; +// 导入测试依赖kit +import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit'; +import { UIAbility, Want } from '@kit.AbilityKit'; + +const delegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); +const bundleName = abilityDelegatorRegistry.getArguments().bundleName; +let want: Want; + +export default function keyNegotiationTest() { + describe('keyNegotiationTest', () => { + /** + * 打开应用 + */ + beforeAll(async () => { + want = { + bundleName: bundleName, + abilityName: 'EntryAbility' + }; + await delegator.startAbility(want); + const driver = Driver.create(); + await driver.delayMs(1000); + const ability: UIAbility = await delegator.getCurrentTopAbility(); + console.info('get top ability'); + await driver.delayMs(1000); + expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); + }) + + /** + * 点击按钮,使用ECDH进行密钥协商(await方式) + */ + it('testKeyNegotiation001', 0, async () => { + console.info('uitest: testKeyNegotiation001 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('ECDH Async')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call EDCH Async')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('EDCH Async Success')); + console.info('uitest: testKeyNegotiation001 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用ECDH进行密钥协商(同步方式) + */ + it('testKeyNegotiation002', 0, async () => { + console.info('uitest: testKeyNegotiation002 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('ECDH Sync')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call EDCH Sync')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('EDCH Sync Success')); + console.info('uitest: testKeyNegotiation002 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用X25519进行密钥协商(await方式) + */ + it('testKeyNegotiation003', 0, async () => { + console.info('uitest: testKeyNegotiation003 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('X25519 Async')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call X25519 Async')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('X25519 Async Success')); + console.info('uitest: testKeyNegotiation003 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用X25519进行密钥协商(同步方式) + */ + it('testKeyNegotiation004', 0, async () => { + console.info('uitest: testKeyNegotiation004 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('X25519 Sync')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call X25519 Sync')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('X25519 Sync Success')); + console.info('uitest: testKeyNegotiation004 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用DH进行密钥协商(await方式) + */ + it('testKeyNegotiation005', 0, async () => { + console.info('uitest: testKeyNegotiation005 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('DH Async')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call DH Async')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('DH Async Success')); + console.info('uitest: testKeyNegotiation005 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,使用DH进行密钥协商(同步方式) + */ + it('testKeyNegotiation006', 0, async () => { + console.info('uitest: testKeyNegotiation006 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('DH Sync')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call DH Sync')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('DH Sync Success')); + console.info('uitest: testKeyNegotiation006 end'); + await driver.pressBack(); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/ohosTest/ets/test/List.test.ets b/CryptoArchitectureKit/KeyNegotiation/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..6cb0bf87cc7cfbc0e3a6d55f9996c94bc163df3b --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 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 abilityTest from './Ability.test'; +import keyNegotiationTest from './KeyNegotiation.test'; + +export default function testsuite() { + abilityTest(); + keyNegotiationTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/ohosTest/module.json5 b/CryptoArchitectureKit/KeyNegotiation/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c3fd9dda3040d888d9d8b0b62bcb5d3b6fbeb614 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/test/List.test.ets b/CryptoArchitectureKit/KeyNegotiation/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 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 localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/entry/src/test/LocalUnit.test.ets b/CryptoArchitectureKit/KeyNegotiation/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/hvigor/hvigor-config.json5 b/CryptoArchitectureKit/KeyNegotiation/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/CryptoArchitectureKit/KeyNegotiation/hvigorfile.ts b/CryptoArchitectureKit/KeyNegotiation/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/KeyNegotiation/oh-package.json5 b/CryptoArchitectureKit/KeyNegotiation/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/CryptoArchitectureKit/KeyNegotiation/ohosTest.md b/CryptoArchitectureKit/KeyNegotiation/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..b082027408632e0de31dc3026ef368ed62c62c89 --- /dev/null +++ b/CryptoArchitectureKit/KeyNegotiation/ohosTest.md @@ -0,0 +1,13 @@ +# 密钥协商测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ------------------------------------------- | ------------ | ---- | -------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 点击按钮,使用ECDH进行密钥协商(await方式) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用ECDH进行密钥协商(同步方式) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用X25519进行密钥协商(await方式) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用X25519进行密钥协商(同步方式) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用DH进行密钥协商(await方式) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,使用DH进行密钥协商(同步方式) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | \ No newline at end of file diff --git a/CryptoArchitectureKit/KeyNegotiation/screenshots/KeyNegotiation1.png b/CryptoArchitectureKit/KeyNegotiation/screenshots/KeyNegotiation1.png new file mode 100644 index 0000000000000000000000000000000000000000..5db6f1847ef3594efacf0c9a15a6a6a6183f1068 Binary files /dev/null and b/CryptoArchitectureKit/KeyNegotiation/screenshots/KeyNegotiation1.png differ diff --git a/CryptoArchitectureKit/KeyNegotiation/screenshots/KeyNegotiation2.png b/CryptoArchitectureKit/KeyNegotiation/screenshots/KeyNegotiation2.png new file mode 100644 index 0000000000000000000000000000000000000000..f4ae2d8e94f2ef43f810cbf3b0bd9d5947922799 Binary files /dev/null and b/CryptoArchitectureKit/KeyNegotiation/screenshots/KeyNegotiation2.png differ diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/.gitignore b/CryptoArchitectureKit/MessageAuthenticationCode/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/AppScope/app.json5 b/CryptoArchitectureKit/MessageAuthenticationCode/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..78f9ea61a085288da1707f7bec35a0a437567f6b --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "app": { + "bundleName": "com.samples.messageauthenticationcode", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/AppScope/resources/base/element/string.json b/CryptoArchitectureKit/MessageAuthenticationCode/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..230944b974465b50915c796506ba59d301a9893a --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "MessageAuthenticationCode" + } + ] +} diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/AppScope/resources/base/media/app_icon.png b/CryptoArchitectureKit/MessageAuthenticationCode/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/CryptoArchitectureKit/MessageAuthenticationCode/AppScope/resources/base/media/app_icon.png differ diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/README.md b/CryptoArchitectureKit/MessageAuthenticationCode/README.md new file mode 100644 index 0000000000000000000000000000000000000000..8a8dbcd24791df38b0d70136f4a3ebed309f9b2e --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/README.md @@ -0,0 +1,75 @@ +# 消息认证码计算 + +### 介绍 + +本示例主要展示了消息认证码计算(HMAC、分段HMAC)场景 。该工程中展示的代码详细描述可查如下链接。 + +- [消息认证码计算](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-compute-mac.md) + +### 效果预览 + +| 首页效果图 | 执行结果图 | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| | | + +### 使用说明 + +1. 运行Index主界面。 +2. 页面呈现上述执行结果图效果,点击不同按钮可以跳转到不同功能页面,点击跳转页面中按钮可以执行对应操作,并更新文本内容。 +3. 运行测试用例MessageAuthenticationCode.test.ets文件对页面代码进行测试可以全部通过。 + +### 工程目录 + +``` +entry/src/ + ├── main + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // 消息认证码计算示例代码 + │ │ ├── HMACSegmentation + │ │ | ├── Async.ets + │ │ | ├── Sync.ets + │ │ ├── HMACSingleTime + │ │ | ├── Async.ets + │ │ | ├── Sync.ets + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ └── test + │ │ ├── Ability.test.ets + │ │ ├── MessageAuthenticationCode.test.ets // 自动化测试代码 + │ │ └── List.test.ets +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 + +2. HarmonyOS系统:HarmonyOS 5.0.2 Release及以上。 + +3. DevEco Studio版本:DevEco Studio 5.0.2 Release及以上。 + +4. HarmonyOS SDK版本:HarmonyOS 5.0.2 Release及以上。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +```` +git init +git config core.sparsecheckout true +echo CryptoArchitectureKit/MessageAuthenticationCode > .git/info/sparse-checkout +git remote add origin https://gitee.com/harmonyos_samples/guide-snippets.git +git pull origin master +```` \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/build-profile.json5 b/CryptoArchitectureKit/MessageAuthenticationCode/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fb5a1ab654ae6715a0ab69a31900ad26dcf2487f --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/build-profile.json5 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.2(14)", + "targetSdkVersion": "5.0.2(14)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/code-linter.json5 b/CryptoArchitectureKit/MessageAuthenticationCode/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/.gitignore b/CryptoArchitectureKit/MessageAuthenticationCode/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/build-profile.json5 b/CryptoArchitectureKit/MessageAuthenticationCode/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e7569e3056e27af38e9991b7ea73ec10f3ba8a05 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/build-profile.json5 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/hvigorfile.ts b/CryptoArchitectureKit/MessageAuthenticationCode/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/oh-package.json5 b/CryptoArchitectureKit/MessageAuthenticationCode/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c9cb6c8174858277c9b0d465a51547dcab16d5ff --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/entryability/EntryAbility.ets b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..edc2839f203ba057c186e19b0cbbbf80c8faa8b3 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 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 { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 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 { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/HMACSegmentation/Async.ets b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/HMACSegmentation/Async.ets new file mode 100644 index 0000000000000000000000000000000000000000..b74165d49ccf1911341edbe03ccf937960943fc1 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/HMACSegmentation/Async.ets @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2025 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. + */ + +// [Start message_authentication_code_calculation_hmac_one_time_incoming] + +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +async function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let aesGenerator = cryptoFramework.createSymKeyGenerator('HMAC'); + let symKey = await aesGenerator.convertKey(symKeyBlob); + console.info('convertKey success'); + return symKey; +} + +async function doHmac() { + // 把字符串按utf-8解码为Uint8Array,使用固定的128位的密钥,即16字节 + let keyData = new Uint8Array(buffer.from('12345678abcdefgh', 'utf-8').buffer); + let key = await genSymKeyByData(keyData); + let macAlgName = 'SHA256'; // 摘要算法名 + let message = 'hmacTestMessgae'; // 待进行HMAC的数据 + let mac = cryptoFramework.createMac(macAlgName); + await mac.init(key); + // 数据量较少时,可以只做一次update,将数据全部传入,接口未对入参长度做限制 + await mac.update({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }); + let macResult = await mac.doFinal(); + console.info('HMAC result:' + macResult.data); + let macLen = mac.getMacLength(); + console.info('HMAC len:' + macLen); +} + +// [End message_authentication_code_calculation_hmac_one_time_incoming] + +@Entry +@Component +struct Index { + @State message: string = 'Segmentation Async'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_segmentation_async')) + .width('70%') + .onClick(async () => { + try { + await doHmac(); + this.message = 'Segmentation Async Success'; + } catch { + this.message = 'Segmentation Async Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/HMACSegmentation/Sync.ets b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/HMACSegmentation/Sync.ets new file mode 100644 index 0000000000000000000000000000000000000000..de5f90cfbbfa908022e6767180b945a206e3978b --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/HMACSegmentation/Sync.ets @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2025 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. + */ +// [Start message_authentication_code_calculation_sync_one_time_incoming] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let aesGenerator = cryptoFramework.createSymKeyGenerator('HMAC'); + let symKey = aesGenerator.convertKeySync(symKeyBlob); + console.info('[Sync]convertKey success'); + return symKey; +} + +function doHmacBySync() { + // 把字符串按utf-8解码为Uint8Array,使用固定的128位的密钥,即16字节 + let keyData = new Uint8Array(buffer.from('12345678abcdefgh', 'utf-8').buffer); + let key = genSymKeyByData(keyData); + let macAlgName = 'SHA256'; // 摘要算法名 + let message = 'hmacTestMessgae'; // 待进行HMAC的数据 + let mac = cryptoFramework.createMac(macAlgName); + mac.initSync(key); + // 数据量较少时,可以只做一次update,将数据全部传入,接口未对入参长度做限制 + mac.updateSync({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }); + let macResult = mac.doFinalSync(); + console.info('[Sync]HMAC result:' + macResult.data); + let macLen = mac.getMacLength(); + console.info('HMAC len:' + macLen); +} + +// [End message_authentication_code_calculation_sync_one_time_incoming] + +@Entry +@Component +struct Index { + @State message: string = 'Segmentation Sync'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_segmentation_sync')) + .width('70%') + .onClick(() => { + try { + doHmacBySync(); + this.message = 'Segmentation Sync Success'; + } catch { + this.message = 'Segmentation Sync Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/HMACSingleTime/Async.ets b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/HMACSingleTime/Async.ets new file mode 100644 index 0000000000000000000000000000000000000000..86e933b7a0b67cbc4c39a6638d03d4ad9fb81cdd --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/HMACSingleTime/Async.ets @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2025 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. + */ +// [Start message_authentication_code_calculated_as_fragmented_hmac_async] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +async function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let aesGenerator = cryptoFramework.createSymKeyGenerator('HMAC'); + let symKey = await aesGenerator.convertKey(symKeyBlob); + console.info('convertKey success'); + return symKey; +} + +async function doLoopHmac() { + // 把字符串按utf-8解码为Uint8Array,使用固定的128位的密钥,即16字节 + let keyData = new Uint8Array(buffer.from('12345678abcdefgh', 'utf-8').buffer); + let key = await genSymKeyByData(keyData); + let macAlgName = 'SHA256'; // 摘要算法名 + let mac = cryptoFramework.createMac(macAlgName); + // 假设信息总共43字节,根据utf-8解码后,也是43字节 + let messageText = 'aaaaa.....bbbbb.....ccccc.....ddddd.....eee'; + let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer); + let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 + await mac.init(key); + for (let i = 0; i < messageData.length; i += updateLength) { + let updateMessage = messageData.subarray(i, i + updateLength); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + await mac.update(updateMessageBlob); + } + let macOutput = await mac.doFinal(); + console.info('HMAC result: ' + macOutput.data); + let macLen = mac.getMacLength(); + console.info('HMAC len:' + macLen); +} + + +// [End message_authentication_code_calculated_as_fragmented_hmac_async] +@Entry +@Component +struct Index { + @State message: string = 'SingleTime Async'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_single_time_async')) + .width('70%') + .onClick(async () => { + try { + await doLoopHmac(); + this.message = 'SingleTime Async Success'; + } catch { + this.message = 'SingleTime Async Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/HMACSingleTime/Sync.ets b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/HMACSingleTime/Sync.ets new file mode 100644 index 0000000000000000000000000000000000000000..caa9d1ba84e8380197d2774421ba9abcb312d8f4 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/HMACSingleTime/Sync.ets @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2025 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. + */ +// [Start message_authentication_code_calculated_as_fragmented_hmac_sync] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function genSymKeyByData(symKeyData: Uint8Array) { + let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; + let aesGenerator = cryptoFramework.createSymKeyGenerator('HMAC'); + let symKey = aesGenerator.convertKeySync(symKeyBlob); + console.info('[Sync]convertKey success'); + return symKey; +} + +function doLoopHmacBySync() { + // 把字符串按utf-8解码为Uint8Array,使用固定的128位的密钥,即16字节 + let keyData = new Uint8Array(buffer.from('12345678abcdefgh', 'utf-8').buffer); + let key = genSymKeyByData(keyData); + let macAlgName = 'SHA256'; // 摘要算法名 + let mac = cryptoFramework.createMac(macAlgName); + // 假设信息总共43字节,根据utf-8解码后,也是43字节 + let messageText = 'aaaaa.....bbbbb.....ccccc.....ddddd.....eee'; + let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer); + let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 + mac.initSync(key); + for (let i = 0; i < messageData.length; i += updateLength) { + let updateMessage = messageData.subarray(i, i + updateLength); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + mac.updateSync(updateMessageBlob); + } + let macOutput = mac.doFinalSync(); + console.info('[Sync]HMAC result: ' + macOutput.data); + let macLen = mac.getMacLength(); + console.info('HMAC len:' + macLen); +} + + +// [End message_authentication_code_calculated_as_fragmented_hmac_sync] +@Entry +@Component +struct Index { + @State message: string = 'SingleTime Sync'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_single_time_sync')) + .width('70%') + .onClick(() => { + try { + doLoopHmacBySync(); + this.message = 'SingleTime Sync Success'; + } catch { + this.message = 'SingleTime Sync Fail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/Index.ets b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..5850b8c94c87c46bcefece4a5c3f9d4109896cdb --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2025 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 { router } from '@kit.ArkUI'; + +@Entry +@Component +struct Index { + @State message: string = 'MessageAuthenticationCode'; + + goToSample(url: string): void { + router.pushUrl({ + url: url, + }, router.RouterMode.Single, (err) => { + if (err) { + console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); + return; + } + console.info('pushUrl success'); + }) + } + + build() { + Column({ space: 5 }) { + Text(this.message) + .fontSize(25) + .fontWeight(FontWeight.Bold) + Button($r('app.string.segmentation_async')) + .width('70%') + .onClick(() => { + this.goToSample('pages/HMACSegmentation/Async') + }) + Button($r('app.string.segmentation_sync')) + .width('70%') + .onClick(() => { + this.goToSample('pages/HMACSegmentation/Sync') + }) + Button($r('app.string.single_time_async')) + .width('70%') + .onClick(() => { + this.goToSample('pages/HMACSingleTime/Async') + }) + Button($r('app.string.single_time_sync')) + .width('70%') + .onClick(() => { + this.goToSample('pages/HMACSingleTime/Sync') + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/module.json5 b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4144486d1af4c03b0d767cce1cda86fc0d697f91 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/element/color.json b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/element/string.json b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..c038f95733bc29c8a4f422555d626873f358c84e --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/element/string.json @@ -0,0 +1,48 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "MessageAuthenticationCode" + }, + { + "name": "segmentation_async", + "value": "Segmentation Async" + }, + { + "name": "segmentation_sync", + "value": "Segmentation Sync" + }, + { + "name": "single_time_async", + "value": "SingleTime Async" + }, + { + "name": "single_time_sync", + "value": "SingleTime Sync" + }, + { + "name": "call_segmentation_async", + "value": "Call Segmentation Async" + }, + { + "name": "call_segmentation_sync", + "value": "Call Segmentation Sync" + }, + { + "name": "call_single_time_async", + "value": "Call SingleTime Async" + }, + { + "name": "call_single_time_sync", + "value": "Call SingleTime Sync" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/media/background.png b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/media/background.png differ diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/media/foreground.png b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/media/foreground.png differ diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/media/layered_image.json b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/media/startIcon.png b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/media/startIcon.png differ diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/profile/backup_config.json b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/profile/main_pages.json b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..f38fb90901f6245b8ee7de2ab84dbd18f580cfd3 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,9 @@ +{ + "src": [ + "pages/Index", + "pages/HMACSegmentation/Async", + "pages/HMACSegmentation/Sync", + "pages/HMACSingleTime/Async", + "pages/HMACSingleTime/Sync" + ] +} diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/dark/element/color.json b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/ohosTest/ets/test/Ability.test.ets b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0f8ce9a2c012f8fe36114cef65216ef0b6254f41 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 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 { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/ohosTest/ets/test/List.test.ets b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..ec00a06338a7662913c0b56bbabdf84fc8155a79 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 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 abilityTest from './Ability.test'; +import messageAuthenticationCodeTest from './MessageAuthenticationCode.test'; + +export default function testsuite() { + abilityTest(); + messageAuthenticationCodeTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/ohosTest/ets/test/MessageAuthenticationCode.test.ets b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/ohosTest/ets/test/MessageAuthenticationCode.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1c094db7b422bf1ad90f7fcc234f13d28fb15468 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/ohosTest/ets/test/MessageAuthenticationCode.test.ets @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2025 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 { describe, it, expect, beforeAll } from '@ohos/hypium'; +// 导入测试依赖kit +import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit'; +import { UIAbility, Want } from '@kit.AbilityKit'; + +const delegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); +const bundleName = abilityDelegatorRegistry.getArguments().bundleName; +let want: Want; + +export default function messageAuthenticationCodeTest() { + describe('messageAuthenticationCodeTest', () => { + /** + * 打开应用 + */ + beforeAll(async () => { + want = { + bundleName: bundleName, + abilityName: 'EntryAbility' + }; + await delegator.startAbility(want); + const driver = Driver.create(); + await driver.delayMs(1000); + const ability: UIAbility = await delegator.getCurrentTopAbility(); + console.info('get top ability'); + await driver.delayMs(1000); + expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); + }) + + /** + * 点击按钮,消息认证码计算(HMAC await方式) + */ + it('testMessageAuthenticationCode001', 0, async () => { + console.info('uitest: testMessageAuthenticationCode001 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Segmentation Async')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call Segmentation Async')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Segmentation Async Success')); + console.info('uitest: testMessageAuthenticationCode001 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,消息认证码计算(HMAC 同步方式) + */ + it('testMessageAuthenticationCode002', 0, async () => { + console.info('uitest: testMessageAuthenticationCode002 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Segmentation Sync')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call Segmentation Sync')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Segmentation Sync Success')); + console.info('uitest: testMessageAuthenticationCode002 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,消息认证码计算(分段HMAC await方式) + */ + it('testMessageAuthenticationCode003', 0, async () => { + console.info('uitest: testMessageAuthenticationCode003 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('SingleTime Async')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SingleTime Async')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SingleTime Async Success')); + console.info('uitest: testMessageAuthenticationCode003 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,消息认证码计算(分段HMAC 同步方式) + */ + it('testMessageAuthenticationCode004', 0, async () => { + console.info('uitest: testMessageAuthenticationCode004 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('SingleTime Sync')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SingleTime Sync')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SingleTime Sync Success')); + console.info('uitest: testMessageAuthenticationCode004 end'); + await driver.pressBack(); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/ohosTest/module.json5 b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c3fd9dda3040d888d9d8b0b62bcb5d3b6fbeb614 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/test/List.test.ets b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 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 localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/test/LocalUnit.test.ets b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/hvigor/hvigor-config.json5 b/CryptoArchitectureKit/MessageAuthenticationCode/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/hvigorfile.ts b/CryptoArchitectureKit/MessageAuthenticationCode/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/oh-package.json5 b/CryptoArchitectureKit/MessageAuthenticationCode/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/ohosTest.md b/CryptoArchitectureKit/MessageAuthenticationCode/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..997924c6832a78fa7ff948d55fd5dd219d12605d --- /dev/null +++ b/CryptoArchitectureKit/MessageAuthenticationCode/ohosTest.md @@ -0,0 +1,11 @@ +# 消息认证码计算测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| -------------------------------------------- | ------------ | ---- | -------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 点击按钮,消息认证码计算(HMAC await方式) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,消息认证码计算(HMAC 同步方式) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,消息认证码计算(分段HMAC await方式) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,消息认证码计算(分段HMAC 同步方式) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/screenshots/MessageAuthenticationCode1.png b/CryptoArchitectureKit/MessageAuthenticationCode/screenshots/MessageAuthenticationCode1.png new file mode 100644 index 0000000000000000000000000000000000000000..5ffe8253520904c7bc3cd3ef4b1feceeca5eb2e4 Binary files /dev/null and b/CryptoArchitectureKit/MessageAuthenticationCode/screenshots/MessageAuthenticationCode1.png differ diff --git a/CryptoArchitectureKit/MessageAuthenticationCode/screenshots/MessageAuthenticationCode2.png b/CryptoArchitectureKit/MessageAuthenticationCode/screenshots/MessageAuthenticationCode2.png new file mode 100644 index 0000000000000000000000000000000000000000..cc6e3b708f2c2cc03cdb9df536ae076b7ee3a607 Binary files /dev/null and b/CryptoArchitectureKit/MessageAuthenticationCode/screenshots/MessageAuthenticationCode2.png differ diff --git a/CryptoArchitectureKit/MessageDigestComputation/.gitignore b/CryptoArchitectureKit/MessageDigestComputation/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/AppScope/app.json5 b/CryptoArchitectureKit/MessageDigestComputation/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..67ca91640924a02245b97f443f27d99761c11769 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "app": { + "bundleName": "com.samples.messagedigestcomputation", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/CryptoArchitectureKit/MessageDigestComputation/AppScope/resources/base/element/string.json b/CryptoArchitectureKit/MessageDigestComputation/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..8b289a1aceb74578de2d3518de476fe19489fa89 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "MessageDigestComputation" + } + ] +} diff --git a/CryptoArchitectureKit/MessageDigestComputation/AppScope/resources/base/media/app_icon.png b/CryptoArchitectureKit/MessageDigestComputation/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/CryptoArchitectureKit/MessageDigestComputation/AppScope/resources/base/media/app_icon.png differ diff --git a/CryptoArchitectureKit/MessageDigestComputation/README.md b/CryptoArchitectureKit/MessageDigestComputation/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9c25a35d92548aa467a1bcc68feded26659aad1d --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/README.md @@ -0,0 +1,87 @@ +# 消息摘要计算 + +### 介绍 + +本示例主要展示了消息摘要计算(ArkTS)、消息摘要计算(C/C++)场景 。该工程中展示的代码详细描述可查如下链接。 + +- [消息摘要计算(ArkTS)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-generate-message-digest.md) +- [消息摘要计算(C/C++)](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/CryptoArchitectureKit/crypto-generate-message-digest-ndk.md) + +### 效果预览 + +| 首页效果图 | 执行结果图 | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| | | + +### 使用说明 + +1. 运行Index主界面。 +2. 页面呈现上述执行结果图效果,点击不同按钮可以跳转到不同功能页面,点击跳转页面中按钮可以执行对应操作,并更新文本内容。 +3. 运行测试用例MessageDigestComputation.test.ets文件对页面代码进行测试可以全部通过。 + +### 工程目录 + +``` +entry/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ | ├── libentry + │ │ | ├── index.d.ts + │ │ | ├── oh-package.json5 + │ │ | ├── project + │ │ | ├── segmentation.cpp + │ │ | ├── singleTime.cpp + │ │ | ├── file.h + │ │ ├── CMakeList.txt + │ │ ├── napi_init.cpp + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // 消息摘要计算示例代码 + │ │ ├── segmentation + │ │ | ├── SegmentationAsync.ets + │ │ | ├── SegmentationSync.ets + │ │ ├── singleTime + │ │ | ├── SingleTimeAsync.ets + │ │ | ├── SingleTimeSync.ets + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ └── test + │ │ ├── Ability.test.ets + │ │ ├── MessageDigestComputation.test.ets // 自动化测试代码 + │ │ └── List.test.ets +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:华为手机。 + +2. HarmonyOS系统:HarmonyOS 5.0.2 Release及以上。 + +3. DevEco Studio版本:DevEco Studio 5.0.2 Release及以上。 + +4. HarmonyOS SDK版本:HarmonyOS 5.0.2 Release及以上。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +```` +git init +git config core.sparsecheckout true +echo code/DocsSample/Security/CryptoArchitectureKit/MessageDigestComputation > .git/info/sparse-checkout +git remote add origin https://gitee.com/harmonyos_samples/guide-snippets.git +git pull origin master +```` \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/build-profile.json5 b/CryptoArchitectureKit/MessageDigestComputation/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fb5a1ab654ae6715a0ab69a31900ad26dcf2487f --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/build-profile.json5 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.2(14)", + "targetSdkVersion": "5.0.2(14)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/code-linter.json5 b/CryptoArchitectureKit/MessageDigestComputation/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/.gitignore b/CryptoArchitectureKit/MessageDigestComputation/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/build-profile.json5 b/CryptoArchitectureKit/MessageDigestComputation/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..31862257476bc8403e59fa42b4c924974d7dbe03 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/build-profile.json5 @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + "abiFilters": [ + "arm64-v8a", + "x86_64", + "armeabi-v7a" + ] + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/hvigorfile.ts b/CryptoArchitectureKit/MessageDigestComputation/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/oh-package.json5 b/CryptoArchitectureKit/MessageDigestComputation/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..118bdd4fe7699368a010e04c24f5bfc887cf1298 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libentry.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/CMakeLists.txt b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..e9e4d0405dc9cf2d466184b43eb662b8cee8b018 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/CMakeLists.txt @@ -0,0 +1,15 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(MessageDigestComputation) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +if(DEFINED PACKAGE_FIND_FILE) + include(${PACKAGE_FIND_FILE}) +endif() + +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(entry SHARED napi_init.cpp types/project/segmentation.cpp types/project/singleTime.cpp) +target_link_libraries(entry PUBLIC libace_napi.z.so libohcrypto.so) \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/napi_init.cpp b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/napi_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87ee66888a1b1e1735d66f93fd0c9574c4229573 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/napi_init.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 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. + */ + +#include "napi/native_api.h" +#include "types/project/file.h" + +static napi_value NAPI_Global_doTestMd(napi_env env, napi_callback_info info) +{ + napi_value ret; + napi_create_int32(env, doTestMd(), &ret); + return ret; +} +static napi_value NAPI_Global_doLoopMd(napi_env env, napi_callback_info info) +{ + napi_value ret; + napi_create_int32(env, doLoopMd(), &ret); + return ret; +} +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + {"doTestMd", nullptr, NAPI_Global_doTestMd, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"doLoopMd", nullptr, NAPI_Global_doLoopMd, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "entry", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/libentry/index.d.ts b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/libentry/index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..8d3539298b95d72f368d0d11392941493db862f2 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/libentry/index.d.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2025 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. + */ + +export const doTestMd: () => number; + +export const doLoopMd: () => number; \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/libentry/oh-package.json5 b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2826cc7d6bd199c1008bb51d898dffa922201e6c --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "name": "libentry.so", + "types": "./index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/project/file.h b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/project/file.h new file mode 100644 index 0000000000000000000000000000000000000000..4e6a15a13ede84884b3a90aca8f5eb0399e2ce4a --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/project/file.h @@ -0,0 +1,23 @@ +#ifndef FILE_H +#define FILE_H +/* + * Copyright (c) 2025 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. + */ + +#include "CryptoArchitectureKit/crypto_common.h" + +OH_Crypto_ErrCode doTestMd(); +OH_Crypto_ErrCode doLoopMd(); + +#endif // FILE_H \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/project/segmentation.cpp b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/project/segmentation.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3900c7b7ba07114b5b2479015bbc4fa4e5b96191 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/project/segmentation.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2025 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. + */ + +// [Start message_digest_calculation_segmented_digest_algorithm] + +#include +#include "CryptoArchitectureKit/crypto_common.h" +#include "CryptoArchitectureKit/crypto_digest.h" +#include "file.h" +#define OH_CRYPTO_DIGEST_DATA_MAX (1024 * 1024 * 100) + +static constexpr int INT_640 = 640; + +OH_Crypto_ErrCode doLoopMd() +{ + OH_Crypto_ErrCode ret; + OH_CryptoDigest *ctx = nullptr; + uint8_t *testData = (uint8_t *)malloc(OH_CRYPTO_DIGEST_DATA_MAX); + if (testData == nullptr) { + return CRYPTO_MEMORY_ERROR; + } + Crypto_DataBlob out = {.data = nullptr, .len = 0}; + int mdLen = 0; + int isBlockSize = 20; + int offset = 0; + + ret = OH_CryptoDigest_Create("SHA256", &ctx); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + do { + for (int i = 0; i < INT_640 / isBlockSize; i++) { + Crypto_DataBlob in = {.data = reinterpret_cast(testData + offset), + .len = static_cast(isBlockSize)}; + ret = OH_CryptoDigest_Update(ctx, &in); + if (ret != CRYPTO_SUCCESS) { + break; + } + offset += isBlockSize; + } + ret = OH_CryptoDigest_Final(ctx, &out); + if (ret != CRYPTO_SUCCESS) { + break; + } + mdLen = OH_CryptoDigest_GetLength(ctx); + } while (0); + OH_Crypto_FreeDataBlob(&out); + OH_DigestCrypto_Destroy(ctx); + return ret; +} + +// [End message_digest_calculation_segmented_digest_algorithm] \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/project/singleTime.cpp b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/project/singleTime.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3776b2b61f7ad9477f7b548d02c33c4314db82f --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/cpp/types/project/singleTime.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2025 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. + */ + +// [Start message_digest_calculation_digest_algorithm_one_time_incoming] + +#include "CryptoArchitectureKit/crypto_common.h" +#include "CryptoArchitectureKit/crypto_digest.h" +#include +#include "file.h" + +OH_Crypto_ErrCode doTestMd() +{ + OH_Crypto_ErrCode ret; + OH_CryptoDigest *ctx = nullptr; + char *testData = const_cast("0123456789"); + Crypto_DataBlob in = {.data = (uint8_t *)(testData), .len = strlen(testData)}; + Crypto_DataBlob out = {.data = nullptr, .len = 0}; + int mdLen = 0; + ret = OH_CryptoDigest_Create("SHA256", &ctx); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + do { + ret = OH_CryptoDigest_Update(ctx, &in); + if (ret != CRYPTO_SUCCESS) { + break; + } + ret = OH_CryptoDigest_Final(ctx, &out); + if (ret != CRYPTO_SUCCESS) { + break; + } + mdLen = OH_CryptoDigest_GetLength(ctx); + } while (0); + OH_Crypto_FreeDataBlob(&out); + OH_DigestCrypto_Destroy(ctx); + return ret; +} + +// [End message_digest_calculation_digest_algorithm_one_time_incoming] \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/entryability/EntryAbility.ets b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..edc2839f203ba057c186e19b0cbbbf80c8faa8b3 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 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 { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 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 { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/Index.ets b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..a7b78cc6a0d56c2acf88072387d4748c23a01fc0 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2025 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 { router } from '@kit.ArkUI'; +import testNapi from 'libentry.so'; + +@Entry +@Component +struct Index { + @State message: string = 'MessageDigestComputation'; + + goToSample(url: string): void { + router.pushUrl({ + url: url, + }, router.RouterMode.Single, (err) => { + if (err) { + console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); + return; + } + console.info('pushUrl success'); + }) + } + + build() { + Column({ space: 5 }) { + Text(this.message) + .fontSize(25) + .fontWeight(FontWeight.Bold) + Button($r('app.string.segmentation_async')) + .width('70%') + .onClick(() => { + this.goToSample('pages/segmentation/SegmentationAsync') + }) + Button($r('app.string.segmentation_sync')) + .width('70%') + .onClick(() => { + this.goToSample('pages/segmentation/SegmentationSync') + }) + Button($r('app.string.single_time_async')) + .width('70%') + .onClick(() => { + this.goToSample('pages/singleTime/SingleTimeAsync') + }) + Button($r('app.string.single_time_sync')) + .width('70%') + .onClick(() => { + this.goToSample('pages/singleTime/SingleTimeSync') + }) + Button($r('app.string.single_time')) + .width('70%') + .onClick(() => { + let ret: number = testNapi.doTestMd(); + this.message = 'SingleTime C++ Success Result:' + ret.toString(); + }) + Button($r('app.string.segmentation')) + .width('70%') + .onClick(() => { + let ret: number = testNapi.doLoopMd(); + this.message = 'Segmentation C++ Success Result:' + ret.toString(); + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/segmentation/SegmentationAsync.ets b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/segmentation/SegmentationAsync.ets new file mode 100644 index 0000000000000000000000000000000000000000..9dce1b36b155fa27c126f951d4122a4e2d536855 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/segmentation/SegmentationAsync.ets @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2025 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. + */ +// [Start message_digest_calculation_segmented_digest_algorithm_async] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +async function doLoopMd() { + let mdAlgName = 'SHA256'; // 摘要算法名 + let md = cryptoFramework.createMd(mdAlgName); + // 假设信息总共43字节,根据utf-8解码后,也是43字节 + let messageText = 'aaaaa.....bbbbb.....ccccc.....ddddd.....eee'; + let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer); + let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 + for (let i = 0; i < messageData.length; i += updateLength) { + let updateMessage = messageData.subarray(i, i + updateLength); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + await md.update(updateMessageBlob); + } + let mdOutput = await md.digest(); + console.info('md result: ' + mdOutput.data); + let mdLen = md.getMdLength(); + console.info('md len: ' + mdLen); +} + +// [End message_digest_calculation_segmented_digest_algorithm_async] +@Entry +@Component +struct Index { + @State message: string = 'SegmentationAsync'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_segmentation_async')) + .width('70%') + .onClick(async () => { + try { + await doLoopMd(); + this.message = 'SegmentationAsyncSuccess'; + } catch { + this.message = 'SegmentationAsyncFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/segmentation/SegmentationSync.ets b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/segmentation/SegmentationSync.ets new file mode 100644 index 0000000000000000000000000000000000000000..ccac0bd6d842fa043335b7b43cb9f600d5460aab --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/segmentation/SegmentationSync.ets @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2025 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. + */ + +// [Start message_digest_calculation_segmented_digest_algorithm_sync] + +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function doLoopMdBySync() { + let mdAlgName = 'SHA256'; // 摘要算法名 + let md = cryptoFramework.createMd(mdAlgName); + // 假设信息总共43字节,根据utf-8解码后,也是43字节 + let messageText = 'aaaaa.....bbbbb.....ccccc.....ddddd.....eee'; + let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer); + let updateLength = 20; // 假设以20字节为单位进行分段update,实际并无要求 + for (let i = 0; i < messageData.length; i += updateLength) { + let updateMessage = messageData.subarray(i, i + updateLength); + let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage }; + md.updateSync(updateMessageBlob); + } + let mdOutput = md.digestSync(); + console.info('[Sync]:md result: ' + mdOutput.data); + let mdLen = md.getMdLength(); + console.info('md len: ' + mdLen); +} +// [End message_digest_calculation_segmented_digest_algorithm_sync] +@Entry +@Component +struct Index { + @State message: string = 'SegmentationSync'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_segmentation_sync')) + .width('70%') + .onClick(() => { + try { + doLoopMdBySync(); + this.message = 'SegmentationSyncSuccess'; + } catch { + this.message = 'SegmentationSyncFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/singleTime/SingleTimeAsync.ets b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/singleTime/SingleTimeAsync.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1d60c8f8462b0bfc4e0ad5c2024d59e176a76f4 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/singleTime/SingleTimeAsync.ets @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2025 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. + */ +// [Start message_digest_calculation_digest_algorithm_one_time_incoming_async] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +async function doMd() { + let mdAlgName = 'SHA256'; // 摘要算法名 + let message = 'mdTestMessgae'; // 待摘要的数据 + let md = cryptoFramework.createMd(mdAlgName); + // 数据量较少时,可以只做一次update,将数据全部传入,接口未对入参长度做限制 + await md.update({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }); + let mdResult = await md.digest(); + console.info('Md result:' + mdResult.data); + let mdLen = md.getMdLength(); + console.info('md len: ' + mdLen); +} + +// [End message_digest_calculation_digest_algorithm_one_time_incoming_async] + +@Entry +@Component +struct Index { + @State message: string = 'SingleTimeAsync'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button($r('app.string.call_single_time_async')) + .width('70%') + .onClick(async () => { + try { + await doMd(); + this.message = 'SingleTimeAsyncSuccess'; + } catch { + this.message = 'SingleTimeAsyncFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/singleTime/SingleTimeSync.ets b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/singleTime/SingleTimeSync.ets new file mode 100644 index 0000000000000000000000000000000000000000..8fe219d1105ad9cf66ba08bd6a0c3f4e56006d07 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/ets/pages/singleTime/SingleTimeSync.ets @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2025 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. + */ +// [Start message_digest_calculation_digest_algorithm_one_time_incoming_sync] +import { cryptoFramework } from '@kit.CryptoArchitectureKit'; +import { buffer } from '@kit.ArkTS'; + +function doMdBySync() { + let mdAlgName = 'SHA256'; // 摘要算法名 + let message = 'mdTestMessgae'; // 待摘要的数据 + let md = cryptoFramework.createMd(mdAlgName); + // 数据量较少时,可以只做一次update,将数据全部传入,接口未对入参长度做限制 + md.updateSync({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }); + let mdResult = md.digestSync(); + console.info('[Sync]:Md result:' + mdResult.data); + let mdLen = md.getMdLength(); + console.info('md len: ' + mdLen); +} +// [End message_digest_calculation_digest_algorithm_one_time_incoming_sync] +@Entry +@Component +struct Index { + @State message: string = 'SingleTimeSync'; + + build() { + Column({ space: 12 }) { + Text(this.message).fontSize(20).fontWeight(FontWeight.Bold) + Button('Call SingleTimeSync') + .width('70%') + .onClick(() => { + try { + doMdBySync(); + this.message = 'SingleTimeSyncSuccess'; + } catch { + this.message = 'SingleTimeSyncFail'; + } + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/module.json5 b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4144486d1af4c03b0d767cce1cda86fc0d697f91 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/element/color.json b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/element/string.json b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..82cae4d4d44809b6ae144278fc59084789c39cd1 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/element/string.json @@ -0,0 +1,56 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "MessageDigestComputation" + }, + { + "name": "segmentation_async", + "value": "SegmentationAsync" + }, + { + "name": "segmentation_sync", + "value": "SegmentationSync" + }, + { + "name": "single_time_async", + "value": "SingleTimeAsync" + }, + { + "name": "single_time_sync", + "value": "SingleTimeSync" + }, + { + "name": "single_time", + "value": "SingleTime C++" + }, + { + "name": "segmentation", + "value": "Segmentation C++" + }, + { + "name": "call_segmentation_async", + "value": "Call SegmentationAsync" + }, + { + "name": "call_segmentation_sync", + "value": "Call SegmentationSync" + }, + { + "name": "call_single_time_async", + "value": "Call SingleTimeAsync" + }, + { + "name": "call_single_time_sync", + "value": "Call SingleTimeSync" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/media/background.png b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/media/background.png differ diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/media/foreground.png b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/media/foreground.png differ diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/media/layered_image.json b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/media/startIcon.png b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/media/startIcon.png differ diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/profile/backup_config.json b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/profile/main_pages.json b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1f23926264e37e2f1e564cec70ca43eff9d7e260 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,9 @@ +{ + "src": [ + "pages/Index", + "pages/segmentation/SegmentationAsync", + "pages/segmentation/SegmentationSync", + "pages/singleTime/SingleTimeAsync", + "pages/singleTime/SingleTimeSync" + ] +} diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/dark/element/color.json b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/ohosTest/ets/test/Ability.test.ets b/CryptoArchitectureKit/MessageDigestComputation/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0f8ce9a2c012f8fe36114cef65216ef0b6254f41 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 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 { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/ohosTest/ets/test/List.test.ets b/CryptoArchitectureKit/MessageDigestComputation/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..c6d2312d3034d55d31502df94f3f5ecaa998fd7c --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 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 abilityTest from './Ability.test'; +import messageDigestComputationTest from './MessageDigestComputation.test'; + +export default function testsuite() { + abilityTest(); + messageDigestComputationTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/ohosTest/ets/test/MessageDigestComputation.test.ets b/CryptoArchitectureKit/MessageDigestComputation/entry/src/ohosTest/ets/test/MessageDigestComputation.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..77a030e99cae340bcd914031bd589c79eb16b5d3 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/ohosTest/ets/test/MessageDigestComputation.test.ets @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2025 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 { describe, it, expect, beforeAll } from '@ohos/hypium'; +// 导入测试依赖kit +import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit'; +import { UIAbility, Want } from '@kit.AbilityKit'; + +const delegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); +const bundleName = abilityDelegatorRegistry.getArguments().bundleName; +let want: Want; + +export default function messageDigestComputationTest() { + describe('messageDigestComputationTest', () => { + /** + * 打开应用 + */ + beforeAll(async () => { + want = { + bundleName: bundleName, + abilityName: 'EntryAbility' + }; + await delegator.startAbility(want); + const driver = Driver.create(); + await driver.delayMs(1000); + const ability: UIAbility = await delegator.getCurrentTopAbility(); + console.info('get top ability'); + await driver.delayMs(1000); + expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); + }) + + /** + * 点击按钮,消息摘要计算(ArkTS 摘要算法await方式) + */ + it('testMessageDigestComputation001', 0, async () => { + console.info('uitest: testMessageDigestComputation001 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('SegmentationAsync')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SegmentationAsync')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SegmentationAsyncSuccess')); + console.info('uitest: testMessageDigestComputation001 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,消息摘要计算(ArkTS 摘要算法同步方式) + */ + it('testMessageDigestComputation002', 0, async () => { + console.info('uitest: testMessageDigestComputation002 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('SegmentationSync')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SegmentationSync')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SegmentationSyncSuccess')); + console.info('uitest: testMessageDigestComputation002 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,消息摘要计算(ArkTS 分段摘要算法await方式) + */ + it('testMessageDigestComputation003', 0, async () => { + console.info('uitest: testMessageDigestComputation003 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('SingleTimeAsync')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SingleTimeAsync')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SingleTimeAsyncSuccess')); + console.info('uitest: testMessageDigestComputation003 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,消息摘要计算(ArkTS 分段摘要算法同步方式) + */ + it('testMessageDigestComputation004', 0, async () => { + console.info('uitest: testMessageDigestComputation004 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('SingleTimeSync')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + const button2 = await driver.findComponent(ON.text('Call SingleTimeSync')); + await button2.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SingleTimeSyncSuccess')); + console.info('uitest: testMessageDigestComputation004 end'); + await driver.pressBack(); + }) + + /** + * 点击按钮,消息摘要计算(C/C++ 摘要算法) + */ + it('testMessageDigestComputation005', 0, async () => { + console.info('uitest: testMessageDigestComputation005 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('SingleTime C++')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('SingleTime C++ Success Result:0')); + console.info('uitest: testMessageDigestComputation005 end'); + }) + + /** + * 点击按钮,消息摘要计算(C/C++ 分段摘要算法) + */ + it('testMessageDigestComputation006', 0, async () => { + console.info('uitest: testMessageDigestComputation006 begin'); + const driver = Driver.create(); + const button1 = await driver.findComponent(ON.text('Segmentation C++')); + await driver.delayMs(1000); + await button1.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Segmentation C++ Success Result:0')); + console.info('uitest: testMessageDigestComputation006 end'); + }) + }) +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/ohosTest/module.json5 b/CryptoArchitectureKit/MessageDigestComputation/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c3fd9dda3040d888d9d8b0b62bcb5d3b6fbeb614 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/test/List.test.ets b/CryptoArchitectureKit/MessageDigestComputation/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 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 localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/entry/src/test/LocalUnit.test.ets b/CryptoArchitectureKit/MessageDigestComputation/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/hvigor/hvigor-config.json5 b/CryptoArchitectureKit/MessageDigestComputation/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/CryptoArchitectureKit/MessageDigestComputation/hvigorfile.ts b/CryptoArchitectureKit/MessageDigestComputation/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/CryptoArchitectureKit/MessageDigestComputation/oh-package.json5 b/CryptoArchitectureKit/MessageDigestComputation/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/CryptoArchitectureKit/MessageDigestComputation/ohosTest.md b/CryptoArchitectureKit/MessageDigestComputation/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..53df17f13a975ea08a43c3016c5808a08865cab8 --- /dev/null +++ b/CryptoArchitectureKit/MessageDigestComputation/ohosTest.md @@ -0,0 +1,13 @@ +# 消息摘要计算测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| --------------------------------------------------- | ------------ | ---- | -------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 点击按钮,消息摘要计算(ArkTS 摘要算法await方式) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,消息摘要计算(ArkTS 摘要算法同步方式) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,消息摘要计算(ArkTS 分段摘要算法await方式) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,消息摘要计算(ArkTS 分段摘要算法同步方式) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,消息摘要计算(C/C++ 摘要算法) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | +| 点击按钮,消息摘要计算(C/C++ 分段摘要算法) | 成功拉起应用 | | 成功并输出日志 | 是 | Pass | \ No newline at end of file diff --git a/CryptoArchitectureKit/MessageDigestComputation/screenshots/MessageDigestComputation1.png b/CryptoArchitectureKit/MessageDigestComputation/screenshots/MessageDigestComputation1.png new file mode 100644 index 0000000000000000000000000000000000000000..a82dbf6d10c36df0e65db94d418a991d49210c5f Binary files /dev/null and b/CryptoArchitectureKit/MessageDigestComputation/screenshots/MessageDigestComputation1.png differ diff --git a/CryptoArchitectureKit/MessageDigestComputation/screenshots/MessageDigestComputation2.png b/CryptoArchitectureKit/MessageDigestComputation/screenshots/MessageDigestComputation2.png new file mode 100644 index 0000000000000000000000000000000000000000..f9fbf4cf75def2c4b17ec48c4dc7c973636a2cf1 Binary files /dev/null and b/CryptoArchitectureKit/MessageDigestComputation/screenshots/MessageDigestComputation2.png differ