Exemple #1
0
        public SenderKeyDistributionMessage(uint id, uint iteration, byte[] chainKey, IEcPublicKey signatureKey)
        {
            byte[] version  = { ByteUtil.IntsToByteHighAndLow((int)CurrentVersion, (int)CurrentVersion) };
            byte[] protobuf = new SenderKeyDistributionMessage
            {
                Id         = id,
                Iteration  = iteration,
                ChainKey   = ByteString.CopyFrom(chainKey),
                SigningKey = ByteString.CopyFrom(signatureKey.Serialize())
            }.ToByteArray();

            _id           = id;
            _iteration    = iteration;
            _chainKey     = chainKey;
            _signatureKey = signatureKey;
            _serialized   = ByteUtil.Combine(version, protobuf);
        }
Exemple #2
0
        public SenderKeyDistributionMessage(byte[] serialized)
        {
            try
            {
                byte[][] messageParts = ByteUtil.Split(serialized, 1, serialized.Length - 1);
                byte     version      = messageParts[0][0];
                byte[]   message      = messageParts[1];

                if (ByteUtil.HighBitsToInt(version) < CurrentVersion)
                {
                    throw new LegacyMessageException("Legacy message: " + ByteUtil.HighBitsToInt(version));
                }

                if (ByteUtil.HighBitsToInt(version) > CurrentVersion)
                {
                    throw new InvalidMessageException("Unknown version: " + ByteUtil.HighBitsToInt(version));
                }

                SenderKeyDistributionMessage distributionMessage = Parser.ParseFrom(message);

                if (distributionMessage.IdOneofCase == IdOneofOneofCase.None ||
                    distributionMessage.IterationOneofCase == IterationOneofOneofCase.None ||
                    distributionMessage.ChainKeyOneofCase == ChainKeyOneofOneofCase.None ||
                    distributionMessage.SigningKeyOneofCase == SigningKeyOneofOneofCase.None)
                {
                    throw new InvalidMessageException("Incomplete message.");
                }

                _serialized   = serialized;
                _id           = distributionMessage.Id;
                _iteration    = distributionMessage.Iteration;
                _chainKey     = distributionMessage.ChainKey.ToByteArray();
                _signatureKey = Curve.DecodePoint(distributionMessage.SigningKey.ToByteArray(), 0);
            }
            catch (Exception e)
            {
                //InvalidProtocolBufferException | InvalidKey
                throw new InvalidMessageException(e);
            }
        }