public void HashableFile() { var precomputedHashes = new Dictionary <HashProvider, string>() { { HashProvider.SHA256, @"7zVoltYfxNG3135m1sAy0gHa1FAnfPX8f6/7gXj0bhQ=" }, { HashProvider.SHA384, @"k+a9v4mKAMpp0c+QvyIcF/vo7E7bsg+4D0VRWxCPzlMYOhqO+LsOTLrjqT2HcXWL" }, { HashProvider.SHA512, @"+NLCPuBL1SKOVctXmBP6jI58yY2KEm38iZWNMJA/SnUAh03AR0p6M4u9VsElKnrJnSxfPdtj1BuLGm9sYH4A3Q==" } }; var testFilePath = "HashableFileTest.txt"; File.WriteAllText(testFilePath, "DE85CD8E-87CB-45AE-A07E-F7863B9065B7"); foreach (HashProvider provider in Enum.GetValues(typeof(HashProvider))) { var hf = new HashableFile(testFilePath); hf.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 computedHashString = hf.ComputedHash.ToString(); Assert.AreEqual(computedHashString, precomputedTestHash, "Computed and stored hash differ"); } }
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 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()); } }