Example #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="Content"/> is <c>null</c>.
        /// </exception>
        public string ComputeContentMd5()
        {
            if (Content == null)
            {
                throw new InvalidOperationException("Cannot compute Md5 checksum without a ContentObject.");
            }

            using (var stream = Content.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));
            }
        }
Example #2
0
 private byte[] ComputeChecksum(IFileSystemDirectory root, string path)
 {
     using (var stream = root.ReadBinaryFile(path))
         using (var bufferedStream = new BufferedStream(stream, 1048576))
             using (var md5 = MD5.Create())
             {
                 return(md5.ComputeHash(bufferedStream));
             }
 }
Example #3
0
        public async Task <InputFile> UploadFile(string name, byte[] data)
        {
            var partSize = 65536;

            var file_id = DateTime.Now.Ticks;

            var partedData  = new Dictionary <int, byte[]>();
            var parts       = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(data.Length) / Convert.ToDouble(partSize)));
            var remainBytes = data.Length;

            for (int i = 0; i < parts; i++)
            {
                partedData.Add(i, data
                               .Skip(i * partSize)
                               .Take(remainBytes < partSize ? remainBytes : partSize)
                               .ToArray());

                remainBytes -= partSize;
            }

            for (int i = 0; i < parts; i++)
            {
                var saveFilePartRequest = new Upload_SaveFilePartRequest(file_id, i, partedData[i]);
                await _sender.Send(saveFilePartRequest);

                await _sender.Recieve(saveFilePartRequest);

                if (saveFilePartRequest.Done == false)
                {
                    throw new InvalidOperationException($"File part {i} does not uploaded");
                }
            }

            string md5_checksum;

            using (var md5 = MD5.Create())
            {
                var hash       = md5.ComputeHash(data);
                var hashResult = new StringBuilder(hash.Length * 2);

                for (int i = 0; i < hash.Length; i++)
                {
                    hashResult.Append(hash[i].ToString("x2"));
                }

                md5_checksum = hashResult.ToString();
            }

            var inputFile = new InputFileConstructor(file_id, parts, name, md5_checksum);

            return(inputFile);
        }
Example #4
0
 public static string GetMd5String(string source)
 {
     using (var md = SystemMd5.Create())
     {
         byte[]        bytes   = Encoding.UTF8.GetBytes(source);
         byte[]        buffer2 = md.ComputeHash(bytes);
         StringBuilder builder = new StringBuilder();
         foreach (byte num in buffer2)
         {
             builder.Append(num.ToString("x2"));
         }
         return(builder.ToString());
     }
 }
Example #5
0
 private static byte[] MD5File(string fname)
 {
     try
     {
         using (FileStream fs = new FileStream(fname, FileMode.Open, FileAccess.Read))
             using (MD5 md5 = MD5.Create())
             {
                 return(md5.ComputeHash(fs));
             }
     }
     catch
     {
         return(null);
     }
 }
Example #6
0
        /// <summary>
        /// Gets a MD5 hash.
        /// </summary>
        /// <param name="input">Input</param>
        /// <returns>Hashed input</returns>
        public static string GetMD5Hash(string input)
        {
            byte[] data     = null;
            var    sBuilder = new StringBuilder();

            using (var md5Hash = MD5Hasher.Create())
                data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            for (var i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return(sBuilder.ToString());
        }
Example #7
0
 private static string ComputeMD5(string path)
 {
     using (var md5 = MD5.Create())
     {
         using (var input = File.OpenRead(path))
         {
             var sb = new StringBuilder();
             foreach (var b in md5.ComputeHash(input))
             {
                 sb.Append(b.ToString("x2"));
             }
             return(sb.ToString());
         }
     }
 }
Example #8
0
        public async Task <InputFileConstructor> UploadFile(string name, Stream content)
        {
            var buffer = new byte[65536];
            var fileId = BitConverter.ToInt64(Helpers.GenerateRandomBytes(8), 0);

            int partsCount = 0;
            int bytesRead;

            while ((bytesRead = content.Read(buffer, 0, buffer.Length)) > 0)
            {
                var request = new SaveFilePartRequest(fileId, partsCount, buffer, 0, bytesRead);
                await SendRpcRequest(request);

                partsCount++;

                if (request.done == false)
                {
                    throw new InvalidOperationException($"Failed to upload file({name}) part: {partsCount})");
                }
            }

            var md5Checksum = string.Empty;

            if (content.CanSeek)
            {
                content.Position = 0;

                using (var md5 = MD5.Create())
                {
                    var hash       = md5.ComputeHash(content);
                    var hashResult = new StringBuilder(hash.Length * 2);

                    foreach (byte b in hash)
                    {
                        hashResult.Append(b.ToString("x2"));
                    }

                    md5Checksum = hashResult.ToString();
                }
            }

            return(new InputFileConstructor(fileId, partsCount, name, md5Checksum));
        }
Example #9
0
 public static string Encrypt(string str)
 {
     return(BitConverter.ToString(SysMD5.Create().ComputeHash(Encoding.Default.GetBytes(str))).Replace("-", "").ToLower());
 }