From 44866f7ffe7e1872b0a1fb5913e65805cc8a44e4 Mon Sep 17 00:00:00 2001 From: Vishnu Mohandas Date: Sat, 26 Sep 2020 00:32:00 +0530 Subject: [PATCH] Introduce new models to encapsulate encryption attributes --- lib/models/decryption_params.dart | 82 +++++++++++++++++++++++ lib/models/encrypted_data_attributes.dart | 9 +++ lib/models/encryption_attribute.dart | 16 +++++ 3 files changed, 107 insertions(+) create mode 100644 lib/models/decryption_params.dart create mode 100644 lib/models/encrypted_data_attributes.dart create mode 100644 lib/models/encryption_attribute.dart diff --git a/lib/models/decryption_params.dart b/lib/models/decryption_params.dart new file mode 100644 index 000000000..56d959563 --- /dev/null +++ b/lib/models/decryption_params.dart @@ -0,0 +1,82 @@ +import 'dart:convert'; + +class DecryptionParams { + final String encryptedKey; + final String keyDecryptionNonce; + String header; + String nonce; + + DecryptionParams({ + this.encryptedKey, + this.keyDecryptionNonce, + this.header, + this.nonce, + }); + + DecryptionParams copyWith({ + String encryptedKey, + String keyDecryptionNonce, + String header, + String nonce, + }) { + return DecryptionParams( + encryptedKey: encryptedKey ?? this.encryptedKey, + keyDecryptionNonce: keyDecryptionNonce ?? this.keyDecryptionNonce, + header: header ?? this.header, + nonce: nonce ?? this.nonce, + ); + } + + Map toMap() { + return { + 'encryptedKey': encryptedKey, + 'keyDecryptionNonce': keyDecryptionNonce, + 'header': header, + 'nonce': nonce, + }; + } + + factory DecryptionParams.fromMap(Map map) { + if (map == null) return null; + + return DecryptionParams( + encryptedKey: map['encryptedKey'], + keyDecryptionNonce: map['keyDecryptionNonce'], + header: map['header'], + nonce: map['nonce'], + ); + } + + String toJson() => json.encode(toMap()); + + factory DecryptionParams.fromJson(String source) { + if (source == null) { + return null; + } + return DecryptionParams.fromMap(json.decode(source)); + } + + @override + String toString() { + return 'DecryptionParams(encryptedKey: $encryptedKey, keyDecryptionNonce: $keyDecryptionNonce, header: $header, nonce: $nonce)'; + } + + @override + bool operator ==(Object o) { + if (identical(this, o)) return true; + + return o is DecryptionParams && + o.encryptedKey == encryptedKey && + o.keyDecryptionNonce == keyDecryptionNonce && + o.header == header && + o.nonce == nonce; + } + + @override + int get hashCode { + return encryptedKey.hashCode ^ + keyDecryptionNonce.hashCode ^ + header.hashCode ^ + nonce.hashCode; + } +} diff --git a/lib/models/encrypted_data_attributes.dart b/lib/models/encrypted_data_attributes.dart new file mode 100644 index 000000000..85884dd25 --- /dev/null +++ b/lib/models/encrypted_data_attributes.dart @@ -0,0 +1,9 @@ +import 'package:photos/models/encryption_attribute.dart'; + +class EncryptedData { + final EncryptionAttribute key; + final EncryptionAttribute nonce; + final EncryptionAttribute encryptedData; + + EncryptedData(this.key, this.nonce, this.encryptedData); +} diff --git a/lib/models/encryption_attribute.dart b/lib/models/encryption_attribute.dart new file mode 100644 index 000000000..68904936e --- /dev/null +++ b/lib/models/encryption_attribute.dart @@ -0,0 +1,16 @@ +import 'dart:typed_data'; + +import 'package:flutter_sodium/flutter_sodium.dart'; + +class EncryptionAttribute { + String base64; + Uint8List bytes; + + EncryptionAttribute({this.base64, this.bytes}) { + if (base64 != null) { + this.bytes = Sodium.base642bin(base64); + } else { + this.base64 = Sodium.bin2base64(bytes); + } + } +}