Introduce new models to encapsulate encryption attributes
This commit is contained in:
parent
3bad6a7c94
commit
44866f7ffe
3 changed files with 107 additions and 0 deletions
82
lib/models/decryption_params.dart
Normal file
82
lib/models/decryption_params.dart
Normal file
|
@ -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<String, dynamic> toMap() {
|
||||
return {
|
||||
'encryptedKey': encryptedKey,
|
||||
'keyDecryptionNonce': keyDecryptionNonce,
|
||||
'header': header,
|
||||
'nonce': nonce,
|
||||
};
|
||||
}
|
||||
|
||||
factory DecryptionParams.fromMap(Map<String, dynamic> 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;
|
||||
}
|
||||
}
|
9
lib/models/encrypted_data_attributes.dart
Normal file
9
lib/models/encrypted_data_attributes.dart
Normal file
|
@ -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);
|
||||
}
|
16
lib/models/encryption_attribute.dart
Normal file
16
lib/models/encryption_attribute.dart
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue