public void HashableBytesSerializeDeserialize() { foreach (HashProvider provider in Enum.GetValues(typeof(HashProvider))) { var h1 = new HashableBytes(Guid.NewGuid().ToByteArray()); h1.ComputeHash(provider); var h1s = Newtonsoft.Json.JsonConvert.SerializeObject(h1); var h1d = Newtonsoft.Json.JsonConvert.DeserializeObject <HashableBytes>(h1s); //verify before recomputing Assert.IsTrue(h1.Verify()); Assert.IsTrue(h1d.Verify()); //check that the hash has not changed Assert.AreEqual(h1.ComputedHash, h1d.ComputedHash); Assert.AreEqual(h1.Value, h1d.Value); //verify after recomputing h1.ComputeHash(provider); h1d.ComputeHash(provider); Assert.IsTrue(h1.Verify()); Assert.IsTrue(h1d.Verify()); //check that the hash has not changed Assert.AreEqual(h1.ComputedHash, h1d.ComputedHash); Assert.AreEqual(h1.Value, h1d.Value); } }
static void Main(string[] args) { // Make some values to hash string stringToHash = "Easy!"; byte[] bytesToHash = new byte[] { 0x45, 0x61, 0x73, 0x79, 0x21 }; Stream streamToHash = new MemoryStream(new byte[] { 0x45, 0x61, 0x73, 0x79, 0x21 }); File.WriteAllText("CryptLinkDemo.txt", "Easy!"); Stream fileToHash = new FileStream("CryptLinkDemo.txt", FileMode.Open); // Using Extentions stringToHash.ComputeHash(HashProvider.SHA256); bytesToHash.ComputeHash(HashProvider.SHA256); streamToHash.ComputeHash(HashProvider.SHA256); fileToHash.ComputeHash(HashProvider.SHA256); // Using Hash static methods Hash.Compute(stringToHash, HashProvider.SHA256); Hash.Compute(bytesToHash, HashProvider.SHA256); Hash.Compute(streamToHash, HashProvider.SHA256); Hash.Compute(fileToHash, HashProvider.SHA256); // Instanced examples, the value and hash are combined into a meta object // HashableString, holds the original string and the hash var hashableString = new HashableString("Easy!", HashProvider.SHA256); // HashableBytes, holds the original set of bytes and the hash - best for small arrays of bytes var hashableBytes = new HashableBytes(new byte[] { 0x45, 0x61, 0x73, 0x79, 0x21 }, HashProvider.SHA256); // HashableFile, holds a refernce to a local file path and the hash var hashableFile = new HashableFile("CryptLinkDemo.txt", HashProvider.SHA256); var widget = new HashableWidgetExample() { ID = 0, Name = "Widget", Price = 100, PurchaseCount = 1000000 }; widget.ComputeHash(HashProvider.SHA256); Console.WriteLine(widget.ComputedHash); using (X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine)) { store.Open(OpenFlags.ReadOnly); X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySerialNumber, "123456", true); var cert = new Cert(certs[0]); widget.ComputeHash(HashProvider.SHA256, cert); widget.Verify(cert); } }
public Hash StoreString([FromForm] string value) { if (value == null) { throw new ArgumentException("Value was null"); } var encoding = new ASCIIEncoding(); var hs = new HashableBytes(encoding.GetBytes(value), provider); hs.ComputeHash(provider, null); memStore.StoreItem(hs); return(hs.ComputedHash); }
public void HashingPerformance() { var results = GetTestSizeString(); var hItem = new HashableBytes(GetBytes()); foreach (HashProvider provider in Enum.GetValues(typeof(HashProvider))) { long hashCount = 0; DateTime startTime = DateTime.Now; while ((startTime + TestLength) > DateTime.Now) { hItem.ComputeHash(provider); hashCount++; } results += GetResultString(provider, hashCount); } Assert.Pass(results); }
public void NextNode() { var c = new ConsistentHash <HashableBytes>(HashProvider.SHA384); var firstItem = new HashableBytes(new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }); var secondItem = new HashableBytes(new byte[] { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 }); var thirdItem = new HashableBytes(new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }); firstItem.ComputeHash(c.Provider, null); secondItem.ComputeHash(c.Provider, null); thirdItem.ComputeHash(c.Provider, null); c.Add(secondItem, true, 0); var founditem = c.GetNode(firstItem.ComputedHash); Assert.True(secondItem == founditem); var founditem2 = c.GetNode(thirdItem.ComputedHash); Assert.True(secondItem == founditem2); }
public void SigningPerformance() { var results = GetTestSizeString(); var hItem = new HashableBytes(GetBytes()); var signingCert = Cert.LoadFromPfx("Certs/ca1.pfx_testonly_donotuse", ""); foreach (HashProvider provider in Enum.GetValues(typeof(HashProvider))) { long hashCount = 0; DateTime startTime = DateTime.Now; while ((startTime + TestLength) > DateTime.Now) { hItem.ComputeHash(provider, signingCert); hashCount++; } results += GetResultString(provider, hashCount); } Assert.Pass(results); }
public void HashableFromsEquate() { var precomputedHashes = new Dictionary <HashProvider, string>() { { HashProvider.SHA256, @"AYSLdKSodmdJQlMzKiDoAZlUL09GgLyHr9VTZlhQtkg=" }, { HashProvider.SHA384, @"TDQMTam7Wy20bTaD0vV7mCd760L4DNmsp55cgPnltRvVVf18qtTi/6ryuR4bXvJm" }, { HashProvider.SHA512, @"NMp1zZdwa+MmPPwe6YoPe0eS1UxXk0sBCJN6tEzD0/BCHsqk9P282nwS3paL+XJRSdipcCb3EObB7F5r/qe6xQ==" } }; var testFilePath = "HashableFromsEquate.txt"; var testString = "60EC9927-35B3-4CCB-9791-56D0FF00F07B"; File.WriteAllText(testFilePath, testString); var hBytes = new HashableBytes(Encoding.ASCII.GetBytes(testString)); var hString = new HashableString(testString); var hFile = new HashableFile(testFilePath); var hStream = new HashableStream(new MemoryStream(Encoding.ASCII.GetBytes(testString))); foreach (HashProvider provider in Enum.GetValues(typeof(HashProvider))) { hBytes.ComputeHash(provider); hString.ComputeHash(provider); hFile.ComputeHash(provider); hStream.ComputeHash(provider); Assert.True(precomputedHashes.ContainsKey(provider), "The stored test hash dictionary has a comparison Hash, and same as HashableString"); var precomputedTestHash = precomputedHashes[provider]; var hBytesString = hBytes.ComputedHash.ToString(); Assert.AreEqual(hBytesString, precomputedTestHash, "Computed and stored hash differ"); Assert.AreEqual(hBytesString, hString.ComputedHash.ToString()); Assert.AreEqual(hBytesString, hFile.ComputedHash.ToString()); Assert.AreEqual(hBytesString, hStream.ComputedHash.ToString()); } }
public Hash StoreBinary(IFormFile file) { if (file == null) { throw new ArgumentException("Upload payload was null"); } var ms = new MemoryStream(); file.CopyTo(ms); var hs = new HashableBytes(ms.ToArray(), provider); ms.Dispose(); hs.ComputeHash(provider, null); memStore.StoreItem(hs); Console.WriteLine(hs.ComputedHash.ToString()); Response.StatusCode = 200; Response.ContentType = "text/plain"; return(hs.ComputedHash); }