I am trying to compute the sha1 of a file using the below method. The result is different from what is reported by "git ls-tree HEAD". What's wrong with my code?
static public string ComputeSha1()
{
//"blob " + <size_of_file> + "\0" + <contents_of_file>
var content = File.ReadAllBytes(@"c:\projects\myproj\Word.docx");
var contentLength = Encoding.UTF8.GetByteCount(Encoding.UTF8.GetString(content));
var blob = "blob " + contentLength + "\0" + Encoding.UTF8.GetString(content);
using (SHA1 sha1 = SHA1.Create())
{
byte[] bytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(blob));
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
return builder.ToString();
}
}