public SenderKeyMessage(uint keyId, uint iteration, byte[] ciphertext, ECPrivateKey signatureKey) { byte[] version = { ByteUtil.intsToByteHighAndLow((int)CURRENT_VERSION, (int)CURRENT_VERSION) }; byte[] message = new SenderKeyMessage { Id = keyId, Iteration = iteration, Ciphertext = ByteString.CopyFrom(ciphertext), }.ToByteArray(); byte[] signature = getSignature(signatureKey, ByteUtil.combine(version, message)); this.serialized = ByteUtil.combine(version, message, signature); this.messageVersion = CURRENT_VERSION; this.keyId = keyId; this.iteration = iteration; this.ciphertext = ciphertext; }
public SenderKeyMessage(byte[] serialized) { try { byte[][] messageParts = ByteUtil.split(serialized, 1, serialized.Length - 1 - SIGNATURE_LENGTH, SIGNATURE_LENGTH); byte version = messageParts[0][0]; byte[] message = messageParts[1]; byte[] signature = messageParts[2]; if (ByteUtil.highBitsToInt(version) < 3) { throw new LegacyMessageException("Legacy message: " + ByteUtil.highBitsToInt(version)); } if (ByteUtil.highBitsToInt(version) > CURRENT_VERSION) { throw new InvalidMessageException("Unknown version: " + ByteUtil.highBitsToInt(version)); } SenderKeyMessage senderKeyMessage = SenderKeyMessage.Parser.ParseFrom(message); if (senderKeyMessage.IdOneofCase == IdOneofOneofCase.None || senderKeyMessage.IterationOneofCase == IterationOneofOneofCase.None || senderKeyMessage.CiphertextOneofCase == CiphertextOneofOneofCase.None) { throw new InvalidMessageException("Incomplete message."); } this.serialized = serialized; this.messageVersion = (uint)ByteUtil.highBitsToInt(version); this.keyId = senderKeyMessage.Id; this.iteration = senderKeyMessage.Iteration; this.ciphertext = senderKeyMessage.Ciphertext.ToByteArray(); } catch (/*InvalidProtocolBufferException | Parse*/ Exception e) { throw new InvalidMessageException(e); } }