我的代码从SFTP服务器下载文件,而下载时我想计算即将要下载的文件的Sha256哈希代码。这样我就不会下载重复的文件。
我目前正在Renci.SshNet
和Renci.SshNet.sftp
上进行SFTP下载操作。它们成功完成。唯一的问题是计算哈希
我已经有一个主哈希表可以交叉检查我的数据库。
请建议使用a来计算哈希值。
我将代码放置如下。
namespace DownloadFromFTP
{
class Program
{
string sqlcon = ConfigurationManager.ConnectionStrings["sqlConn"].ToString();
static void Main(string[] args)
{
try
{
Program program = new Program();
SqlConnection scon = new SqlConnection(program.sqlcon);
string command = ConfigurationManager.AppSettings["GethashTable"].ToString();
SqlDataAdapter sql = new SqlDataAdapter(command, scon);
DataTable hashtable = new DataTable();
sql.Fill(hashtable);
var localPath = @"C:\Users\user1\Desktop\Working Copy\imagefromftp\hash\";
var keyFile = new PrivateKeyFile(@"C:\Users\user1\Desktop\Working Copy\imagefromftp\openssh", "SecretKey");
var keyFiles = new[] { keyFile };
var username = "username";
var methods = new List<AuthenticationMethod>();
methods.Add(new PasswordAuthenticationMethod(username, "password"));
methods.Add(new PrivateKeyAuthenticationMethod(username, keyFiles));
var con = new ConnectionInfo("xx.xxx.xx.xxx", 21, username, methods.ToArray());
using (var client = new SftpClient(con))
{
client.Connect();
var files = client.ListDirectory("/my/mypath1/mypath2/docs/master/files");
foreach (var file in files)
{
if (file.Name != "." && file.Name != ".." & !file.IsSymbolicLink)
{
string remoteHash = program.Gethash(file.FullName).ToString();
bool result = hashtable.AsEnumerable().Where(c => c.Field<string>("sha_hash").Equals(remoteHash)).Count() < 0;
if (result == true )
{
string tblFileName = string.Empty;
string tblRemotePath = string.Empty;
string tblhaschcode = string.Empty;
tblhaschcode = file.GetHashCode().ToString();
tblFileName = file.Name.ToString();
tblRemotePath = file.FullName.ToString();
using (var fs = new FileStream(localPath + file.Name, FileMode.Create, FileAccess.Write))
{
if (!file.IsDirectory && !file.IsSymbolicLink)
{
client.DownloadFile(file.FullName, fs);
program.InsertIntoTable(tblFileName, tblhaschcode, tblRemotePath);
}
else if (file.Name != "." && file.Name != "..")
{
var dir = Directory.CreateDirectory(Path.Combine(localPath, file.Name));
}
}
}
}
}
}
}
catch (Exception)
{
throw;
}
}
protected virtual byte[] Gethash(byte[] hashData)
{
using (var sha1 = new Renci.SshNet.Security.Cryptography.SHA1Hash())
{
return sha1.ComputeHash(hashData, 0, hashData.Length);
}
}
public void InsertIntoTable(string _imageName, string _hashcode, string _remotePath)
{
string sqlcon = ConfigurationManager.ConnectionStrings["sqlConn"].ToString();
string insertCommand = ConfigurationManager.AppSettings["InsertintoImagetable"].ToString();
using (SqlConnection conn = new SqlConnection(sqlcon))
using (SqlCommand sql = new SqlCommand(insertCommand, conn))
{
sql.Parameters.AddWithValue("@_imagename", _imageName);
sql.Parameters.AddWithValue("@_hashcode", _hashcode);
sql.Parameters.AddWithValue("@_remotepath", _remotePath);
foreach( SqlParameter parameter in sql.Parameters)
{
if(parameter.Value == null)
{
parameter.Value = DBNull.Value;
}
conn.Open();
sql.ExecuteNonQuery();
conn.Close();
break;
}
}
}
}
}
我试图将文件传递给一种方法来计算我在处理sftp文件时遇到的困难。我找不到将sftp文件转换为字节或普通文件的方法。
public static string Gethash(SftpFile file)
{
using (FileStream stream = File.OpenRead(actual))
{
var sha = new SHA256Managed();
byte[] checksum = sha.ComputeHash(stream);
return BitConverter.ToString(checksum).Replace("-", string.Empty);
}
}
我的目标是从远程sftp服务器获取要下载的文件的哈希码,并与哈希表进行比较,仅当哈希表中没有哈希表时才下载。