예제 #1
0
        public static SignatureMessage FromSignatureMessageBlock(ArraySegment <byte> message, IExchangeDecrypt privateKey)
        {
            if (message.Array == null)
            {
                throw new ArgumentNullException("message.Array");
            }
            if (privateKey == null)
            {
                throw new ArgumentNullException("privateKey");
            }

            try
            {
                using (Stream typeStream = new MemoryStream(message.Array, message.Offset, message.Count))
                    using (Stream cryptoStream = ContentConverter.RemoveType(typeStream, "SignatureMessage"))
                        using (Stream hashStream = ContentConverter.Decrypt(cryptoStream, privateKey))
                            using (Stream paddingStream = ContentConverter.RemoveHash(hashStream))
                                using (Stream compressStream = ContentConverter.RemovePadding(paddingStream))
                                    using (Stream messageStream = ContentConverter.Decompress(compressStream))
                                    {
                                        return(SignatureMessage.Import(messageStream, _bufferManager));
                                    }
            }
            catch (Exception)
            {
                return(null);
            }
        }
예제 #2
0
        public static ArraySegment <byte> ToSignatureMessageBlock(SignatureMessage message, IExchangeEncrypt publicKey)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            if (publicKey == null)
            {
                throw new ArgumentNullException("publicKey");
            }

            ArraySegment <byte> value;

            using (Stream messageStream = message.Export(_bufferManager))
                using (Stream compressStream = ContentConverter.Compress(messageStream))
                    using (Stream paddingStream = ContentConverter.AddPadding(compressStream, 1024 * 256))
                        using (Stream hashStream = ContentConverter.AddHash(paddingStream))
                            using (Stream cryptostream = ContentConverter.Encrypt(hashStream, publicKey))
                                using (Stream typeStream = ContentConverter.AddType(cryptostream, "SignatureMessage"))
                                {
                                    value = new ArraySegment <byte>(_bufferManager.TakeBuffer((int)typeStream.Length), 0, (int)typeStream.Length);
                                    typeStream.Read(value.Array, value.Offset, value.Count);
                                }

            return(value);
        }
예제 #3
0
        public SignatureMessage GetMessage(SignatureMessageMetadata metadata, ExchangePrivateKey exchangePrivateKey)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException("metadata");
            }
            if (exchangePrivateKey == null)
            {
                throw new ArgumentNullException("exchangePrivateKey");
            }

            lock (this.ThisLock)
            {
                if (!_cacheManager.Contains(metadata.Key))
                {
                    _connectionsManager.Download(metadata.Key);

                    return(null);
                }
                else
                {
                    ArraySegment <byte> buffer = new ArraySegment <byte>();

                    try
                    {
                        buffer = _cacheManager[metadata.Key];

                        var message = ContentConverter.FromSignatureMessageBlock(buffer, exchangePrivateKey);

                        if (metadata.Signature != message.Signature ||
                            metadata.CreationTime != message.CreationTime ||
                            metadata.Certificate.ToString() != message.Certificate.ToString())
                        {
                            return(null);
                        }

                        this.Lock(metadata.Key);

                        return(message);
                    }
                    catch (Exception)
                    {
                    }
                    finally
                    {
                        if (buffer.Array != null)
                        {
                            _bufferManager.ReturnBuffer(buffer.Array);
                        }
                    }

                    return(null);
                }
            }
        }
예제 #4
0
        public ChatTopic GetMessage(ChatTopicMetadata metadata)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException("metadata");
            }

            lock (this.ThisLock)
            {
                if (!_cacheManager.Contains(metadata.Key))
                {
                    _connectionsManager.Download(metadata.Key);

                    return(null);
                }
                else
                {
                    ArraySegment <byte> buffer = new ArraySegment <byte>();

                    try
                    {
                        buffer = _cacheManager[metadata.Key];

                        var message = ContentConverter.FromChatTopicBlock(buffer);

                        if (metadata.Tag != message.Tag ||
                            metadata.CreationTime != message.CreationTime ||
                            metadata.Certificate.ToString() != message.Certificate.ToString())
                        {
                            return(null);
                        }

                        this.Lock(metadata.Key);

                        return(message);
                    }
                    catch (Exception)
                    {
                    }
                    finally
                    {
                        if (buffer.Array != null)
                        {
                            _bufferManager.ReturnBuffer(buffer.Array);
                        }
                    }

                    return(null);
                }
            }
        }
예제 #5
0
        public static ArraySegment <byte> ToChatMessageBlock(ChatMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            ArraySegment <byte> value;

            using (Stream messageStream = message.Export(_bufferManager))
                using (Stream compressStream = ContentConverter.Compress(messageStream))
                    using (Stream typeStream = ContentConverter.AddType(compressStream, "ChatMessage"))
                    {
                        value = new ArraySegment <byte>(_bufferManager.TakeBuffer((int)typeStream.Length), 0, (int)typeStream.Length);
                        typeStream.Read(value.Array, value.Offset, value.Count);
                    }

            return(value);
        }
