如何在C#中计算OneDrive XOrHash [英] How to compute OneDrive XOrHash in C#
问题描述
我们希望以与OneDrive相同的方式计算XOrHash,以便我们可以检测与OD4B后端同步所需的任何更改.
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.
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
但是,没有什么可以建议我如何在我们的代码中计算相同的散列来匹配OD4B提供给我们的散列.
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.
我们使用上面链接中提供的XOrHash算法为我们提供哈希的字节数组,其典型长度为20个字节.
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.
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="
很明显,我们不匹配,因此无法找到关于此文档的任何文档,但以下页面除外,该页面建议使用哈希值是base64字符串 https://docs.microsoft.com/zh-cn/onedrive/developer/rest-api/resources/hashhes
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
任何有关如何计算哈希然后返回字符串的示例代码或提示,将不胜感激.
Any example code or hints as to how we should be computing the hash and then returning the string would be much appreciated.
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}
推荐答案
这样做:
XOrHash.Create()
您实际上并没有使用快速异或算法. Create()
是 HashAlgorithm
类的静态方法,该方法创建一些默认的哈希算法实例(我记得SHA1).因此,您实际上要呼叫的是:
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). So what you are actually calling is:
HashAlgorithm.Create()
相反,请这样做:
using (var quickXor = new XOrHash())
这篇关于如何在C#中计算OneDrive XOrHash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!