Introduce new models to encapsulate encryption attributes

This commit is contained in:
Vishnu Mohandas 2020-09-26 00:32:00 +05:30
parent 3bad6a7c94
commit 44866f7ffe
3 changed files with 107 additions and 0 deletions

View 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;
}
}

View 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);
}

View 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);
}
}
}