Beispiel #1
0
        /// <summary>
        /// Notifies the handle that a new response segment
        /// is available
        /// </summary>
        /// <param name="message">The complex ack message</param>
        /// <param name="segment">The response segment</param>
        public override void FeedComplexAck(ComplexAckMessage message, BufferSegment segment)
        {
            _buffers.Add(segment);

            if (!message.MoreFollows)
            {
                try
                {
                    // TODO: Is this the best place to do this? Decoding
                    // large requests could potentially be more time than
                    // we want to spent in the transaction's lock

                    using (var stream = new MultiBufferStream(_buffers))
                    {
                        var tagReader       = new TagReader(stream);
                        var tagReaderStream = new TagReaderStream(tagReader, Value <TAck> .Schema);
                        var ack             = Value <TAck> .Load(tagReaderStream);

                        _source.SetResult(ack);
                    }
                }
                catch (Exception e)
                {
                    _source.SetException(new RejectException(RejectReason.InvalidTag));
                }
            }
        }
        public void ComputeHashOnMultiBufferStream()
        {
            var firstArray  = Encoding.ASCII.GetBytes("this is good");
            var secondArray = Encoding.ASCII.GetBytes("this is bad");

            using (var stream = new MultiBufferStream()) {
                stream.AddBuffer(firstArray);
                stream.AddBuffer(secondArray);

                using (var sha256Hash = new SHA256Managed()) {
                    var hash = sha256Hash.ComputeHash(stream);

                    var hashString = HashToString(hash);

                    //Хеш для строки 'this is goodthis is bad', вычисленный с помощью внешней программы
                    var verifyHash = "B61A0664186896AF7D947E6A56DEB8D608FDF092E515DB531834FDE7DBFCAF79";
                    Assert.That(hashString, Is.EqualTo(verifyHash).IgnoreCase);

                    hashString = Convert.ToBase64String(hash);

                    var allStringHash = Convert.ToBase64String(sha256Hash.ComputeHash(Encoding.ASCII.GetBytes("this is goodthis is bad")));
                    Assert.That(hashString, Is.EqualTo(allStringHash).IgnoreCase);
                }
            }
        }
Beispiel #3
0
        public static string GetSHA256Hash(this Mime mime)
        {
            using (var stream = new MultiBufferStream()) {
                if (!String.IsNullOrWhiteSpace(mime.MainEntity.Subject))
                {
                    stream.AddBuffer(Encoding.ASCII.GetBytes(mime.MainEntity.Subject));
                }
                if (!String.IsNullOrWhiteSpace(mime.BodyText))
                {
                    stream.AddBuffer(Encoding.ASCII.GetBytes(mime.BodyText));
                }
                if (String.IsNullOrWhiteSpace(mime.BodyText) && !String.IsNullOrWhiteSpace(mime.BodyHtml))
                {
                    var convertedHtml = mime.HtmlToText();
                    if (!String.IsNullOrWhiteSpace(convertedHtml))
                    {
                        stream.AddBuffer(Encoding.ASCII.GetBytes(convertedHtml));
                    }
                }

                var attachments = mime.GetValidAttachements().OrderBy(m => m.GetFilename());

                foreach (var attachment in attachments)
                {
                    stream.AddBuffer(attachment.Data);
                }

                if (stream.Length > 0)
                {
                    using (var sha256Hash = new SHA256Managed()) {
                        var hash = sha256Hash.ComputeHash(stream);

                        return(Convert.ToBase64String(hash));
                    }
                }

                return(String.Empty);
            }
        }