refactor: Make encryption utils null safe

This commit is contained in:
Christian Pauly 2021-08-19 09:10:53 +02:00
parent 4bee82dbe0
commit 8f35683120
3 changed files with 25 additions and 18 deletions

View File

@ -1,4 +1,3 @@
// @dart=2.9
/* /*
* Famedly Matrix SDK * Famedly Matrix SDK
* Copyright (C) 2019, 2020, 2021 Famedly GmbH * Copyright (C) 2019, 2020, 2021 Famedly GmbH
@ -23,10 +22,10 @@ import 'crypto.dart';
class EncryptedFile { class EncryptedFile {
EncryptedFile({ EncryptedFile({
this.data, required this.data,
this.k, required this.k,
this.iv, required this.iv,
this.sha256, required this.sha256,
}); });
Uint8List data; Uint8List data;
String k; String k;
@ -47,7 +46,7 @@ Future<EncryptedFile> encryptFile(Uint8List input) async {
); );
} }
Future<Uint8List> decryptFile(EncryptedFile input) async { Future<Uint8List?> decryptFile(EncryptedFile input) async {
if (base64.encode(await sha256(input.data)) != if (base64.encode(await sha256(input.data)) !=
base64.normalize(input.sha256)) { base64.normalize(input.sha256)) {
return null; return null;

View File

@ -1,10 +1,11 @@
// @dart=2.9
// Copyright (c) 2020 Famedly GmbH // Copyright (c) 2020 Famedly GmbH
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
import 'dart:typed_data'; import 'dart:typed_data';
// ignore: import_of_legacy_library_into_null_safe
import 'subtle.dart'; import 'subtle.dart';
// ignore: import_of_legacy_library_into_null_safe
import 'subtle.dart' as subtle; import 'subtle.dart' as subtle;
abstract class Hash { abstract class Hash {

View File

@ -1,4 +1,3 @@
// @dart=2.9
// Copyright (c) 2020 Famedly GmbH // Copyright (c) 2020 Famedly GmbH
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
@ -16,21 +15,29 @@ class CryptoKey {}
@JS() @JS()
@anonymous @anonymous
class Pbkdf2Params { class Pbkdf2Params {
external factory Pbkdf2Params( external factory Pbkdf2Params({
{String name, String hash, Uint8List salt, int iterations}); String name,
String name; String hash,
String hash; Uint8List salt,
Uint8List salt; int iterations,
int iterations; });
String? name;
String? hash;
Uint8List? salt;
int? iterations;
} }
@JS() @JS()
@anonymous @anonymous
class AesCtrParams { class AesCtrParams {
external factory AesCtrParams({String name, Uint8List counter, int length}); external factory AesCtrParams({
String name; String name,
Uint8List counter; Uint8List counter,
int length; int length,
});
String? name;
Uint8List? counter;
int? length;
} }
@JS('crypto.subtle.encrypt') @JS('crypto.subtle.encrypt')