c#如何给文件计算MD5

在项目中,当我们需要把数据文件上传到ftp时,为了保证数据的完整性,我们会计算数据文件的MD5,并把MD5文件和数据文件一同上传。

坚守“ 做人真诚 · 做事靠谱 · 口碑至上 · 高效敬业 ”的价值观,专业网站建设服务10余年为成都成都人造雾小微创业公司专业提供企业网站制作营销网站建设商城网站建设手机网站建设小程序网站建设网站改版,从内容策划、视觉设计、底层架构、网页布局、功能开发迭代于一体的高端网站建设服务。

那么如何产生数据文件的MD5呢?

我们可以通过System.Security.Cryptography.MD5类来

private void WriteMD5(string zipFile, string ctrlFile)
        {
            // Add hash code to ctrl file
            byte[] validHash = GenerateMD5Hash(zipFile);
            // make sure that we add hash value in a new line
            bool addNewLine = false;
            using (StreamReader sr = new StreamReader(ctrlFile))
            {
                string[] lines = sr.ReadToEnd().Split(new char[] { '\n' });
                if (lines.Length == 1) addNewLine = true;
            }

            // ready to put the control file, so add the hash string to it
            using (StreamWriter sw = new StreamWriter(ctrlFile, true))
            {
                if (addNewLine) sw.WriteLine("");
                sw.WriteLine(BitConverter.ToString(validHash).Replace("-", ""));
            }
        }

private byte[] GenerateMD5Hash(string localFileName)
        {
            byte[] localHash = null;
            using (MD5 md5Hash = MD5.Create())
            {
                using (FileStream localFile = File.OpenRead(localFileName))
                {
                    localHash = md5Hash.ComputeHash(localFile);
                }
            }
            return localHash;
        }


网站名称:c#如何给文件计算MD5
网页地址:http://lszwz.com/article/gpjsjh.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款