diff --git a/CustomDialogPractice/entry/src/main/ets/pages/CustomDialogDisplayAndExitAnimations.ets b/CustomDialogPractice/entry/src/main/ets/pages/CustomDialogDisplayAndExitAnimations.ets index c25234e7d305ba43b906ff26660d84e59971c5f1..1cf7894822ca2e4059603067e94a9f71ef9e9b2d 100644 --- a/CustomDialogPractice/entry/src/main/ets/pages/CustomDialogDisplayAndExitAnimations.ets +++ b/CustomDialogPractice/entry/src/main/ets/pages/CustomDialogDisplayAndExitAnimations.ets @@ -82,17 +82,9 @@ export struct CustomDialogDisplayAndExitAnimations { transition: TransitionEffect.asymmetric( TransitionEffect.OPACITY .animation({ duration: 1000 }) - .combine( - TransitionEffect - .translate({ y: 1000 }) - .animation({ duration: 1000 })) , TransitionEffect.OPACITY - .animation({ delay: 1000, duration: 1000 }) - .combine( - TransitionEffect - .translate({ y: 1000 }) - .animation({ duration: 1000 })) + .animation({ delay: 500, duration: 1000 }) ) }).then((dialogId: number) => { this.customDialogComponentId = dialogId; diff --git a/CustomDialogPractice/entry/src/main/ets/pages/GlobalDialogDecoupledFromThePage.ets b/CustomDialogPractice/entry/src/main/ets/pages/GlobalDialogDecoupledFromThePage.ets index f1697725e5bbd7076f06f5b9f55ed96e4491d2b1..74bd700b539beab38c1d710f901782e7eb9e2c88 100644 --- a/CustomDialogPractice/entry/src/main/ets/pages/GlobalDialogDecoupledFromThePage.ets +++ b/CustomDialogPractice/entry/src/main/ets/pages/GlobalDialogDecoupledFromThePage.ets @@ -13,18 +13,68 @@ * limitations under the License. */ -import { buildText } from '../uitls/DialogComponent'; -import { PromptAction } from '@kit.ArkUI'; +import { PromptActionClass } from '../uitls/PromptActionClass'; +import { ComponentContent } from '@kit.ArkUI'; + +class Params { + text: string = "" + + constructor(text: string) { + this.text = text; + } +} + + +@Builder +function buildText(params: Params) { + Column() { + Row() { + Text('Title') + .fontSize(20) + .fontWeight(FontWeight.Bold) + } + .width('100%') + .height(56) + .justifyContent(FlexAlign.Center) + + Text(params.text) + .fontSize(14) + + Button('CONFIRM') + .fontSize(16) + .fontColor('#0A59F7') + .backgroundColor(Color.White) + .onClick(() => { + PromptActionClass.closeDialog(); + }) + .width('100%') + .margin({ + top: 8, + bottom: 16 + }) + } + .padding({ + left: 24, + right: 24 + }) + .justifyContent(FlexAlign.Center) + .alignItems(HorizontalAlign.Center) + .backgroundColor(Color.White) + .borderRadius(32) + .margin({ left: 16, right: 16 }) +} @Component export struct GlobalDialogDecoupledFromThePage { - promptAction: PromptAction = this.getUIContext().getPromptAction(); - @State message: string = 'This is a dialog content.'; - @Consume('NavPathStack') pageStack: NavPathStack; + @State message: string = "This is a dialog content."; + private ctx: UIContext = this.getUIContext(); + private contentNode: ComponentContent = + new ComponentContent(this.ctx, wrapBuilder(buildText), new Params(this.message)); - @Builder - customDialogComponent() { - buildText({ message: this.message }) + aboutToAppear(): void { + PromptActionClass.setContext(this.ctx); + PromptActionClass.setContentNode(this.contentNode); + PromptActionClass.setOptions({ alignment: DialogAlignment.Center, maskColor: 'rgba(0, 0, 0, 0.2)' }); } build() { @@ -38,15 +88,7 @@ export struct GlobalDialogDecoupledFromThePage { .margin({ bottom: 16 }) .backgroundColor('#0A59F7') .onClick(() => { - this.promptAction.openCustomDialog({ - builder: () => { - this.customDialogComponent() - }, - alignment: DialogAlignment.Center, - maskColor: 'rgba(0, 0, 0, 0.2)', - }).then((dialogId: number) => { - AppStorage.setOrCreate('dialogId', dialogId); - }) + PromptActionClass.openDialog(); }) } .width('100%') diff --git a/CustomDialogPractice/entry/src/main/ets/uitls/DialogComponent.ets b/CustomDialogPractice/entry/src/main/ets/uitls/DialogComponent.ets deleted file mode 100644 index 0ec6141548aff337236e7400324172798595adbe..0000000000000000000000000000000000000000 --- a/CustomDialogPractice/entry/src/main/ets/uitls/DialogComponent.ets +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2024 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 { promptAction } from "@kit.ArkUI"; - -// DocsCode 1 -@Component -export struct buildText { - @Prop message: string = ''; - - build() { - Column() { - Row() { - Text('Title') - .fontSize(20) - .fontWeight(FontWeight.Bold) - } - .width('100%') - .height(56) - .justifyContent(FlexAlign.Center) - - Text(this.message) - .fontSize(14) - - Button('CONFIRM') - .fontSize(16) - .fontColor('#0A59F7') - .backgroundColor(Color.White) - .onClick(() => { - let dialogId = AppStorage.get('dialogId'); - promptAction.closeCustomDialog(dialogId) - }) - .width('100%') - .margin({ - top: 8, - bottom: 16 - }) - } - .padding({ - left: 24, - right: 24 - }) - .justifyContent(FlexAlign.Center) - .alignItems(HorizontalAlign.Center) - .backgroundColor(Color.White) - .borderRadius(32) - .margin({ left: 16, right: 16 }) - } -} - -// DocsCode 1 \ No newline at end of file diff --git a/CustomDialogPractice/entry/src/main/ets/uitls/PromptActionClass.ets b/CustomDialogPractice/entry/src/main/ets/uitls/PromptActionClass.ets new file mode 100644 index 0000000000000000000000000000000000000000..f83cea5640b11ebd3835b996542a43cc00099c0a --- /dev/null +++ b/CustomDialogPractice/entry/src/main/ets/uitls/PromptActionClass.ets @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2024 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 { BusinessError } from '@kit.BasicServicesKit'; +import { ComponentContent, promptAction } from '@kit.ArkUI'; +import { UIContext } from '@ohos.arkui.UIContext'; + +export class PromptActionClass { + static ctx: UIContext; + static contentNode: ComponentContent; + static options: promptAction.BaseDialogOptions; + + static setContext(context: UIContext) { + PromptActionClass.ctx = context; + } + + static setContentNode(node: ComponentContent) { + PromptActionClass.contentNode = node; + } + + static setOptions(options: promptAction.BaseDialogOptions) { + PromptActionClass.options = options; + } + + static openDialog() { + if (PromptActionClass.contentNode !== null) { + PromptActionClass.ctx.getPromptAction() + .openCustomDialog(PromptActionClass.contentNode, PromptActionClass.options) + .then(() => { + console.info('OpenCustomDialog complete.') + }) + .catch((error: BusinessError) => { + let message = (error as BusinessError).message; + let code = (error as BusinessError).code; + console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`); + }) + } + } + + static closeDialog() { + if (PromptActionClass.contentNode !== null) { + PromptActionClass.ctx.getPromptAction() + .closeCustomDialog(PromptActionClass.contentNode) + .then(() => { + console.info('CloseCustomDialog complete.') + if (PromptActionClass.contentNode !== null) { + PromptActionClass.contentNode.dispose(); // 释放contentNode + } + }) + .catch((error: BusinessError) => { + let message = (error as BusinessError).message; + let code = (error as BusinessError).code; + console.error(`CloseCustomDialog args error code is ${code}, message is ${message}`); + }) + } + } +} \ No newline at end of file diff --git a/LiteWearable/build-profile.json5 b/LiteWearable/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..9952ea80b6c8fe477b3f226597fbf48a22525fad --- /dev/null +++ b/LiteWearable/build-profile.json5 @@ -0,0 +1,41 @@ +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "4.0.0(10)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": false + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/LiteWearable/entry/build-profile.json5 b/LiteWearable/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..da8e97d0230f6ea16e63eb9d60c2d9d597bb191f --- /dev/null +++ b/LiteWearable/entry/build-profile.json5 @@ -0,0 +1,10 @@ +{ + "apiType": "faMode", + "buildOption": { + }, + "targets": [ + { + "name": "default" + } + ] +} \ No newline at end of file diff --git a/LiteWearable/entry/hvigorfile.ts b/LiteWearable/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..f5d6dbc4a300ccd28ff794472d6862d15f666427 --- /dev/null +++ b/LiteWearable/entry/hvigorfile.ts @@ -0,0 +1,6 @@ +import { legacyHapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: legacyHapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/LiteWearable/entry/oh-package.json5 b/LiteWearable/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..248c3b7541a589682a250f86a6d3ecf7414d2d6a --- /dev/null +++ b/LiteWearable/entry/oh-package.json5 @@ -0,0 +1,10 @@ +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/LiteWearable/entry/src/main/config.json b/LiteWearable/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..84e6fbb15516304ac16e6a2922e67258b54ea9f7 --- /dev/null +++ b/LiteWearable/entry/src/main/config.json @@ -0,0 +1,78 @@ +// Start 1 +// Start 2 +{ + // StartExclude 2 + "app": { + "bundleName": "com.example.litewearable", + "vendor": "example", + "version": { + "code": 1000000, + "name": "1.0.0" + } + }, + "deviceConfig": {}, + // EndExclude 2 + "module": { + "deviceType": [ + "liteWearable" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry" + }, + // StartExclude 1 + "distroFilter": { + "screenShape": { + "policy": "include", + "value": [ + "rect" + ] + }, + "screenWindow": { + "policy": "include", + "value": [ + "408*480" + ] + } + }, + // EndExclude 1 + // StartExclude 2 + "abilities": [ + { + "name": ".MainAbility", + "srcLanguage": "js", + "srcPath": "MainAbility", + "icon": "$media:icon", + "description": "$string:MainAbility_desc", + "label": "$string:MainAbility_label", + "type": "page" + } + ], + "js": [ + { + "pages": [ + "pages/index/index", + // StartExclude 1 + "initpage/initpage", + "style1/style1", + "style2/style2", + "style3/style3", + "style4/style4", + "event/event", + "details/details", + "detail/detail", + "exit/exit", + "security/security", + "cryptoFramework/cryptoFramework", + "screenLock/screenLock" + // EndExclude 1 + ], + "name": ".MainAbility" + } + ] + // EndExclude 2 + } +} +// End 1 +// End 2 \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/app.js b/LiteWearable/entry/src/main/js/MainAbility/app.js new file mode 100644 index 0000000000000000000000000000000000000000..326063fc7456533c3fc09658140972d70bc5cba7 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/app.js @@ -0,0 +1,8 @@ +export default { + onCreate() { + console.info('Application onCreate'); + }, + onDestroy() { + console.info('Application onDestroy'); + } +}; diff --git a/LiteWearable/entry/src/main/js/MainAbility/cryptoFramework/cryptoFramework.css b/LiteWearable/entry/src/main/js/MainAbility/cryptoFramework/cryptoFramework.css new file mode 100644 index 0000000000000000000000000000000000000000..269400ee202d398dd727d6ef74800d0ee5961945 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/cryptoFramework/cryptoFramework.css @@ -0,0 +1,18 @@ +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + left: 0px; + top: 0px; + width: 100%; + height: 100%; +} + +.title { + font-size: 38px; + text-align: center; + width: 100%; + height: 40%; + margin: 10px; +} diff --git a/LiteWearable/entry/src/main/js/MainAbility/cryptoFramework/cryptoFramework.hml b/LiteWearable/entry/src/main/js/MainAbility/cryptoFramework/cryptoFramework.hml new file mode 100644 index 0000000000000000000000000000000000000000..13ae33e07e3a90032d5fb5589183072449422c77 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/cryptoFramework/cryptoFramework.hml @@ -0,0 +1,5 @@ +
+ + {{ title }} + +
diff --git a/LiteWearable/entry/src/main/js/MainAbility/cryptoFramework/cryptoFramework.js b/LiteWearable/entry/src/main/js/MainAbility/cryptoFramework/cryptoFramework.js new file mode 100644 index 0000000000000000000000000000000000000000..04ed2ee98dcd038ead13214177b2328a84680232 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/cryptoFramework/cryptoFramework.js @@ -0,0 +1,92 @@ +// Start 1 +import cryptoFramework from '@ohos.security.cryptoFramework'; +// StartExclude 1 +export default { + data: { + title: "" + }, + onInit() { + this.title = "Hello World"; + } +}; +// EndExclude 1 +function stringToUint8Array(str) { + let arr = []; + for (let i = 0, j = str.length; i < j; ++i) { + arr.push(str.charCodeAt(i)); + } + let tmpUint8Array = new Uint8Array(arr); + return tmpUint8Array; +} + +function doMd() { + let mdAlgName = 'SHA256'; // 摘要算法名 + let message = 'mdTestMessage'; // 待摘要的数据 + let handle; + let mdResult; + let mdLen; + //指定摘要算法SHA256,生成摘要操作实例 + try { + handle = cryptoFramework.createMd(mdAlgName); + } catch (error) { + console.error(`createMd error, code: ${error.code}, msg: ${error.message}`); + } + try { + // 数据量较少时,可以只做一次update,将数据全部传入,接口未对入参长度做限制 + handle?.updateSync({ data: stringToUint8Array(message) }); + } catch (error) { + console.error(`updateSync error, code:+${error.code}, msg: ${error.message}`); + } + // 获取摘要计算结果。 + try { + mdResult = handle?.digest(); + } catch (error) { + console.error(`digest error, code: ${error.code}, msg: ${error.message}`); + } + console.info('Md result:' + mdResult?.data); + // 获取摘要计算长度,单位为字节 + try { + mdLen = handle?.getMdLength(); + } catch (error) { + console.error(`getMdLength error, code: ${error.code}, msg: ${error.message}`); + } + console.info(`md len: ${mdLen}`); +} +// End 1 +// Start 2 +function doRand() { + let rand; + let ret = true; + let randData + // 生成随机数操作实例 + try { + rand = cryptoFramework.createRandom(); + } catch (error) { + ret = false; + console.error(`createRandom error, code:+${error.code}, msg: ${error.message}`); + } + let len = 24; // Generate a 24-byte random number. + // (可选)调用Random.setSeed,为随机数生成池设置种子 + let seed = new Uint8Array([1, 2, 3]); + try { + rand?.setSeed({ data: seed }); + } catch (error) { + ret = false; + console.error(`setSeed error, code:+${error.code}, msg: ${error.message}`); + } + + try { + //生成安全随机数 + randData = rand?.generateRandomSync(len); + } catch (error) { + ret = false; + console.error(`generateRandomSync error, code:+${error.code}, msg: ${error.message}`); + } + if (ret) { + return randData?.data; + } else { + console.error(`doRand error`); + return 'doRand error'; + } +} +// End 2 \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/detail/detail.css b/LiteWearable/entry/src/main/js/MainAbility/detail/detail.css new file mode 100644 index 0000000000000000000000000000000000000000..269400ee202d398dd727d6ef74800d0ee5961945 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/detail/detail.css @@ -0,0 +1,18 @@ +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + left: 0px; + top: 0px; + width: 100%; + height: 100%; +} + +.title { + font-size: 38px; + text-align: center; + width: 100%; + height: 40%; + margin: 10px; +} diff --git a/LiteWearable/entry/src/main/js/MainAbility/detail/detail.hml b/LiteWearable/entry/src/main/js/MainAbility/detail/detail.hml new file mode 100644 index 0000000000000000000000000000000000000000..720f9253d4b35cd83eaf94df05044f2ddee8b60e --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/detail/detail.hml @@ -0,0 +1,9 @@ + +
+ + Hello World + + +
+ \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/detail/detail.js b/LiteWearable/entry/src/main/js/MainAbility/detail/detail.js new file mode 100644 index 0000000000000000000000000000000000000000..d8c94efa804b6cd9e5993730817b5eb9b6dd14a9 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/detail/detail.js @@ -0,0 +1,12 @@ +// Start 1 +// index.js +import router from '@ohos.router'; + +export default { + clickAction() { + router.replaceUrl({ + uri: 'pages/details/details' + }); + } +}; +// End 1 \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/details/details.css b/LiteWearable/entry/src/main/js/MainAbility/details/details.css new file mode 100644 index 0000000000000000000000000000000000000000..269400ee202d398dd727d6ef74800d0ee5961945 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/details/details.css @@ -0,0 +1,18 @@ +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + left: 0px; + top: 0px; + width: 100%; + height: 100%; +} + +.title { + font-size: 38px; + text-align: center; + width: 100%; + height: 40%; + margin: 10px; +} diff --git a/LiteWearable/entry/src/main/js/MainAbility/details/details.hml b/LiteWearable/entry/src/main/js/MainAbility/details/details.hml new file mode 100644 index 0000000000000000000000000000000000000000..a393b645b934cf4699ad14981ce993d9b61650d3 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/details/details.hml @@ -0,0 +1,9 @@ + +
+ + Details Page + + +
+ \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/details/details.js b/LiteWearable/entry/src/main/js/MainAbility/details/details.js new file mode 100644 index 0000000000000000000000000000000000000000..abe03e6d58e9ed5ecce8c03babfda11569b47e6f --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/details/details.js @@ -0,0 +1,12 @@ +// Start 1 +// details.js +import router from '@ohos.router'; + +export default { + clickAction() { + router.replaceUrl({ + uri: 'pages/index/index' + }); + } +}; +// End 1 \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/event/event.css b/LiteWearable/entry/src/main/js/MainAbility/event/event.css new file mode 100644 index 0000000000000000000000000000000000000000..269400ee202d398dd727d6ef74800d0ee5961945 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/event/event.css @@ -0,0 +1,18 @@ +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + left: 0px; + top: 0px; + width: 100%; + height: 100%; +} + +.title { + font-size: 38px; + text-align: center; + width: 100%; + height: 40%; + margin: 10px; +} diff --git a/LiteWearable/entry/src/main/js/MainAbility/event/event.hml b/LiteWearable/entry/src/main/js/MainAbility/event/event.hml new file mode 100644 index 0000000000000000000000000000000000000000..40b3051af5f9234672f3b855dba270c789d27383 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/event/event.hml @@ -0,0 +1,9 @@ + +
+ + Hello World + + +
+ \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/event/event.js b/LiteWearable/entry/src/main/js/MainAbility/event/event.js new file mode 100644 index 0000000000000000000000000000000000000000..1db6a24ce28535499928453b3e09fc40b2cc2c35 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/event/event.js @@ -0,0 +1,13 @@ +// Start 1 +// index.js: +export default { + data: { + fontSize: '30px', + fontColor: '#FF0000', + }, + clickAction() { + this.fontSize = '38px'; + this.fontColor = '#FFFFFF'; + } +}; +// End 1 \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/exit/exit.css b/LiteWearable/entry/src/main/js/MainAbility/exit/exit.css new file mode 100644 index 0000000000000000000000000000000000000000..269400ee202d398dd727d6ef74800d0ee5961945 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/exit/exit.css @@ -0,0 +1,18 @@ +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + left: 0px; + top: 0px; + width: 100%; + height: 100%; +} + +.title { + font-size: 38px; + text-align: center; + width: 100%; + height: 40%; + margin: 10px; +} diff --git a/LiteWearable/entry/src/main/js/MainAbility/exit/exit.hml b/LiteWearable/entry/src/main/js/MainAbility/exit/exit.hml new file mode 100644 index 0000000000000000000000000000000000000000..1ab84cfabc622fa6d24d924960fe75021739f6f2 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/exit/exit.hml @@ -0,0 +1,9 @@ + +
+ + Hello {{ title }} + + +
+ \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/exit/exit.js b/LiteWearable/entry/src/main/js/MainAbility/exit/exit.js new file mode 100644 index 0000000000000000000000000000000000000000..83a2985d9057dc73f7331718c7346a8fc2c2e04f --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/exit/exit.js @@ -0,0 +1,22 @@ +// Start 1 +// index.js +import router from '@ohos.router'; +// 导入app模块 +import app from '@system.app'; + +export default { + data: { + title: 'World' + }, + clickAction() { + router.replaceUrl({ + uri: 'pages/details/details' + }); + }, + touchMove(e) { // swipe处理事件 + if (e.direction == "right") { // 右滑退出 + app.terminate(); + } + } +}; +// End 1 \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/i18n/en-US.json b/LiteWearable/entry/src/main/js/MainAbility/i18n/en-US.json new file mode 100644 index 0000000000000000000000000000000000000000..e63c70d978a3a53be988388c87182f81785e170c --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/i18n/en-US.json @@ -0,0 +1,6 @@ +{ + "strings": { + "hello": "Hello", + "world": "World" + } +} \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/i18n/zh-CN.json b/LiteWearable/entry/src/main/js/MainAbility/i18n/zh-CN.json new file mode 100644 index 0000000000000000000000000000000000000000..de6ee5748322f44942c1b003319d8e66c837675f --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/i18n/zh-CN.json @@ -0,0 +1,6 @@ +{ + "strings": { + "hello": "您好", + "world": "世界" + } +} \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/initpage/initpage.css b/LiteWearable/entry/src/main/js/MainAbility/initpage/initpage.css new file mode 100644 index 0000000000000000000000000000000000000000..23be9f2820d7370ecb058793f1e25a01712ec46f --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/initpage/initpage.css @@ -0,0 +1,15 @@ +.container { + display: flex; + justify-content: center; + align-items: center; + left: 0px; + top: 0px; + width: 466px; + height: 466px; +} +.title { + font-size: 30px; + text-align: center; + width: 200px; + height: 100px; +} \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/initpage/initpage.hml b/LiteWearable/entry/src/main/js/MainAbility/initpage/initpage.hml new file mode 100644 index 0000000000000000000000000000000000000000..1e1e08a1ad3724ee5f7c44179cc10e01328cfa69 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/initpage/initpage.hml @@ -0,0 +1,8 @@ + +
+ + Hello {{ title }} + +
+ \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/initpage/initpage.js b/LiteWearable/entry/src/main/js/MainAbility/initpage/initpage.js new file mode 100644 index 0000000000000000000000000000000000000000..0dfc89acf2134f6e524998e981a9b5e7a6c7ca3d --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/initpage/initpage.js @@ -0,0 +1,7 @@ +// [Start 1] +export default { + data: { + title: 'World' + } +} +// [End 1] \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/pages/index/index.css b/LiteWearable/entry/src/main/js/MainAbility/pages/index/index.css new file mode 100644 index 0000000000000000000000000000000000000000..a75028a4780f2f5652297afac125500b61e51370 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/pages/index/index.css @@ -0,0 +1,31 @@ +.container { + width: 100%;/* 容器宽度设置为 100% */ + height: 100%;/* 容器高度设置为 100% */ + justify-content: center; + align-items: center; + flex-direction: column; + background-color: aquamarine;/* 设置背景色为碧绿色 */ +} + +.stackContainer { + width: 50%; + height: 50%; + background-color: white; +} + +.button { + top: 10%;/* 设置y轴上距离父组件为父组件高度的10%位置 */ + left: 10%;/* 设置x轴上距离父组件为父组件宽度的10%位置 */ + width: 50%; + height: 30%; + font-size: 30px; + background-color: black; +} + +.button2 { + width: 50%; + height: 20%; + font-size: 30px; + background-color: black; + margin-top: 2%;/* 父组件高度的2% */ +} \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/pages/index/index.hml b/LiteWearable/entry/src/main/js/MainAbility/pages/index/index.hml new file mode 100644 index 0000000000000000000000000000000000000000..8b8fe70b6678c15d3390e9d42efadc0066899f2b --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/pages/index/index.hml @@ -0,0 +1,9 @@ + +
+ + + + +
+ \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/pages/index/index.js b/LiteWearable/entry/src/main/js/MainAbility/pages/index/index.js new file mode 100644 index 0000000000000000000000000000000000000000..ab75d2c9902d12abdf9b7a59c0c75a628ef65947 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/pages/index/index.js @@ -0,0 +1,8 @@ +export default { + data: { + title: '' + }, + onInit() { + this.title = this.$t('strings.world'); + } +}; diff --git a/LiteWearable/entry/src/main/js/MainAbility/screenLock/screenLock.css b/LiteWearable/entry/src/main/js/MainAbility/screenLock/screenLock.css new file mode 100644 index 0000000000000000000000000000000000000000..269400ee202d398dd727d6ef74800d0ee5961945 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/screenLock/screenLock.css @@ -0,0 +1,18 @@ +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + left: 0px; + top: 0px; + width: 100%; + height: 100%; +} + +.title { + font-size: 38px; + text-align: center; + width: 100%; + height: 40%; + margin: 10px; +} diff --git a/LiteWearable/entry/src/main/js/MainAbility/screenLock/screenLock.hml b/LiteWearable/entry/src/main/js/MainAbility/screenLock/screenLock.hml new file mode 100644 index 0000000000000000000000000000000000000000..13ae33e07e3a90032d5fb5589183072449422c77 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/screenLock/screenLock.hml @@ -0,0 +1,5 @@ +
+ + {{ title }} + +
diff --git a/LiteWearable/entry/src/main/js/MainAbility/screenLock/screenLock.js b/LiteWearable/entry/src/main/js/MainAbility/screenLock/screenLock.js new file mode 100644 index 0000000000000000000000000000000000000000..41374e8726168c153a8097f223f50d8606cb32d3 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/screenLock/screenLock.js @@ -0,0 +1,58 @@ +// Start 1 +import screenLock from '@ohos.screenLock'; + +// StartExclude 1 +export default { + data: { + title: "" + }, + onInit() { + this.title = "Hello World"; + } +}; + +// EndExclude 1 + +function unlockScreen() { + let result; + screenLock.unlockScreen(function (err) { + if (err) { + result = `Failed to unlock the screen, Code: ${err.code}, ${err.message}`; + } else { + result = `call unlockScreen sucess`; + } + }); + + return result; +} +// End 1 +// Start 2 +function isScreenLocked() { + let isLocked = false; + let result; + screenLock.isScreenLocked(function (err, data) { + if (err) { + result = `call isScreenLocked erro ${err.message}`; + } else { + isLocked = data + result = `call isScreenLocked sucess islocked: ${isLocked}` + } + }); + return result; +} +// End 2 +// Start 3 +function isSecureMode() { + let result; + let isSafety = false; + screenLock.isSecureMode(function (err, data) { + if (err) { + result = `call isSecureMode erro ${err.message}`; + } else { + isSafety = data + result = `call isSecureMode sucess isSafety ${isSafety}`; + } + }); + return result; +} +// End 3 \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/security/DES.js b/LiteWearable/entry/src/main/js/MainAbility/security/DES.js new file mode 100644 index 0000000000000000000000000000000000000000..25f4199188fb04d95a260a7eecf0b27bbb9e49fa --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/security/DES.js @@ -0,0 +1,305 @@ +// Start 1 +import huks from '@ohos.security.huks'; + +// Start 2 +// Start 3 +// 别名,用于区分生成的KEY +const DES_CBC_64_KEY_ALIAS = 'DesCBC64KeyAlias'; +// StartExclude 1 +// 三段式句柄,用于连接三段式上下文 +let handle; +let IV = '12345678'; +// 明文,加密前数据 +let plainText = 'DESAAAdffssghCBC5612345612345L64'; +// 密文,存放加密后数据 +let cipherText = ''; +// EndExclude 1 + +// StartExclude 2 +// StartExclude 3 +// 配置生成密钥所需的Tag +function getDesGenProperties() { + let properties = new Array(); + let index = 0; + // DES算法 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_ALGORITHM, + value: huks.HuksKeyAlg.HUKS_ALG_DES + }; + // 密钥长度64 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, + value: huks.HuksKeySize.HUKS_DES_KEY_SIZE_64 + }; + // 密钥用途,加密和解密 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_PURPOSE, + value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT + }; + + return properties; +} + +// 生成密钥 +function generateDESKey() { + let huksInfo; + let options = { properties: getDesGenProperties() } + huks.generateKeyItem(DES_CBC_64_KEY_ALIAS, options, function (err, data) { + if (err) { + huksInfo = "generateKeyDES return code:" + err.code + " : " + err.message; + } else { + huksInfo = "The key has been generated:" + JSON.stringify(data); + } + }); + return huksInfo; +} +// EndExclude 2 +// End 1 +function getDesCBCEncryptProperties() { + let properties = new Array(); + let index = 0; + // 算法 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_ALGORITHM, + value: huks.HuksKeyAlg.HUKS_ALG_DES + }; + + // 密钥长度 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, + value: huks.HuksKeySize.HUKS_DES_KEY_SIZE_64 + }; + + // 密钥用途 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_PURPOSE, + value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT + }; + + // 填充方式 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_PADDING, + value: huks.HuksKeyPadding.HUKS_PADDING_NONE + }; + + // 分组方式 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE, + value: huks.HuksCipherMode.HUKS_MODE_CBC + }; + + // 分组加密偏移向量,更安全 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_IV, + value: stringToUint8Array(IV) + }; + + return properties; +} + +function encryptDES() { + let huksInfo; + let ret = true; + let initOptions = { + properties: getDesCBCEncryptProperties(), + inData: new Uint8Array() + } + + let updateOptions = { + properties: getDesCBCEncryptProperties(), + inData: stringToUint8Array(plainText.substring(0, 16)) + } + + let finishOptions = { + properties: getDesCBCEncryptProperties(), + inData: stringToUint8Array(plainText.substring(16, 32)) + } + + huks.initSession(DES_CBC_64_KEY_ALIAS, initOptions, function (initErr, initData) { + if (initErr) { + huksInfo = "encryptDES initSession return code:" + initErr.code + " : " + initErr.message; + ret = false; + huks.abortSession(initData.handle, initOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = "encryptDES init abortSession return code:" + abortErr.code + " : " + abortErr.message; + } + }); + } else { + handle = initData.handle; + } + }); + + if (ret == false) { + return huksInfo; + } + + huks.updateSession(handle, updateOptions, function (updateErr, updateData) { + if (updateErr) { + huksInfo = "encryptDES updateSession return code:" + updateErr.code + " : " + updateErr.message; + ret = false; + huks.abortSession(handle, updateOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = "encryptDES update abortSession return code:" + abortErr.code + " : " + abortErr.message; + } + }); + } else { + // 密文接收 + cipherText = uint8ArrayToString(updateData.outData); + huksInfo = cipherText + } + }); + if (ret == false) { + return huksInfo; + } + + huks.finishSession(handle, finishOptions, function (finishErr, finishData) { + if (finishErr) { + ret = false; + huksInfo = "encryptDES finishSession return code:" + finishErr.code + " : " + finishErr.message; + huks.abortSession(handle, finishOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = + "encryptDES finish abortSession return code:" + abortErr.code + " : " + abortErr.message; + } + }); + } else { + // 密文接收 + cipherText = cipherText + uint8ArrayToString(finishData.outData); + huksInfo = cipherText + } + }); + + return huksInfo; +} +// EndExclude 3 +function stringToUint8Array(str) { + let arr = []; + for (let i = 0, j = str.length; i < j; ++i) { + arr.push(str.charCodeAt(i)); + } + + return new Uint8Array(arr); +} + +function uint8ArrayToString(fileData) { + let dataString = ''; + for (let i = 0; i < fileData.length; i++) { + dataString += String.fromCharCode(fileData[i]); + } + + return dataString; +} +// End 2 + +function GetDesCBCDecryptProperties() { + let properties = new Array(); + let index = 0; + // 算法 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_ALGORITHM, + value: huks.HuksKeyAlg.HUKS_ALG_DES + }; + // 密钥长度 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, + value: huks.HuksKeySize.HUKS_DES_KEY_SIZE_64 + }; + // 密钥用途 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_PURPOSE, + value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT + }; + // 填充方式 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_PADDING, + value: huks.HuksKeyPadding.HUKS_PADDING_NONE + }; + // 分组方式 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_BLOCK_MODE, + value: huks.HuksCipherMode.HUKS_MODE_CBC + }; + // 分组加密偏移向量,更安全 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_IV, + value: stringToUint8Array(IV) + }; + return properties; +} + +function decryptDES() { + let huksInfo; + let ret = true; + let initOptions = { + properties: GetDesCBCDecryptProperties(), + inData: new Uint8Array() + } + let updateOptions = { + properties: GetDesCBCDecryptProperties(), + inData: stringToUint8Array(cipherText.substring(0, 16)) + } + + let finishOptions = { + properties: GetDesCBCDecryptProperties(), + inData: stringToUint8Array(cipherText.substring(16, 32)) + } + + huks.initSession(DES_CBC_64_KEY_ALIAS, initOptions, function (initErr, initData) { + if (initErr) { + ret = false; + huksInfo = "decryptDES initSession return code:" + initErr.code + " : " + initErr.message; + huks.abortSession(initData.handle, initOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = + "decryptDES initSession abortSession return code:" + abortErr.code + " : " + abortErr.message; + } + }); + } else { + handle = initData.handle; + } + }); + + if (ret == false) { + return huksInfo; + } + + huks.updateSession(handle, updateOptions, function (updateErr, updateData) { + if (updateErr) { + ret = false; + huksInfo = "decryptDES updateSession return code:" + updateErr.code + " : " + updateErr.message; + huks.abortSession(handle, updateOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = "decryptDES update abortSession return code:" + abortErr.code + " : " + abortErr.message; + } + }); + } else { + // 明文接收 + outPlainText = uint8ArrayToString(updateData.outData); + huksInfo = outPlainText; + } + }); + + if (ret == false) { + return huksInfo; + } + + huks.finishSession(handle, finishOptions, function (finishErr, finishData) { + if (finishErr) { + ret = false; + huksInfo = "decryptDES finishSession return code:" + finishErr.code + " : " + finishErr.message; + huks.abortSession(handle, finishOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = "decryptDES abortSession return code:" + abortErr.code + " : " + abortErr.message; + } + }); + } else { + // 明文接收 + outPlainText = outPlainText + uint8ArrayToString(finishData.outData); + huksInfo = outPlainText; + } + }); + + return huksInfo; +} + +// End 3 \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/security/DesCBC64.js b/LiteWearable/entry/src/main/js/MainAbility/security/DesCBC64.js new file mode 100644 index 0000000000000000000000000000000000000000..a0e942cb53a9a88c7dc3faa76b9dc71bf9693f9b --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/security/DesCBC64.js @@ -0,0 +1,39 @@ +// Start 1 +// Start 2 +import huks from '@ohos.security.huks'; + +const KEY_ALIAS = 'DesCBC64KeyAlias'; +// StartExclude 2 +function isKeyItemExist() { + let huksInfo; + let emptyOptions = { + properties: [] + }; + + huks.isKeyItemExist(KEY_ALIAS, emptyOptions, function (err, data) { + if (data) { + huksInfo = "The key:" + KEY_ALIAS + " exists"; + } else { + huksInfo = "The key doesn't exist errcode:" + err.code + " : " + err.message; + } + }); + + return huksInfo; +} +// End 1 +// EndExclude 2 +function deleteKeyProcess() { + let huksInfo; + let emptyOptions = { + properties: [] + }; + huks.deleteKeyItem(KEY_ALIAS, emptyOptions, function (err, data) { + if (err) { + huksInfo = "deleteKeyItem error return code:" + err.code + " : " + err.message; + } else { + huksInfo = "The key has been deleted"; + } + }) + return huksInfo; +} +// End 2 \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/security/HMAC.js b/LiteWearable/entry/src/main/js/MainAbility/security/HMAC.js new file mode 100644 index 0000000000000000000000000000000000000000..e7e6fcb9cfd5294ccd650a9e937508309f447738 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/security/HMAC.js @@ -0,0 +1,166 @@ +// Start 1 +// Start 2 +import huks from '@ohos.security.huks'; + +// HMACKeyAlias 别名,用于区分生成的KEY +const HMAC_KEY_ALIAS = 'HMACKeyAlias'; +// StartExclude 1 +// 明文,加密前数据 +let plainText = "HMACSAdffssghABC5612345612345192"; +// 密文,存放加密后数据 +let cipherText = ''; +// 操作句柄 +let handle; +// EndExclude 1 +// StartExclude 2 +function getHMACGenProperties() { + let properties = new Array(); + let index = 0; + // 算法 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_ALGORITHM, + value: huks.HuksKeyAlg.HUKS_ALG_AES + }; + // 密钥长度 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, + value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256 + }; + // 密钥用途 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_PURPOSE, + value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_MAC + }; + return properties; +} + +function generateHMACKey() { + let huksInfo; + let options = { + properties: getHMACGenProperties() + } + huks.generateKeyItem(HMAC_KEY_ALIAS, options, function (err, data) { + if (err) { + huksInfo = "generateKeyHMAC return code:" + err.code + " : " + err.message; + } else { + huksInfo = "The key has been generated" + JSON.stringify(data); + } + }); + return huksInfo; +} +// End 1 +// EndExclude 2 +function uint8ArrayToString(fileData) { + let dataString = ''; + for (let i = 0; i < fileData.length; i++) { + dataString += String.fromCharCode(fileData[i]); + } + return dataString; +} + +function stringToUnit8Array(str) { + let arr = []; + for (let i = 0, j = str.length; i < j; ++i) { + arr.push(str.charCodeAt(i)); + } + return new Uint8Array(arr); +} + +function getHMACProperties() { + let properties = new Array(); + let index = 0; + // 算法 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_ALGORITHM, + value: huks.HuksKeyAlg.HUKS_ALG_HMAC + }; + // 密钥长度 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, + value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256 + }; + // 密钥用途 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_PURPOSE, + value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_MAC + }; + // 摘要算法 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_DIGEST, + value: huks.HuksKeyPurpose.HUKS_DIGEST_SHA256 + }; + return properties; +} + +function HMACProcess() { + let huksInfo; + let ret = true; + let initOptions = { + properties: getHMACProperties(), + inData: new Uint8Array() + } + let updateOptions = { + properties: getHMACProperties(), + inData: stringToUnit8Array(plainText.substring(0, 16)) + } + let finishOptions = { + properties: getHMACProperties(), + inData: stringToUnit8Array(plainText.substring(16, 32)) + } + + huks.initSession(HMAC_KEY_ALIAS, initOptions, function (initErr, initData) { + if (initErr) { + huksInfo = "HMAC initSession return code:" + initErr.code + " : " + initErr.message; + ret = false; + huks.abortSession(initData.handle, initOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = "HMAC init abortSession return code:" + abortErr.code + " : " + abortErr.message; + } + }); + } else { + handle = initData.handle; + } + }); + if (ret == false) { + return huksInfo; + } + + huks.updateSession(handle, updateOptions, function (updateErr, updateData) { + if (updateErr) { + huksInfo = "HMAC updateSession return code:" + updateErr.code + " : " + updateErr.message; + ret = false; + huks.abortSession(handle, updateOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = "HMAC update abortSession return code:" + abortErr.code + " : " + abortErr.message; + } + }); + } + }); + + if (ret == false) { + return huksInfo; + } + + huks.finishSession(handle, finishOptions, function (finishErr, finishData) { + if (finishErr) { + ret = false; + huksInfo = "encrypt HMAC finishSession return code:" + finishErr.code + " : " + finishErr.message; + huks.abortSession(handle, finishOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = + "encrypt HMAC finish abortSession return code:" + abortErr.code + " : " + abortErr.message; + } + }); + } else { + // HMAC密文接收 + cipherText = uint8ArrayToString(finishData.outData); + } + }); + if (ret == false) { + return huksInfo; + } else { + huksInfo = "success:" + cipherText; + } + return huksInfo; +} +// End 2 \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/security/KeyAlias.js b/LiteWearable/entry/src/main/js/MainAbility/security/KeyAlias.js new file mode 100644 index 0000000000000000000000000000000000000000..c1fb0db5bb20165779521914b6fa7159b58af44e --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/security/KeyAlias.js @@ -0,0 +1,83 @@ +// Start 1 +// Start 2 +import huks from '@ohos.security.huks'; + +// StartExclude 2 +// 密钥材料 +let plainTextKey = new Uint8Array([ + 0x1d, 0x2c, 0x3a, 0x4b, 0x5e, 0x6f, 0x7d, 0x8a, 0x9c, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf1, 0x23 +]); +// EndExclude 2 +// 确定密钥别名 +const KEY_ALIAS = 'keyAlias'; + +// StartExclude 2 +// 封装密钥属性集和密钥材料 +function getImportKeyProperties() { + let properties = new Array(); + let index = 0; + // 算法 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_ALGORITHM, + value: huks.HuksKeyAlg.HUKS_ALG_AES + }; + // 秘钥长度 (128/192/256) + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, + value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_128 + }; + // 秘钥用途,生成秘钥时使用可以限制秘钥的使用权限 (AES一般用于加密、解密) + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_PURPOSE, + value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT + }; + return properties +} + +// 明文导入密钥 +function importKey() { + let huksInfo; + let ret = true; + let options = { + properties: getImportKeyProperties(), + inData: plainTextKey + } + huks.importKeyItem(KEY_ALIAS, options, function (initErr, initData) { + if (initErr) { + ret = false; + huksInfo = "import key:" + initErr.code + " : " + initErr.message; + huks.abortSession(initData.handle, options, function (abortErr, abortData) { + if (abortErr) { + huksInfo = "import key init abortSession return code:" + abortErr.code + " : " + abortErr.message; + } + }); + } else { + huksInfo = uint8ArrayToString(initData.outData); + } + }); + + if (ret == false) { + return huksInfo + "导入失败"; + } + + return huksInfo + "导入成功"; +} +// End 1 +// EndExclude 2 +function exportKeyProcess() { + let huksInfo; + let emptyOptions = { + properties: [] + } + + huks.exportKeyItem(KEY_ALIAS, emptyOptions, function (err, data) { + if (err) { + huksInfo = "exportKeyItem error return code:" + err.code + " : " + err.message; + } else { + huksInfo = uint8ArrayToString(data.outData); + } + }) + + return huksInfo; +} +// End 2 \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/security/RSA.js b/LiteWearable/entry/src/main/js/MainAbility/security/RSA.js new file mode 100644 index 0000000000000000000000000000000000000000..611b1836ae25c9856fb64845aababf3a060f1b07 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/security/RSA.js @@ -0,0 +1,285 @@ +// Start 1 +// Start 3 +import huks from '@ohos.security.huks'; + +// Start 2 +// 别名,用于区分生成的KEY +const RSA_KEY_ALIAS = 'RSAKeyAlias'; +// 自定义密钥长度必须在1024 -2048之间,且是8的倍数 +const HUKS_RSA_KEY_SIZE_1024 = 1024; +// StartExclude 1 +// 明文,加密前数据 +let plainText = "RSASSAdffssghCBC5612345612345192"; +// 明文,加密前数据的长度 +let plainTextLen = 32; +// 密文,存放加密后数据 +let cipherText = ''; +// 操作句柄 +let handle; + +// EndExclude 1 +// StartExclude 2 +// StartExclude 3 +function getRSAGenProperties() { + let properties = new Array(); + let index = 0; + // 算法 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_ALGORITHM, + value: huks.HuksKeyAlg.HUKS_ALG_RSA + }; + // 密钥长度 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, + value: HUKS_RSA_KEY_SIZE_1024 + }; + // 密钥用途 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_PURPOSE, + value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT + }; + return properties; +} + +function generateRSAKey() { + let huksInfo; + let options = { properties: getRSAGenProperties() }; + huks.generateKeyItem(HUKS_RSA_KEY_SIZE_1024, options, function (err, data) { + if (err) { + huksInfo = "generateRSAKey return code:" + err.code + " : " + err.message; + } else { + huksInfo = "The key has been generated:" + JSON.stringify(data); + } + }); + return huksInfo; +} + +// End 1 +// EndExclude 2 +function getRSAEncryptProperties() { + let properties = new Array(); + let index = 0; + // 算法 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_ALGORITHM, + value: huks.HuksKeyAlg.HUKS_ALG_RSA + }; + // 密钥长度 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, + value: HUKS_RSA_KEY_SIZE_1024 + }; + + // 密钥用途 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_PURPOSE, + value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT + }; + // 密钥 PADDING方式 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_PADDING, + value: huks.HuksKeyPadding.HUKS_PADDING_PKCS1_V1_5 + }; + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_DIGEST, + value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 + } + return properties; +} + +function encryptProcess() { + let ret = true; + let huksInfo; + let initOptions = { + properties: getRSAEncryptProperties(), + inData: new Uint8Array() + } + let updateOptions = { + properties: getRSAEncryptProperties(), + inData: stringToUint8Array(plainText.substring(0, plainTextLen / 2)) + } + let finishOptions = { + properties: getRSAEncryptProperties(), + inData: stringToUint8Array(plainText.substring(plainTextLen / 2, plainTextLen)) + } + huks.initSession(RSA_KEY_ALIAS, initOptions, function (initErr, initData) { + if (initErr) { + huksInfo = "encryptProcess initSession return code:" + initErr.code + " : " + initErr.message; + ret = false; + huks.abortSession(initData.handle, initOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = + "encryptProcess init abortSession return code:" + abortErr.code + " : " + abortErr.message; + } + }); + } else { + handle = initData.handle; + } + }); + if (ret == false) { + return huksInfo; + } + + huks.updateSession(handle, updateOptions, function (updateErr, updateData) { + if (updateErr) { + huksInfo = "encryptProcess updateSession return code:" + updateErr.code + " : " + updateErr.message; + ret = false; + huks.abortSession(handle, updateOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = "encryptProcess updateSession abortSession return code:" + abortErr.code + " : " + + abortErr.message; + } + }); + } + }); + + if (ret == false) { + return huksInfo; + } + + huks.finishSession(handle, finishOptions, function (finishErr, finishData) { + if (finishErr) { + ret = false; + huksInfo = "encryptProcess finishSession return code:" + finishErr.code + " : " + finishErr.message; + huks.abortSession(handle, finishOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = + "encryptProcess finish abortSession return code:" + abortErr.code + " : " + abortErr.message; + } + }); + } else { + // 密文接收 + cipherText = uint8ArrayToString(finishData.outData); + huksInfo = cipherText; + } + }); + return huksInfo; +} + +function uint8ArrayToString(fileData) { + let dataString = ''; + for (let i = 0; i < fileData.length; i++) { + dataString += String.fromCharCode(fileData[i]); + } + return dataString; +} + +function stringToUint8Array(str) { + let arr = []; + for (let i = 0, j = str.length; i < j; ++i) { + arr.push(str.charCodeAt(i)); + } + return new Uint8Array(arr); +} + +// End 2 +// EndExclude 3 +function getRSADecryptProperties() { + let properties = new Array(); + let index = 0; + // 算法 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_ALGORITHM, + value: huks.HuksKeyAlg.HUKS_ALG_RSA + }; + // 密钥长度 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, + value: HUKS_RSA_KEY_SIZE_1024 + }; + + // 密钥用途 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_PURPOSE, + value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT + }; + + // 密钥 PADDING方式 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_PADDING, + value: huks.HuksKeyPadding.HUKS_PADDING_PKCS1_V1_5 + }; + + // 摘要算法 + properties[index++] = { + tag: huks.HuksTag.HUKS_TAG_DIGEST, + value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 + } + + return properties; +} + +function decryptProcess() { + // 加密密文长度 + let len = HUKS_RSA_KEY_SIZE_1024 / 8; + let ret = true; + let huksInfo; + let initOptions = { + properties: getRSADecryptProperties(), + inData: new Uint8Array() + } + let updateOptions = { + properties: getRSADecryptProperties(), + inData: stringToUint8Array(cipherText.substring(0, len / 2)) + } + let finishOptions = { + properties: getRSADecryptProperties(), + inData: stringToUint8Array(cipherText.substring(len / 2, len)) + } + huks.initSession(RSA_KEY_ALIAS, initOptions, function (initErr, initData) { + if (initErr) { + huksInfo = "decryptProcess initSession return code:" + initErr.code + " : " + initErr.message; + ret = false; + huks.abortSession(initData.handle, initOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = + "decryptProcess init abortSession return code:" + abortErr.code + " : " + abortErr.message; + } + }); + } else { + handle = initData.handle; + } + }); + if (ret == false) { + return huksInfo; + } + huks.updateSession(handle, updateOptions, function (updateErr, updateData) { + if (updateErr) { + huksInfo = "decryptProcess updateSession return code:" + updateErr.code + " : " + updateErr.message; + ret = false; + huks.abortSession(handle, updateOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = "decryptProcess updateSession abortSession return code:" + abortErr.code + " : " + + abortErr.message; + } + }); + } + }); + + if (ret == false) { + return huksInfo; + } + huks.finishSession(handle, finishOptions, function (finishErr, finishData) { + if (finishErr) { + ret = false; + huksInfo = "decryptProcess finishSession return code:" + finishErr.code + " : " + finishErr.message; + huks.abortSession(handle, finishOptions, function (abortErr, abortData) { + if (abortErr) { + huksInfo = + "decryptProcess finish abortSession return code:" + abortErr.code + " : " + abortErr.message; + } + }); + } else { + // 明文接收 + outPlainText = uint8ArrayToString(finishData.outData); + } + }); + if (ret == false) { + return huksInfo; + } else { + huksInfo = "Success:" + outPlainText; + } + return huksInfo; +} + +// End 3 \ No newline at end of file diff --git a/LiteWearable/entry/src/main/js/MainAbility/security/security.css b/LiteWearable/entry/src/main/js/MainAbility/security/security.css new file mode 100644 index 0000000000000000000000000000000000000000..269400ee202d398dd727d6ef74800d0ee5961945 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/security/security.css @@ -0,0 +1,18 @@ +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + left: 0px; + top: 0px; + width: 100%; + height: 100%; +} + +.title { + font-size: 38px; + text-align: center; + width: 100%; + height: 40%; + margin: 10px; +} diff --git a/LiteWearable/entry/src/main/js/MainAbility/security/security.hml b/LiteWearable/entry/src/main/js/MainAbility/security/security.hml new file mode 100644 index 0000000000000000000000000000000000000000..13ae33e07e3a90032d5fb5589183072449422c77 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/security/security.hml @@ -0,0 +1,5 @@ +
+ + {{ title }} + +
diff --git a/LiteWearable/entry/src/main/js/MainAbility/security/security.js b/LiteWearable/entry/src/main/js/MainAbility/security/security.js new file mode 100644 index 0000000000000000000000000000000000000000..c683063934277cfa65caf96b78ad7250554ca995 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/security/security.js @@ -0,0 +1,8 @@ +export default { + data: { + title: "" + }, + onInit() { + this.title = "Hello World"; + } +}; diff --git a/LiteWearable/entry/src/main/js/MainAbility/style1/style1.css b/LiteWearable/entry/src/main/js/MainAbility/style1/style1.css new file mode 100644 index 0000000000000000000000000000000000000000..269400ee202d398dd727d6ef74800d0ee5961945 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/style1/style1.css @@ -0,0 +1,18 @@ +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + left: 0px; + top: 0px; + width: 100%; + height: 100%; +} + +.title { + font-size: 38px; + text-align: center; + width: 100%; + height: 40%; + margin: 10px; +} diff --git a/LiteWearable/entry/src/main/js/MainAbility/style1/style1.hml b/LiteWearable/entry/src/main/js/MainAbility/style1/style1.hml new file mode 100644 index 0000000000000000000000000000000000000000..7696cdf44addd9ed529acac2a77c0c29efb36ec6 --- /dev/null +++ b/LiteWearable/entry/src/main/js/MainAbility/style1/style1.hml @@ -0,0 +1,9 @@ + +
+ + + Hello World + +
+ +
+ + + Hello World + +
+ + +
+ + + Hello World + +
+ +
+ + Hello World + +
+