使用HASH MD5解决大型XML文件问题 [英] Troubles with HASH MD5 of large XML files

查看:90
本文介绍了使用HASH MD5解决大型XML文件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试计算XML文件的哈希MSD5时遇到了麻烦。我有一个正确计算它的第三方验证器,我试图在c#中创建我自己的验证器,但它不起作用,我一直得到一个无效的哈希MD5与大xml文件,小文件它工作正常。大文件有大约63000行。



C#中的代码:



I am having troubles when trying to calculate the hash MSD5 of an XML file. I have a third party validator that calculates it correctly, i am trying to make my own validator in c# but it does not work, i keep getting an invalid hash MD5 with big xml files, with small files it works fine. The big file has around 63000 lines.

The code in C#:

public string GenerateHash(string xml, string hashOrigin = null)
    {

        XDocument doc = XDocument.Parse(xml);

        StringBuilder sb = new StringBuilder();

        string pKeyEncode = doc.Root.Value;
        if (!string.IsNullOrEmpty(hashOrigin))
            pKeyEncode = pKeyEncode.Replace(hashOrigin, "");

        MD5CryptoServiceProvider _cs = new MD5CryptoServiceProvider();
        byte[] _bs = Encoding.GetEncoding("ISO-8859-1").GetBytes(pKeyEncode);
        _bs = _cs.ComputeHash(_bs);
        StringBuilder _s = new System.Text.StringBuilder();
        foreach (byte _b in _bs)
        {
            _s.Append(_b.ToString("x2").ToLower());
        }
        int a = _s.GetHashCode();
        return _s.ToString();
    }

推荐答案

你做错了。



如果你真的有XML文件,请按原样读取文件,转换为字节数组,而不是字符串。它将使您的代码独立于编码。

要计算MD5,使用类 System.Security.Cryptography.MD5 。是的,它是抽象的,但您需要通过其工厂方法 创建来获取提供者实例,如最后的代码示例所示:

https: //msdn.microsoft.com/en-us/library/system.security.cryptography.md5%28v=vs.110%29.aspx [ ^ ]。



要创建 MD5 类实例,请参阅方法 Program.Main



-SA
You are doing it wrong.

If you really have XML file, read the file as is, into array of bytes, not string. It will make your code independent on encoding.
To compute MD5 has, uses the class System.Security.Cryptography.MD5. Yes, it is abstract, but you need to get the provider instance via its factory method Create, as shown in the code sample at the end:
https://msdn.microsoft.com/en-us/library/system.security.cryptography.md5%28v=vs.110%29.aspx[^].

For the creation of MD5 class instance, please see the method Program.Main.

—SA


这篇关于使用HASH MD5解决大型XML文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