When I first began to learn programming, I always wanted to write some small software gadgets.
And this is the classic file MD5 checksum, by the way add a SHA1.
When downloading something on the Web, the author provides the MD5 value.
What it does is that we can do a MD5 check on the downloaded files after downloading the software, to make sure we get the same files as the files provided by the site.
So you need a MD5 verification tool, then go to Baidu, but the domestic download station is needless to say ... A bundle of things is impossible to guard against.
So, as a programmer, write one yourself.
The main requirement for analyzing this gadget is
1. Locate the file according to the path
2. Get MD5
3. Get SHA1
A very simple gadget,
The first method passes directly to the path using the FileInfo class constructor, which is handy because it is possible to drag and drop files directly from the console.
Note that there are spaces in the path that will cause an error.
The method in which s represents the incoming file path
Static void GetFile(string s)
{
Try
{
FileInfo fi = new FileInfo(s);
Console.WriteLine("file path: {0}", s);
Console.WriteLine("File name: {0}", fi.Name.ToString());
Console.WriteLine("file type: {0}", fi.Extension.ToString());
Console.WriteLine("file size: {0} K", fi.Length / 1024);
Console.WriteLine("file creation time: {0}", fi.CreationTime.ToString());
Console.WriteLine("last access time: {0}", fi.LastAccessTime.ToString());
Console.WriteLine("last write time: {0}", fi.LastWriteTime.ToString());
}
Catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
The second method obtains the MD5 value, in which s represents the incoming file path
MD5 the entire file as a large text message, through its irreversible string transform algorithm, produces this unique MD5 message digest.
Static void GetMD5(string s)
{
Try
{
FileStream file = new FileStream(s,FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();
Byte[] retval = md5.ComputeHash(file);
file.Close();
StringBuilder sc = new StringBuilder();
For (int i = 0 ; i<retval.Length ; i++ )
{
sc.Append(retval[i].ToString("x2"));
}
Console.WriteLine("file MD5:{0}",sc);
}
Catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
The third method, obtaining the SHA1 value, in which s represents the file path passed in
The SHA1 is called a secure Hash algorithm, and SHA1 produces a 160-bit message digest for messages that are less than 2^64 bits in length.
SHA1 has the following characteristics: It is not possible to recover information from the message digest; two different messages will not produce the same message digest (but there will be a 1x10 ^ 48 probability of the same message digest, generally ignored when used).
static void GetSHA1(string s)
{
try
{
FileStream file = new FileStream(s, FileMode.Open);
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] retval = sha1.ComputeHash(file);
file.Close();
StringBuilder sc = new StringBuilder();
for (int i = 0; i < retval.Length; i++)
{
sc.Append(retval[i].ToString("x2"));
}
Console.WriteLine("文件SHA1:{0}", sc);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}