using System.Security.Cryptography; using System.Text; // Create a new instance of SHA1CryptoServiceProvider SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider(); // Convert string to byte array byte[] inputBytes = Encoding.UTF8.GetBytes("Hello, world!"); // Compute the hash value for the inputBytes byte[] hashValue = sha1.ComputeHash(inputBytes); // Print the hash value as a hex string Console.WriteLine(BitConverter.ToString(hashValue).Replace("-", ""));
using System.IO; using System.Security.Cryptography; // Create a new instance of SHA1CryptoServiceProvider SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider(); // Open the file for reading FileStream stream = new FileStream("largefile.bin", FileMode.Open); // Compute the hash value for the file in 1024-byte chunks byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) { sha1.TransformBlock(buffer, 0, bytesRead, buffer, 0); } // Finalize the hash computation and store the hash value sha1.TransformFinalBlock(buffer, 0, 0); byte[] hashValue = sha1.Hash; // Print the hash value as a hex string Console.WriteLine(BitConverter.ToString(hashValue).Replace("-", ""));In this example, we open a large file and read it in chunks of 1024 bytes. The TransformBlock method is used to compute the SHA1 hash value for each chunk. Once all chunks have been processed, the TransformFinalBlock method is called to finalize the hash computation and store the hash value. Finally, the hash value is printed as a hex string. The package library for the SHA1CryptoServiceProvider class is included in the .NET Framework, which provides a set of common functionality and is available in all .NET programming languages.