refactored keyDecryption logic into seperate function

This commit is contained in:
Abhinav-grd 2021-01-25 10:55:26 +05:30
parent 7cd85be0b2
commit a395e27f80

View file

@ -160,19 +160,23 @@ class CollectionsService {
Uint8List getCollectionKey(int collectionID) { Uint8List getCollectionKey(int collectionID) {
if (!_cachedKeys.containsKey(collectionID)) { if (!_cachedKeys.containsKey(collectionID)) {
final collection = _collectionIDToCollections[collectionID]; final collection = _collectionIDToCollections[collectionID];
_cachedKeys[collectionID] = _getDecryptedKey(collection);
}
return _cachedKeys[collectionID];
}
Uint8List _getDecryptedKey(Collection collection) {
final encryptedKey = Sodium.base642bin(collection.encryptedKey); final encryptedKey = Sodium.base642bin(collection.encryptedKey);
if (collection.owner.id == _config.getUserID()) { if (collection.owner.id == _config.getUserID()) {
_cachedKeys[collectionID] = CryptoUtil.decryptSync(encryptedKey, return CryptoUtil.decryptSync(encryptedKey, _config.getKey(),
_config.getKey(), Sodium.base642bin(collection.keyDecryptionNonce)); Sodium.base642bin(collection.keyDecryptionNonce));
} else { } else {
_cachedKeys[collectionID] = CryptoUtil.openSealSync( return CryptoUtil.openSealSync(
encryptedKey, encryptedKey,
Sodium.base642bin(_config.getKeyAttributes().publicKey), Sodium.base642bin(_config.getKeyAttributes().publicKey),
_config.getSecretKey()); _config.getSecretKey());
} }
} }
return _cachedKeys[collectionID];
}
Future<List<Collection>> _fetchCollections(int sinceTime) { Future<List<Collection>> _fetchCollections(int sinceTime) {
return _dio return _dio
@ -313,7 +317,6 @@ class CollectionsService {
} }
void _cacheCollectionAttributes(Collection collection) { void _cacheCollectionAttributes(Collection collection) {
_collectionIDToCollections[collection.id] = collection;
final collectionWithDecryptedName = final collectionWithDecryptedName =
_getCollectionWithDecryptedName(collection); _getCollectionWithDecryptedName(collection);
if (collection.attributes.encryptedPath != null) { if (collection.attributes.encryptedPath != null) {
@ -339,7 +342,7 @@ class CollectionsService {
collection.encryptedName.isNotEmpty) { collection.encryptedName.isNotEmpty) {
name = utf8.decode(CryptoUtil.decryptSync( name = utf8.decode(CryptoUtil.decryptSync(
Sodium.base642bin(collection.encryptedName), Sodium.base642bin(collection.encryptedName),
getCollectionKey(collection.id), _getDecryptedKey(collection),
Sodium.base642bin(collection.nameDecryptionNonce))); Sodium.base642bin(collection.nameDecryptionNonce)));
return Collection( return Collection(
collection.id, collection.id,