getOrCreateHcPartyKey method
Implementation
Future<Tuple2<Uint8List, DataOwnerDto?>> getOrCreateHcPartyKey(String myId, String delegateId, {RSAPrivateKey? privateKey, RSAPublicKey? publicKey}) async {
var myPublicKey = publicKey ?? rsaKeyPairs[myId]?.publicKey;
var myPrivateKey = privateKey ?? rsaKeyPairs[myId]?.privateKey;
if (myPublicKey == null) {
throw FormatException("Missing public key for hcp $myId");
}
if (myPrivateKey == null) {
throw FormatException("Missing private key for hcp $myId");
}
var aesKey = await getHcPartyKeyByOwner(delegateId, myId, myPrivateKey);
if (aesKey == null) {
var delegateDataOwner = await dataOwnerResolver.getDataOwner(delegateId);
var delegatePublicKey = delegateDataOwner?.publicKey?.toPublicKey();
if (delegatePublicKey == null) {
throw FormatException("Unknown hcp $delegateId or missing public key");
}
final encryptorForMe = pointy.OAEPEncoding(pointy.RSAEngine())
..init(true, pointy.PublicKeyParameter<pointy.RSAPublicKey>(myPublicKey.asPointyCastle));
final encryptorForDelegate = pointy.OAEPEncoding(pointy.RSAEngine())
..init(true, pointy.PublicKeyParameter<pointy.RSAPublicKey>(delegatePublicKey.asPointyCastle));
final aesKey = Uint8List.fromList(List<int>.generate(32, (i) => random.nextInt(256)));
var keyForMe = encryptorForMe.process(aesKey).toHexString();
var keyForDelegate = encryptorForDelegate.process(aesKey).toHexString();
return new Tuple2(aesKey, await dataOwnerResolver.updateDataOwnerWithNewDelegateKeyPair(myId, {
delegateId: [keyForMe, keyForDelegate]
}));
} else {
return new Tuple2(aesKey, null);
}
}