encryptDocument method
Future<DocumentDto>
encryptDocument(
- String dataOwnerId,
- Set<
String> delegations, - DecryptedDocumentDto document
Implementation
Future<DocumentDto> encryptDocument(String dataOwnerId, Set<String> delegations, DecryptedDocumentDto document) async {
var eks = document.encryptionKeys;
Uint8List? secret;
if (!eks.entries.any((s) => s.value.isNotEmpty)) {
secret = Uint8List.fromList(List<int>.generate(32, (i) => random.nextInt(256)));
final secretForDelegates = await Future.wait((<String>{...delegations, dataOwnerId})
.map((String d) async => Tuple2(d, await this.crypto.encryptAESKeyForHcp(dataOwnerId, d, document.id, secret!.toHexString()))));
eks = {
...eks,
...Map.fromEntries(
secretForDelegates.map((t) => MapEntry(t.item1, <DelegationDto>{DelegationDto(owner: dataOwnerId, delegatedTo: t.item1, key: t.item2.item1)})))
};
} else {
secret = (await this.crypto.decryptEncryptionKeys(dataOwnerId, document.encryptionKeys)).firstOrNull?.formatAsKey().fromHexString();
}
if (secret == null) {
throw FormatException("Cannot get encryption key for ${document.id} and hcp $dataOwnerId");
}
Tuple2 t = await this.marshaller(document);
DocumentDto sanitizedDocument = t.item1;
final Uint8List marshalledData = t.item2;
sanitizedDocument.encryptionKeys = eks;
sanitizedDocument.encryptedSelf = base64.encoder.convert(marshalledData.encryptAES(secret));
return sanitizedDocument;
}