• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import prompt from '@ohos.prompt'
17import { CipherModel } from '../model/CipherModel'
18import Logger from '../model/Logger'
19
20const TAG: string = '[Encryption]'
21
22@Component
23export struct Encryption {
24  @State info: string = ''
25  @State message: string = ''
26  @State algorithmType: string = 'Encryption Algorithm'
27  private cipherModel: CipherModel = new CipherModel()
28
29  build() {
30    Stack({ alignContent: Alignment.Center }) {
31      Column() {
32        Select([{ value: 'RSA' },
33          { value: 'AES' }])
34          .id('encryptionAlgorithm')
35          .margin(4)
36          .selected(0)
37          .value(this.algorithmType)
38          .font({ size: 20, weight: 300, family: 'serif', style: FontStyle.Normal })
39          .optionFont({ size: 16, weight: 280, family: 'serif', style: FontStyle.Normal })
40          .selectedOptionFont({ size: 16, weight: 280, family: 'serif', style: FontStyle.Normal })
41          .onSelect((index: number, value: string) => {
42            this.algorithmType = value
43            Logger.info(TAG, `Select: ${index} value: ${value}`)
44          })
45
46        TextArea()
47          .margin(4)
48          .width('60%')
49          .id('encryptionInput')
50          .onChange((value: string) => {
51            this.message = value
52          })
53
54        Row() {
55          Button($r('app.string.encryption'))
56            .margin(10)
57            .fontSize(20)
58            .width('45%')
59            .height('6%')
60            .id('encryptionBtn')
61            .onClick(() => {
62              if (this.message === '') {
63                prompt.showToast({
64                  message: 'This message is null.'
65                })
66              } else {
67                if (this.algorithmType === 'RSA') {
68                  this.cipherModel.rsaEncrypt(this.message, (result) => {
69                    Logger.info(TAG, `this result = ${JSON.stringify(result)}`)
70                    this.info = `Encryption result is :  ${result.text}`
71                  })
72                } else {
73                  this.cipherModel.aesEncrypt(this.message, (result) => {
74                    Logger.info(TAG, `this result = ${JSON.stringify(result)}`)
75                    this.info = `Encryption result is :  ${result.text}`
76                  })
77                }
78              }
79            })
80
81          Button($r('app.string.reset'))
82            .margin(10)
83            .fontSize(20)
84            .width('30%')
85            .height('6%')
86            .id('encryptionResetBtn')
87            .onClick(() => {
88              this.info = ''
89            })
90        }
91        .margin(10)
92
93        Text(this.info)
94          .id('encryptionInfo')
95          .fontSize(18)
96          .width('85%')
97          .height('25%')
98          .border({ width: 2, color: Color.Black })
99          .margin(10)
100          .copyOption(CopyOptions.InApp)
101      }
102    }
103    .width('100%')
104    .height('100%')
105  }
106}