-
Notifications
You must be signed in to change notification settings - Fork 80
Open
Description
We should compare TreeSHA with local file SHA,Local file SHA calculation method
public static string sha(string path)
{
//1、流式读取
var b = new FileStream(path, System.IO.FileMode.Open, FileAccess.Read);
var len = b.Length;
Debug.WriteLine("本地文件长度:" + len);
string str = string.Format("blob {0}\0", len);
byte[] buffer = Encoding.Default.GetBytes(str);
MemoryStream ms = new MemoryStream();
ms.Write(buffer);
b.CopyTo(ms);
b.Close();
SHA1 sha2 = SHA1.Create();
var sha_bytes = sha2.ComputeHash(ms.ToArray(), 0, (int)ms.Length);
ms.Close();
////2、字符串式读取,有效率问题,不适用非txt文件
//var file_text = File.ReadAllText(path).Replace("\r\n", "\n");
////var file_text = File.ReadAllText(path);
//var computedContent = string.Format("blob {0}\0{1}", file_text.Length, file_text);
//SHA1 sha1 = SHA1.Create();
//var sha_bytes = sha1.ComputeHash(Encoding.Default.GetBytes(computedContent));
string res = BitConverter.ToString(sha_bytes).Replace("-", String.Empty).ToLower();
return res;
}
Metadata
Metadata
Assignees
Labels
No labels