在下載檔案後,通常會使用 MD5SUM 等方式進行檔案驗證,驗證檔案的正確性,以及確認檔案

是否被竄改過,在網路上可以找到很多相關程式,在這邊則簡單的寫個 C# MD5SUM 程式。

using System;
using System.IO;
using System.Security.Cryptography;
 
class MD5SUM
{
    
    public static void Main(string[] args)
    {
        if( args.Length==1 && File.Exists(args[0]))
        {
            FileStream file = new FileStream(args[0], FileMode.Open);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(file);
            file.Close();                        
            Console.WriteLine(BitConverter.ToString(retVal).Replace("-",""));
        }
        else
        {
            Console.WriteLine("MD5SUM filename");                            
        }        
    }
}

使用方式:

將上面原始碼儲存成 MD5SUM.cs,輸入下方指令進行編譯(這邊用的是 .Net Framework 1.1)

2010-10-03_113859

輸入: md5sum 檔名 (拿 ubuntu iso 檔來做測試)。

2010-10-03_112146

對照一下 ubuntu 官方網站 MD5 Hash

2010-10-03_112227

檔案驗證正確。

 

參考網站:http://sharpertutorials.com/calculate-md5-checksum-file/

arrow
arrow
    全站熱搜

    iammic 發表在 痞客邦 留言(0) 人氣()