您好,登錄后才能下訂單哦!
在項(xiàng)目中,當(dāng)我們需要把數(shù)據(jù)文件上傳到ftp時(shí),為了保證數(shù)據(jù)的完整性,我們會(huì)計(jì)算數(shù)據(jù)文件的MD5,并把MD5文件和數(shù)據(jù)文件一同上傳。
那么如何產(chǎn)生數(shù)據(jù)文件的MD5呢?
我們可以通過(guò)System.Security.Cryptography.MD5類來(lái)
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;
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。