public SenderKeyMessage(uint keyId, uint iteration, byte[] ciphertext, IEcPrivateKey signatureKey) { byte[] version = { ByteUtil.IntsToByteHighAndLow((int)CurrentVersion, (int)CurrentVersion) }; byte[] message = new SenderKeyMessage { Id = keyId, Iteration = iteration, Ciphertext = ByteString.CopyFrom(ciphertext), }.ToByteArray(); byte[] signature = GetSignature(signatureKey, ByteUtil.Combine(version, message)); _serialized = ByteUtil.Combine(version, message, signature); _messageVersion = CurrentVersion; _keyId = keyId; _iteration = iteration; _ciphertext = ciphertext; }
public SenderKeyMessage(byte[] serialized) { try { byte[][] messageParts = ByteUtil.Split(serialized, 1, serialized.Length - 1 - SignatureLength, SignatureLength); 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) > CurrentVersion) { throw new InvalidMessageException("Unknown version: " + ByteUtil.HighBitsToInt(version)); } SenderKeyMessage senderKeyMessage = Parser.ParseFrom(message); if (senderKeyMessage.IdOneofCase == IdOneofOneofCase.None || senderKeyMessage.IterationOneofCase == IterationOneofOneofCase.None || senderKeyMessage.CiphertextOneofCase == CiphertextOneofOneofCase.None) { throw new InvalidMessageException("Incomplete message."); } _serialized = serialized; _messageVersion = (uint)ByteUtil.HighBitsToInt(version); _keyId = senderKeyMessage.Id; _iteration = senderKeyMessage.Iteration; _ciphertext = senderKeyMessage.Ciphertext.ToByteArray(); } catch (/*InvalidProtocolBufferException | Parse*/ Exception e) { throw new InvalidMessageException(e); } }