Inheritance: Pubkey, ICanBeSent
        public PrivateKey GeneratePrivateKey(string label = null, bool eighteenByteRipe = false, bool send = false, bool save = true)
        {
            var privateKey = new PrivateKey(label, eighteenByteRipe);
            if (save) privateKey.SaveAsync(DB).Wait();
            if (send) privateKey.SendAsync(this);

            lock (_lock4privateKeysCache)
                _privateKeysCache = null;

            return privateKey;
        }
        public PrivateKey CreateRandomAddress(string label, bool eighteenByteRipe = false)
        {
            var privateKey = new PrivateKey(label, eighteenByteRipe);
            privateKey.SaveAsync(DB);

            lock (_lock4privateKeysCache)
                _privateKeysCache = null;

            return privateKey;
        }
Esempio n. 3
0
        public Msg(Bitmessage bm, Payload payload)
        {
            Status = Status.Invalid;
            try
            {
                int pos = payload.FirstByteAfterTime;
                _inventoryVector = payload.InventoryVector;

                Stream = payload.SentData.ReadVarInt(ref pos);

                byte[] encrypted = payload.SentData.ReadBytes(ref pos, payload.Length - pos);

                // TODO Check ask data

                byte[]     decryptedData   = null;
                PrivateKey myEncryptionKey = null;

                foreach (PrivateKey myKey in bm.ListMyAddresses())
                {
                    if (myKey.Stream != _stream)
                    {
                        continue;
                    }
                    try
                    {
                        decryptedData   = myKey.DecryptAES256CBC4Msg(encrypted);
                        myEncryptionKey = myKey;
                    }
                    // ReSharper disable EmptyGeneralCatchClause
                    catch
                    {
                    }                     // ReSharper restore EmptyGeneralCatchClause

                    if (decryptedData != null)
                    {
                        break;
                    }
                }

                if ((decryptedData == null) || (myEncryptionKey == null))
                {
                    Status = Status.Encrypted;
                    return;
                }

                pos = 0;

                Version = decryptedData.ReadVarInt(ref pos);
                var senderKey = new Pubkey(decryptedData, ref pos);

                if (!decryptedData.ReadBytes(ref pos, 20).SequenceEqual(myEncryptionKey.Hash))
                {
                    //print 'The original sender of this message did not send it to you. Someone is attempting a Surreptitious Forwarding Attack.'
                    //print 'See: http://world.std.com/~dtd/sign_encrypt/sign_encrypt7.html'
                    //print 'your toRipe:', toRipe.encode('hex')
                    //print 'embedded destination toRipe:', decryptedData[readPosition:readPosition + 20].encode('hex')
                    return;
                }

                KeyTo   = myEncryptionKey.Name;
                KeyFrom = senderKey.Name;

                EncodingType = (EncodingType)decryptedData.ReadVarInt(ref pos);
                decryptedData.ReadVarStrSubjectAndBody(ref pos, out _subject, out _body);

                UInt64 askDataLength = decryptedData.ReadVarInt(ref pos);
                _askData = decryptedData.ReadBytes(ref pos, (int)askDataLength);

                int    posOfEndMsg     = pos;
                UInt64 signatureLength = decryptedData.ReadVarInt(ref pos);
                byte[] signature       = decryptedData.ReadBytes(ref pos, (int)signatureLength);

                var data = new byte[posOfEndMsg];
                Buffer.BlockCopy(decryptedData, 0, data, 0, posOfEndMsg);

                if (data.ECDSAVerify(senderKey.SigningKey, signature))
                {
                    Status = Status.Valid;
                    senderKey.SaveAsync(bm.DB);
                }
            }
            catch
            {
                Status = Status.Invalid;
            }
        }