Ejemplo n.º 1
0
        /// <summary>
        /// Computes the MD5 checksum of the content.
        /// </summary>
        /// <remarks>
        /// Computes the MD5 checksum of the MIME content in its canonical
        /// format and then base64-encodes the result.
        /// </remarks>
        /// <returns>The md5sum of the content.</returns>
        /// <exception cref="System.InvalidOperationException">
        /// The <see cref="ContentObject"/> is <c>null</c>.
        /// </exception>
        public string ComputeContentMd5()
        {
            if (ContentObject == null)
            {
                throw new InvalidOperationException("Cannot compute Md5 checksum without a ContentObject.");
            }

            using (var stream = ContentObject.Open()) {
                byte[] checksum;

                using (var filtered = new FilteredStream(stream)) {
                    if (ContentType.IsMimeType("text", "*"))
                    {
                        filtered.Add(new Unix2DosFilter());
                    }

                    using (var md5 = MD5.Create())
                        checksum = md5.ComputeHash(filtered);
                }

                var base64 = new Base64Encoder(true);
                var digest = new byte[base64.EstimateOutputLength(checksum.Length)];
                int n      = base64.Flush(checksum, 0, checksum.Length, digest);

                return(Encoding.ASCII.GetString(digest, 0, n));
            }
        }
Ejemplo n.º 2
0
 public override void WriteValue(byte[] value)
 {
     base.WriteValue(value);
     if (value != null)
     {
         _writer.Write(_quoteChar);
         Base64Encoder.Encode(value, 0, value.Length);
         Base64Encoder.Flush();
         _writer.Write(_quoteChar);
     }
 }
Ejemplo n.º 3
0
        static string GenerateBoundary()
        {
            var base64 = new Base64Encoder(true);
            var rand   = new Random();
            var digest = new byte[16];
            var b64buf = new byte[24];
            int length;

            rand.NextBytes(digest);
            length = base64.Flush(digest, 0, 16, b64buf);

            return("=-" + Encoding.ASCII.GetString(b64buf, 0, length));
        }
Ejemplo n.º 4
0
        static string GenerateBoundary()
        {
            var base64 = new Base64Encoder(true);
            var digest = new byte[16];
            var buf    = new byte[24];
            int length;

            MimeUtils.GetRandomBytes(digest);

            length = base64.Flush(digest, 0, digest.Length, buf);

            return("=-" + Encoding.ASCII.GetString(buf, 0, length));
        }
 /// <summary>
 /// Writes a <see cref="Byte"/>[] value.
 /// </summary>
 /// <param name="value">The <see cref="Byte"/>[] value to write.</param>
 public override void WriteValue(byte[] value)
 {
     if (value == null)
     {
         WriteNull();
     }
     else
     {
         InternalWriteValue(JsonToken.Bytes);
         _writer.Write(_quoteChar);
         Base64Encoder.Encode(value, 0, value.Length);
         Base64Encoder.Flush();
         _writer.Write(_quoteChar);
     }
 }
Ejemplo n.º 6
0
        static string GenerateBoundary()
        {
            var base64 = new Base64Encoder(true);
            var digest = new byte[16];
            var b64buf = new byte[24];
            int length;

            using (var rand = new RNGCryptoServiceProvider()) {
                rand.GetBytes(digest);
            }

            length = base64.Flush(digest, 0, 16, b64buf);

            return("=-" + Encoding.ASCII.GetString(b64buf, 0, length));
        }
Ejemplo n.º 7
0
        static string GenerateBoundary()
        {
            var base64 = new Base64Encoder(true);
            var digest = new byte[16];
            int length;

            MimeUtils.GetRandomBytes(digest);

            var buf = ArrayPool <byte> .Shared.Rent(24);

            try {
                length = base64.Flush(digest, 0, digest.Length, buf);

                return("=-" + Encoding.ASCII.GetString(buf, 0, length));
            } finally {
                ArrayPool <byte> .Shared.Return(buf);
            }
        }