1/* 2 * Copyright (c) 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 promptAction from '@ohos.promptAction'; 17import Logger from '../util/Logger'; 18import picker from '@ohos.file.picker'; 19import { CryptoOperation } from '../cryptoframework/CryptoOperation'; 20import TextFileManager from '../textfilemanager/TextFileManager'; 21import common from '@ohos.app.ability.common'; 22 23const TAG: string = '[Crypto_Framework]'; 24 25@Component 26export struct Sign { 27 @State keyFileName: string = ''; 28 @State keyFileUri: string = ''; 29 @State textFileUri: string = ''; 30 @State textFileName: string = ''; 31 @State keyString: string = ''; 32 @State cipherText: string = ''; 33 @State plainText: string = ''; 34 @State message: string = ''; 35 @State signFileUri: string = ''; 36 @State createKeyUri: string = ''; 37 private CryptoOperation: CryptoOperation = new CryptoOperation(); 38 39 build() { 40 Stack({ alignContent: Alignment.Center }) { 41 Column() { 42 GridRow() { 43 GridCol({ span: { xs: 12, sm: 12, md: 12, lg: 12 } }) { 44 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 45 List() { 46 ListItem() { 47 Row() { 48 Text($r('app.string.open_file')) 49 .fontSize(16) 50 .textAlign(TextAlign.Start) 51 .lineHeight(22) 52 53 Blank() 54 55 Text(this.textFileName === '' ? $r('app.string.please_choose') : this.textFileName) 56 .fontSize(14) 57 .textAlign(TextAlign.Start) 58 .lineHeight(19) 59 60 Image($r('app.media.right_arrow')) 61 .height('19vp') 62 .width('10vp') 63 .margin({ left: 9, right: 9, top: 6, bottom: 6 }) 64 } 65 .backgroundColor(0xFFFFFF) 66 .width('100%') 67 .height('52vp') 68 .padding({ top: 4, left: 12, right: 12 }) 69 }.onClick(() => { 70 this.selectTextFileAndRead(); 71 }) 72 73 ListItem() { 74 Row() { 75 Text($r('app.string.select_key_file')) 76 .fontSize(16) 77 .textAlign(TextAlign.Start) 78 .lineHeight(22) 79 80 Blank() 81 82 Text(this.keyFileName === '' ? $r('app.string.please_choose') : this.keyFileName) 83 .fontSize(14) 84 .textAlign(TextAlign.Start) 85 .lineHeight(19) 86 87 Image($r('app.media.right_arrow')) 88 .height('19vp') 89 .width('10vp') 90 .margin({ left: 9, right: 9, top: 6, bottom: 6 }) 91 } 92 .backgroundColor(0xFFFFFF) 93 .width('100%') 94 .height('48vp') 95 .padding({ left: 12, right: 12 }) 96 }.onClick(() => { 97 this.selectRsaKeyFileAndRead(); 98 }) 99 } 100 .width('100%') 101 .height('100%') 102 .borderRadius(16) 103 } 104 } 105 } 106 .height('100vp') 107 .margin({ left: 12, right: 12, bottom: 12 }) 108 109 GridRow() { 110 GridCol({ span: { xs: 12, sm: 12, md: 12, lg: 12 } }) { 111 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 112 Column() { 113 Row() { 114 Text($r('app.string.text_context')) 115 .fontSize(16) 116 .textAlign(TextAlign.Start) 117 .fontWeight(500) 118 .lineHeight(22) 119 } 120 .padding({ left: 12, right: 12 }) 121 .width('100%') 122 .height('48vp') 123 124 Row() { 125 Text() { 126 Span(this.plainText) 127 .fontSize(16) 128 .fontWeight(400) 129 .fontColor('#182431') 130 }.textAlign(TextAlign.Start) 131 } 132 .padding({ left: 12, right: 12, bottom: 4 }) 133 .width('100%') 134 .height('52vp') 135 } 136 .borderRadius(16) 137 .width('100%') 138 .height('100') 139 .backgroundColor(0xFFFFFF) 140 } 141 } 142 } 143 .height('100vp') 144 .margin({ left: 12, right: 12, bottom: 12 }) 145 146 Column() { 147 GridRow() { 148 GridCol({ span: { xs: 12, sm: 12, md: 12, lg: 12 } }) { 149 Column() { 150 Button() { 151 Text($r('app.string.generate_rsa_key_randomly')).fontSize(16).fontWeight(500) 152 .lineHeight(22) 153 .fontColor('#FFFFFF') 154 } 155 .id('encryptRsaGenKey') 156 .borderRadius(20) 157 .type(ButtonType.Capsule) 158 .width('100%') 159 .height('40vp') 160 .margin({ bottom: 16 }) 161 .backgroundColor('#007DFF') 162 .onClick(() => { 163 this.genRsaKey(); 164 }); 165 166 Button() { 167 Text($r('app.string.sign')).fontSize(16).fontWeight(500) 168 .lineHeight(22) 169 .fontColor('#FFFFFF') 170 } 171 .borderRadius(20) 172 .id('signBtn') 173 .type(ButtonType.Capsule) 174 .margin({ left: 24, right: 24 }) 175 .width('100%') 176 .height('40vp') 177 .backgroundColor('#007DFF') 178 .onClick(() => { 179 if (this.textFileUri === '' || this.keyFileUri === '') { 180 promptAction.showToast({ 181 message: $r('app.string.null_message') 182 }); 183 } else { 184 this.signFunc(); 185 } 186 }); 187 } 188 } 189 }.margin({ left: 24, right: 24 }) 190 }.width('100%').height('412vp').justifyContent(FlexAlign.End) 191 } 192 .width('100%') 193 .height('100%') 194 } 195 } 196 197 async selectRsaKeyFileAndRead() { 198 let config = { 199 action: 'ohos.want.action.OPEN_FILE', 200 parameters: { 201 startMode: 'choose', 202 } 203 } 204 let context = getContext(this) as common.UIAbilityContext; 205 let result = await context.startAbilityForResult(config); 206 if (result === null || result === undefined) { 207 Logger.error(TAG, `result is null or undefined!`); 208 return; 209 } 210 if (result.resultCode !== 0) { 211 Logger.error(TAG, `DocumentPicker.select failed, code is ${result.resultCode}, message is ${result.want.parameters.message}`); 212 return; 213 } 214 if (result.want.parameters.select_item_list === null || result.want.parameters.select_item_list === undefined) { 215 Logger.error(TAG, `result uri is null or undefined!`); 216 return; 217 } 218 if (result.want.parameters.file_name_list === null || result.want.parameters.file_name_list === undefined) { 219 Logger.error(TAG, `result file name is null or undefined!`); 220 return; 221 } 222 // 获取到密钥文档文件的URI 223 this.keyFileUri = result.want.parameters.select_item_list.toString(); 224 // 获取到密钥文档文件的文件名称 225 this.keyFileName = result.want.parameters.file_name_list.toString(); 226 await TextFileManager.readTextFile(this.keyFileUri); 227 this.keyString = TextFileManager.getString(); 228 } 229 230 async selectTextFileAndRead() { 231 let config = { 232 action: 'ohos.want.action.OPEN_FILE', 233 parameters: { 234 startMode: 'choose', 235 } 236 } 237 let context = getContext(this) as common.UIAbilityContext; 238 let result = await context.startAbilityForResult(config); 239 if (result === null || result === undefined) { 240 Logger.error(TAG, `result is null or undefined!`); 241 return; 242 } 243 if (result.resultCode !== 0) { 244 Logger.error(TAG, `DocumentPicker.select failed, code is ${result.resultCode}, message is ${result.want.parameters.message}`); 245 return; 246 } 247 if (result.want.parameters.select_item_list === null || result.want.parameters.select_item_list === undefined) { 248 Logger.error(TAG, `result uri is null or undefined!`); 249 return; 250 } 251 if (result.want.parameters.file_name_list === null || result.want.parameters.file_name_list === undefined) { 252 Logger.error(TAG, `result file name is null or undefined!`); 253 return; 254 } 255 // 获取到文档文件的URI 256 this.textFileUri = result.want.parameters.select_item_list.toString(); 257 // 获取到文档文件的文件名称 258 this.textFileName = result.want.parameters.file_name_list.toString(); 259 await TextFileManager.readTextFile(this.textFileUri); 260 this.plainText = TextFileManager.getString(); 261 } 262 263 async createTextFileAndWrite() { 264 let documentSaveOptions = new picker.DocumentSaveOptions(); 265 documentSaveOptions.newFileNames = ['signText.txt']; 266 let documentPicker = new picker.DocumentViewPicker(); 267 let documentSaveResult = await documentPicker.save(documentSaveOptions); 268 this.signFileUri = documentSaveResult[0]; 269 await TextFileManager.writeTextFile(this.signFileUri, this.cipherText); 270 } 271 272 async createKeyFileAndWrite() { 273 let documentSaveOptions = new picker.DocumentSaveOptions(); 274 documentSaveOptions.newFileNames = ['rsaKey.txt']; 275 let documentPicker = new picker.DocumentViewPicker(); 276 try { 277 let documentSaveResult = await documentPicker.save(documentSaveOptions); 278 this.createKeyUri = documentSaveResult[0]; 279 await TextFileManager.writeTextFile(this.createKeyUri, this.keyString); 280 } catch (error) { 281 Logger.error(TAG, `save key failed, ${error.code}, ${error.message}`); 282 } 283 284 } 285 286 async signFunc() { 287 if (this.plainText === '' || this.keyFileUri === '') { 288 promptAction.showToast({ 289 message: $r('app.string.null_message') 290 }); 291 return; 292 } 293 try { 294 this.cipherText = await this.CryptoOperation.rsaConvertAndSign(this.keyString, this.plainText); 295 } catch (error) { 296 promptAction.showToast({ 297 message: $r('app.string.sign_fail') 298 }); 299 Logger.error(TAG, `sign failed, ${error.code}, ${error.message}`); 300 } 301 if (this.cipherText === '' || this.cipherText === null || this.cipherText === undefined) { 302 promptAction.showToast({ 303 message: $r('app.string.sign_fail') 304 }); 305 return; 306 } else { 307 await this.createTextFileAndWrite(); 308 } 309 if (this.signFileUri === '' || typeof (this.signFileUri) == 'undefined') { 310 promptAction.showToast({ 311 message: $r('app.string.sign_fail') 312 }); 313 } else { 314 promptAction.showToast({ 315 message: $r('app.string.sign_success') 316 }); 317 } 318 } 319 320 async genRsaKey() { 321 promptAction.showToast({ 322 message: $r('app.string.slow_rsa_key_gen') 323 }); 324 try { 325 this.keyString = await this.CryptoOperation.generateRsaKey(); 326 } catch (error) { 327 Logger.error(TAG, `gen rsa key failed, ${error.code}, ${error.message}`); 328 } 329 if (this.keyString === '' || typeof (this.keyString) == 'undefined') { 330 promptAction.showToast({ 331 message: $r('app.string.gen_key_fail') 332 }); 333 return; 334 } else { 335 try { 336 await this.createKeyFileAndWrite(); 337 } catch (error) { 338 Logger.error(TAG, `write rsa key failed, ${error.code}, ${error.message}`); 339 } 340 } 341 if (this.createKeyUri === '' || typeof (this.createKeyUri) == 'undefined') { 342 promptAction.showToast({ 343 message: $r('app.string.gen_key_fail') 344 }); 345 } else { 346 promptAction.showToast({ 347 message: $r('app.string.gen_key_success') 348 }); 349 } 350 351 } 352} 353