c# get md5 hash of an embedded resource before extracting it

旧城冷巷雨未停 提交于 2021-02-11 08:21:05

问题


We have an embedded resource and need to get the md5 hash of the file before extracting it in order to know if it is different from an already existing file, (becouse if we have to extract it to compare them it would be better to replace the file directly)

Any suggestion is appreciated


回答1:


What sort of embedded resource is it? If it's one you get hold of using Assembly.GetManifestResourceStream(), then the simplest approach is:

using (Stream stream = Assembly.GetManifestResourceStream(...))
{
    using (MD5 md5 = MD5.Create())
    {
        byte[] hash = md5.ComputeHash(stream);
    }
}

If that doesn't help, please give more information as to how you normall access/extract your resource.




回答2:


You can use MemoryStream

using (MemoryStream ms = new MemoryStream(Properties.Resources.MyZipFile))
{
  using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
  {
     byte[] hash = md5.ComputeHash(ms);
     string str = Convert.ToBase64String(hash);
     // result for example: WgWKWcyl2YwlF/C8yLU9XQ==
  }
}


来源:https://stackoverflow.com/questions/5591778/c-sharp-get-md5-hash-of-an-embedded-resource-before-extracting-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!