Example #1
0
        async Task SaveLoop()
        {
            var ct = _cts.Token;

            var prevSession = default(Session);

            while (!ct.IsCancellationRequested)
            {
                var session = _session.Get();
                if (!ReferenceEquals(prevSession, session))
                {
                    prevSession = session;
                    await _store.Save(_session.Get()).ConfigureAwait(false);
                }

                try
                {
                    await Task.Delay(Period, ct).ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    break;
                }
            }

            await _store.Save(_session.Get()).ConfigureAwait(false);
        }
        public async Task Send(byte[] msg)
        {
            var session = _session.Get();

            var plainText = BtHelpers.UsingMemBinWriter(bw =>
            {
                bw.Write(session.Salt);
                bw.Write(session.Id);
                bw.Write(msg);

                var bs = bw.BaseStream;
                var requiredPadding = (16 - (int)bs.Position % 16).Apply(x => x == 16 ? 0 : x);
                var randomPadding   = (Rnd.NextInt32() & 15) * 16;
                var padding         = 16 + requiredPadding + randomPadding;
                bw.Write(Rnd.NextBytes(padding));
            });

            var authKey    = session.AuthKey.Key.ToArrayUnsafe();
            var msgKey     = CalcMsgKey(authKey, plainText, true);
            var aesKey     = CalcAesKey(authKey, msgKey, true);
            var cipherText = Aes.EncryptAES(aesKey, plainText);

            await BtHelpers.UsingMemBinWriter(bw =>
            {
                bw.Write(session.AuthKey.KeyId);
                bw.Write(msgKey);
                bw.Write(cipherText);
            }).Apply(_transport.Send);
        }
Example #3
0
 public Y Get() =>
 _var.Get().Apply(_mapper);