简体   繁体   English

如何在C#中计算OneDrive XOrHash

[英]How to compute OneDrive XOrHash in C#

We wish to compute the XOrHash the same way that OneDrive does so we can detect any changes needed to sync with the OD4B backend. 我们希望以与OneDrive相同的方式计算XOrHash,以便我们可以检测与OD4B后端同步所需的任何更改。

I have the current implementation for the XOrHash Algorithm used which can be found here https://docs.microsoft.com/en-us/onedrive/developer/code-snippets/quickxorhash 我已经使用了XOrHash算法的当前实现,可以在这里找到https://docs.microsoft.com/en-us/onedrive/developer/code-snippets/quickxorhash

However there is nothing to suggest how I can compute the same hash in our code to match the hash that OD4B provides us with. 但是,没有什么可以建议我如何在代码中计算相同的哈希值以匹配OD4B提供给我们的哈希值。

We Use the XOrHash Algorithm provided in the link above to give us the byte array for the hash which has a typical length of 20 bytes. 我们使用上面链接中提供的XOrHash算法为我们提供哈希的字节数组,其典型长度为20个字节。

public static byte[] ComputeHash(string filePath)
{
    using (var quickXor = XOrHash.Create())
    {
        using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            return quickXor.ComputeHash(stream);
        }
    }
}

 public static string ConvertHexToString(this byte[] bytes)
 {            
     return Convert.ToBase64String(bytes);
 }

returns "4FPisLqvTiuaxUVVz6Zk+RxMClE=" OD4B tells us the hash is "LmpqMT5KKX4ATcd372ZTyVr3gIk="

Clearly we are mismatching and are unable to find any documentation on this other than the following page which suggests to use that the hash is a base64 string https://docs.microsoft.com/en-us/onedrive/developer/rest-api/resources/hashes 显然,我们不匹配,除了下面的页面(找不到建议使用哈希值是base64字符串)之外,我们无法在此找到任何文档https://docs.microsoft.com/zh-cn/onedrive/developer/rest-api /资源/散列

Any example code or hints as to how we should be computing the hash and then returning the string would be much appreciated. 任何有关我们应该如何计算哈希然后返回字符串的示例代码或提示,将不胜感激。

Link to example file: https://dnqa-my.sharepoint.com/:p:/g/personal/autoslave10_dnqauk_co_uk/EcPZl9l2eXNImfD0paFXKyoBHdZwt5mCMRemLKU9wNYIYg?e=63c7632212d948238dd9696c90a11963 链接到示例文件: https : //dnqa-my.sharepoint.com/ : p :/ g/personal/autoslave10_dnqauk_co_uk/EcPZl9l2eXNImfD0paFXKyoBHdZwt5mCMRemLKU9wNYIYg?e=63c7632212d948238dd9696c90a11963

OD4B json OD4B json

{"@odata.context":"omitted","@odata.type":"#oneDrive.item","@odata.id":"omitted","@odata.etag":"\"{D997D9C3-7976-4873-99F0-F4A5A1572B2A},2\"","@odata.editLink":"omitted","createdDateTime":"2017-12-04T17:07:40Z","id":"omitted","lastModifiedDateTime":"2017-12-04T17:07:40Z","name":"pptx001.pptx","file":{"hashes":{"quickXorHash":"LmpqMT5KKX4ATcd372ZTyVr3gIk="},"mimeType":"application/vnd.openxmlformats-officedocument.presentationml.presentation"},"size":29765}

By doing this: 通过做这个:

XOrHash.Create()

you are not actually using your quick xor algorithm. 您实际上并没有使用快速异或算法。 Create() is static method of HashAlgorithm class, which creates some default hash algorithm instance (SHA1 as I remember). Create()HashAlgorithm类的静态方法,它创建一些默认的哈希算法实例(我记得SHA1)。 So what you are actually calling is: 因此,您实际上要调用的是:

HashAlgorithm.Create()

Instead, do it like this: 而是这样做:

using (var quickXor = new XOrHash())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM