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