using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using NUnit.Framework; using Pithos.Interfaces; namespace Pithos.Network.Test { [TestFixture] class ChecksumTest { private ICloudClient client; [SetUp] public void Setup() { var account = "890329@vho.grnet.gr"; var apiKey = "24989dce4e0fcb072f8cb60c8922be19"; client = new CloudFilesClient(account, apiKey); client.Authenticate(); } [Test] public void TestChecksum() { Assert.DoesNotThrow(() => { var filePath = "devguide.pdf"; var info=new FileInfo(filePath); var hash = CalculateHash(filePath); client.PutObject(null, "Shares", info.Name, filePath); var meta = client.GetObjectInfo(null, "Shares", "DeveloperGuide.pdf"); Assert.IsNotEmpty(meta.Hash); Assert.AreEqual(hash,meta.Hash,String.Format("The hashes don't match, expected {0} but got {1}",hash,meta.Hash)); }); } private static string CalculateHash(string fileName) { string hash; using (var hasher = MD5.Create()) using(var stream=File.OpenRead(fileName)) { var hashBuilder = new StringBuilder(); foreach (byte b in hasher.ComputeHash(stream)) hashBuilder.Append(b.ToString("x2").ToLower()); hash = hashBuilder.ToString(); } return hash; } } }