public override void Handle(Communication.ServiceResponse response)
        {
            if (response.Headers.ContainsKey(HttpHeaders.HashCrc64Ecma))
            {
                ulong crc64    = 0;
                bool  checkCrc = true;
                foreach (var part in _request.PartETags)
                {
                    if (!String.IsNullOrEmpty(part.Crc64) && part.Length != 0)
                    {
                        crc64 = Crc64.Combine(crc64, ulong.Parse(part.Crc64), part.Length);
                    }
                    else
                    {
                        checkCrc = false;
                    }
                }

                if (!checkCrc) // the request does not have CRC64 for at least one part, skip the check
                {
                    return;
                }

                var ossCalculatedHashStr = response.Headers[HttpHeaders.HashCrc64Ecma];
                if (!crc64.ToString().Equals(ossCalculatedHashStr))
                {
                    response.Dispose();
                    throw new ClientException("Crc64 validation failed. Expected hash not equal to calculated hash");
                }
            }
        }
        private bool CompareCrc64(string localFile, string crc64ecma)
        {
            Crc64.InitECMA();
            String hash = String.Empty;

            using (FileStream fs = File.Open(localFile, FileMode.Open))
            {
                byte[] buffer = new byte[2048];
                int    bytesRead;
                ulong  crc = 0;

                while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ulong partCrc = Crc64.Compute(buffer, 0, bytesRead);
                    if (crc == 0)
                    {
                        crc = partCrc;
                    }
                    else
                    {
                        crc = Crc64.Combine(crc, partCrc, bytesRead);
                    }
                }

                localFileCrc64 = crc.ToString();
                return(localFileCrc64 == crc64ecma);
            }
        }
Beispiel #3
0
        public void TestCombine()
        {
            string str1 = "this is a test.";
            string str2 = "hello world.";
            string str  = str1 + str2;

            byte[] content1 = Encoding.ASCII.GetBytes(str1);
            byte[] content2 = Encoding.ASCII.GetBytes(str2);
            byte[] content  = Encoding.ASCII.GetBytes(str);

            Crc64.InitECMA();
            ulong crc1 = Crc64.Compute(content1, 0, content1.Length);
            ulong crc2 = Crc64.Compute(content2, 0, content2.Length);
            ulong crc  = Crc64.Compute(content, 0, content.Length);

            Assert.AreEqual(crc, Crc64.Combine(crc1, crc2, content2.Length));

            ulong crc3 = Crc64.Compute(content2, 0, content2.Length, crc1);

            Assert.AreEqual(crc3, crc);
        }
Beispiel #4
0
        public void TestCrcCombine()
        {
            string text = "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley";

            byte[] content = Encoding.ASCII.GetBytes(text);
            Crc64.InitECMA();
            ulong crc = Crc64.Compute(content, 0, content.Length);

            for (int i = 1; i < text.Length; i++)
            {
                string before   = text.Substring(0, i);
                string after    = text.Substring(i);
                byte[] content1 = Encoding.ASCII.GetBytes(before);
                byte[] content2 = Encoding.ASCII.GetBytes(after);

                ulong crc1 = Crc64.Compute(content1, 0, content1.Length);
                ulong crc2 = Crc64.Compute(content2, 0, content2.Length);
                Assert.AreEqual(crc, Crc64.Combine(crc1, crc2, content2.Length));

                ulong crc3 = Crc64.Compute(content2, 0, content2.Length, crc1);
                Assert.AreEqual(crc3, crc);
            }
        }
        private void Validate(DownloadObjectRequest request, ResumableDownloadContext resumableContext)
        {
            if (_conf.EnalbeMD5Check && !string.IsNullOrEmpty(resumableContext.ContentMd5))
            {
                using (var fs = File.Open(GetTempDownloadFile(request), FileMode.Open))
                {
                    string calcuatedMd5 = OssUtils.ComputeContentMd5(fs, fs.Length);
                    if (calcuatedMd5 != resumableContext.ContentMd5)
                    {
                        throw new OssException(string.Format("The Md5 of the downloaded file {0} does not match the expected. Expected:{1}, actual:{2}",
                                                             GetTempDownloadFile(request),
                                                             resumableContext.ContentMd5,
                                                             calcuatedMd5
                                                             ));
                    }
                }
            }
            else if (_conf.EnableCrcCheck && !string.IsNullOrEmpty(resumableContext.Crc64))
            {
                ulong calculatedCrc = 0;
                foreach (var part in resumableContext.PartContextList)
                {
                    calculatedCrc = Crc64.Combine(calculatedCrc, part.Crc64, part.Length);
                }

                if (calculatedCrc.ToString() != resumableContext.Crc64)
                {
                    throw new OssException(string.Format("The Crc64 of the downloaded file {0} does not match the expected. Expected:{1}, actual:{2}",
                                                         GetTempDownloadFile(request),
                                                         resumableContext.Crc64,
                                                         calculatedCrc
                                                         ));
                }
            }

            File.Move(GetTempDownloadFile(request), request.DownloadFile);
        }