LCOV - code coverage report
Current view: top level - lib/src/utils/crypto - encrypted_file.dart (source / functions) Coverage Total Hit
Test: merged.info Lines: 100.0 % 16 16
Test Date: 2025-01-14 13:39:53 Functions: - 0 0

            Line data    Source code
       1              : /*
       2              :  *   Famedly Matrix SDK
       3              :  *   Copyright (C) 2019, 2020, 2021 Famedly GmbH
       4              :  *
       5              :  *   This program is free software: you can redistribute it and/or modify
       6              :  *   it under the terms of the GNU Affero General Public License as
       7              :  *   published by the Free Software Foundation, either version 3 of the
       8              :  *   License, or (at your option) any later version.
       9              :  *
      10              :  *   This program is distributed in the hope that it will be useful,
      11              :  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
      12              :  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      13              :  *   GNU Affero General Public License for more details.
      14              :  *
      15              :  *   You should have received a copy of the GNU Affero General Public License
      16              :  *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
      17              :  */
      18              : 
      19              : import 'dart:convert';
      20              : import 'dart:typed_data';
      21              : 
      22              : import 'package:matrix/encryption/utils/base64_unpadded.dart';
      23              : import 'package:matrix/src/utils/crypto/crypto.dart';
      24              : 
      25              : class EncryptedFile {
      26            2 :   EncryptedFile({
      27              :     required this.data,
      28              :     required this.k,
      29              :     required this.iv,
      30              :     required this.sha256,
      31              :   });
      32              :   Uint8List data;
      33              :   String k;
      34              :   String iv;
      35              :   String sha256;
      36              : }
      37              : 
      38            1 : Future<EncryptedFile> encryptFile(Uint8List input) async {
      39            1 :   final key = secureRandomBytes(32);
      40            1 :   final iv = secureRandomBytes(16);
      41            2 :   final data = await aesCtr.encrypt(input, key, iv);
      42            2 :   final hash = await sha256(data);
      43            1 :   return EncryptedFile(
      44              :     data: data,
      45            2 :     k: base64Url.encode(key).replaceAll('=', ''),
      46            2 :     iv: base64.encode(iv).replaceAll('=', ''),
      47            2 :     sha256: base64.encode(hash).replaceAll('=', ''),
      48              :   );
      49              : }
      50              : 
      51              : /// you would likely want to use [NativeImplementations] and
      52              : /// [Client.nativeImplementations] instead
      53            1 : Future<Uint8List?> decryptFileImplementation(EncryptedFile input) async {
      54            5 :   if (base64.encode(await sha256(input.data)) !=
      55            2 :       base64.normalize(input.sha256)) {
      56              :     return null;
      57              :   }
      58              : 
      59            3 :   final key = base64decodeUnpadded(base64.normalize(input.k));
      60            3 :   final iv = base64decodeUnpadded(base64.normalize(input.iv));
      61            3 :   return await aesCtr.encrypt(input.data, key, iv);
      62              : }
        

Generated by: LCOV version 2.0-1