encrypt method
Implementation
Future<CiphertextMessage> encrypt(Uint8List paddedMessage) async {
final sessionRecord = await _sessionStore.loadSession(_remoteAddress);
final sessionState = sessionRecord.sessionState;
final chainKey = sessionState.getSenderChainKey();
final messageKeys = chainKey.getMessageKeys();
final senderEphemeral = sessionState.getSenderRatchetKey();
final previousCounter = sessionState.previousCounter;
final sessionVersion = sessionState.getSessionVersion();
final ciphertextBody = getCiphertext(messageKeys, paddedMessage);
CiphertextMessage ciphertextMessage = SignalMessage(
sessionVersion,
messageKeys.getMacKey(),
senderEphemeral,
chainKey.index,
previousCounter,
ciphertextBody,
sessionState.getLocalIdentityKey(),
sessionState.getRemoteIdentityKey());
if (sessionState.hasUnacknowledgedPreKeyMessage()) {
final items = sessionState.getUnacknowledgedPreKeyMessageItems();
final localRegistrationId = sessionState.localRegistrationId;
ciphertextMessage = PreKeySignalMessage.from(
sessionVersion,
localRegistrationId,
items.getPreKeyId(),
items.getSignedPreKeyId(),
items.getBaseKey(),
sessionState.getLocalIdentityKey(),
ciphertextMessage as SignalMessage);
}
final nextChainKey = chainKey.getNextChainKey();
sessionState.setSenderChainKey(nextChainKey);
if (!await _identityKeyStore.isTrustedIdentity(_remoteAddress,
sessionState.getRemoteIdentityKey(), Direction.sending)) {
throw UntrustedIdentityException(
_remoteAddress.getName(), sessionState.getRemoteIdentityKey());
}
await _identityKeyStore.saveIdentity(
_remoteAddress, sessionState.getRemoteIdentityKey());
await _sessionStore.storeSession(_remoteAddress, sessionRecord);
return ciphertextMessage;
}