use of com.icodici.crypto.digest.Crc32 in project universa by UniversaBlockchain.
the class UDPAdapter method sendAsDataBlock.
protected synchronized void sendAsDataBlock(Block rawDataBlock, Session session) throws InterruptedException {
report(getLabel(), () -> concatReportMessage("send data to ", session.remoteNodeId), VerboseLevel.BASE);
report(getLabel(), () -> concatReportMessage("sessionKey is ", session.sessionKey.hashCode(), " for ", session.remoteNodeId));
byte[] crc32Local = new Crc32().digest(rawDataBlock.payload);
report(getLabel(), () -> concatReportMessage("sendAsDataBlock: Crc32 id is ", Arrays.equals(rawDataBlock.crc32, crc32Local)));
try {
byte[] encrypted = session.sessionKey.etaEncrypt(rawDataBlock.payload.clone());
Binder binder = Binder.fromKeysValues("data", encrypted, "crc32", rawDataBlock.crc32);
byte[] packedData = Boss.pack(binder);
report(getLabel(), () -> concatReportMessage(" data size: ", rawDataBlock.payload.length, " for ", session.remoteNodeId));
Block block = new Block(myNodeInfo.getNumber(), session.remoteNodeId, rawDataBlock.blockId, PacketTypes.DATA, session.address, session.port, packedData);
sendBlock(block, session);
} catch (EncryptionError encryptionError) {
callErrorCallbacks("[sendAsDataBlock] EncryptionError in node " + myNodeInfo.getNumber() + ": " + encryptionError.getMessage());
if (sessionsById.containsKey(session.remoteNodeId)) {
sessionsById.remove(session.remoteNodeId);
}
Session newSession = getOrCreateSession(session.remoteNodeId, session.address, session.port);
newSession.publicKey = session.publicKey;
newSession.remoteNodeId = session.remoteNodeId;
newSession.addBlockToWaitingQueue(rawDataBlock);
sendHello(newSession);
}
}