예제 #6
0
        public static ChatMessage FromChatMessageBlock(ArraySegment <byte> message)
        {
            if (message.Array == null)
            {
                throw new ArgumentNullException("message.Array");
            }

            try
            {
                using (Stream typeStream = new MemoryStream(message.Array, message.Offset, message.Count))
                    using (Stream compressStream = ContentConverter.RemoveType(typeStream, "ChatMessage"))
                        using (Stream messageStream = ContentConverter.Decompress(compressStream))
                        {
                            return(ChatMessage.Import(messageStream, _bufferManager));
                        }
            }
            catch (Exception)
            {
                return(null);
            }
        }
예제 #7
0
        private void UploadThread()
        {
            for (; ;)
            {
                Thread.Sleep(1000 * 1);
                if (this.State == ManagerState.Stop)
                {
                    return;
                }

                {
                    UploadItem item = null;

                    lock (this.ThisLock)
                    {
                        if (_settings.UploadItems.Count > 0)
                        {
                            item = _settings.UploadItems[0];
                        }
                    }

                    try
                    {
                        if (item != null)
                        {
                            ArraySegment <byte> buffer = new ArraySegment <byte>();

                            try
                            {
                                if (item.Type == "Profile")
                                {
                                    buffer = ContentConverter.ToProfileBlock(item.Profile);
                                }
                                else if (item.Type == "SignatureMessage")
                                {
                                    buffer = ContentConverter.ToSignatureMessageBlock(item.SignatureMessage, item.ExchangePublicKey);
                                }
                                else if (item.Type == "WikiDocument")
                                {
                                    buffer = ContentConverter.ToWikiDocumentBlock(item.WikiDocument);
                                }
                                else if (item.Type == "ChatTopic")
                                {
                                    buffer = ContentConverter.ToChatTopicBlock(item.ChatTopic);
                                }
                                else if (item.Type == "ChatMessage")
                                {
                                    buffer = ContentConverter.ToChatMessageBlock(item.ChatMessage);
                                }

                                Key key = null;

                                {
                                    if (_hashAlgorithm == HashAlgorithm.Sha256)
                                    {
                                        key = new Key(Sha256.ComputeHash(buffer), _hashAlgorithm);
                                    }

                                    this.Lock(key);
                                }

                                _cacheManager[key] = buffer;
                                _connectionsManager.Upload(key);

                                var miner = new Miner(CashAlgorithm.Version1, item.MiningLimit, item.MiningTime);

                                var task = Task.Factory.StartNew(() =>
                                {
                                    if (item.Type == "Profile")
                                    {
                                        var metadata = new ProfileMetadata(item.Profile.CreationTime, key, item.DigitalSignature);
                                        _connectionsManager.Upload(metadata);
                                    }
                                    else if (item.Type == "SignatureMessage")
                                    {
                                        var metadata = new SignatureMessageMetadata(item.SignatureMessage.Signature, item.SignatureMessage.CreationTime, key, miner, item.DigitalSignature);
                                        _connectionsManager.Upload(metadata);
                                    }
                                    else if (item.Type == "WikiDocument")
                                    {
                                        var metadata = new WikiDocumentMetadata(item.WikiDocument.Tag, item.WikiDocument.CreationTime, key, miner, item.DigitalSignature);
                                        _connectionsManager.Upload(metadata);
                                    }
                                    else if (item.Type == "ChatTopic")
                                    {
                                        var metadata = new ChatTopicMetadata(item.ChatTopic.Tag, item.ChatTopic.CreationTime, key, miner, item.DigitalSignature);
                                        _connectionsManager.Upload(metadata);
                                    }
                                    else if (item.Type == "ChatMessage")
                                    {
                                        var metadata = new ChatMessageMetadata(item.ChatMessage.Tag, item.ChatMessage.CreationTime, key, miner, item.DigitalSignature);
                                        _connectionsManager.Upload(metadata);
                                    }
                                });

                                while (!task.IsCompleted)
                                {
                                    if (this.State == ManagerState.Stop)
                                    {
                                        miner.Cancel();
                                    }

                                    lock (this.ThisLock)
                                    {
                                        if (!_settings.UploadItems.Contains(item))
                                        {
                                            miner.Cancel();
                                        }
                                    }

                                    Thread.Sleep(1000);
                                }

                                if (task.Exception != null)
                                {
                                    throw task.Exception;
                                }

                                lock (this.ThisLock)
                                {
                                    _settings.UploadItems.Remove(item);
                                }
                            }
                            finally
                            {
                                if (buffer.Array != null)
                                {
                                    _bufferManager.ReturnBuffer(buffer.Array);
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }