Skip to content

How to calculate local file sha and compare it with cloud(solved) #876

@qiao9580

Description

@qiao9580

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions