不同的MD5散列结果在Powershell vs Python中。

时间:2022-10-28 18:11:07

When computing MD5 hashes using Python and Powershell I am getting different results. It appears that the Python code returns the 'correct' version.

当使用Python和Powershell计算MD5散列时,我得到了不同的结果。Python代码似乎返回了“正确的”版本。

When not using multi-line variables the results are the same. So if I set xml = 'test' they both give the same result.

当不使用多行变量时,结果是相同的。所以如果我设置xml = 'test'它们会得到相同的结果。

I am thinking maybe it has something to do with formatting or newline character, but maybe there is something else wrong with my Powershell code.

我想它可能与格式化或换行符有关,但是我的Powershell代码可能还有其他问题。

When I use Powershell to compute the hash I use this:

当我使用Powershell来计算散列时,我用这个:

Function Get-StringHash([String] $String,$HashName = "MD5") 
{ 
$StringBuilder = New-Object System.Text.StringBuilder 
[System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))|%{ 
[Void]$StringBuilder.Append($_.ToString("x2")) 
} 
$StringBuilder.ToString() 
}
$xml = @"
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
<OPS_envelope>
    <header>
        <version>0.9</version>
    </header>
    <body>
        <data_block>
            <dt_assoc>
                <item key="protocol">XCP</item>
                <item key="action">get</item>
                <item key="object">nameserver</item>
                <item key="domain">domainname</item>
                <item key="attributes">
                    <dt_assoc>
                        <item key="name">all</item>
                    </dt_assoc>
                </item>
            </dt_assoc>
        </data_block>
    </body>
</OPS_envelope>
"@
$key = '12345'

$obj = $xml + $key
$signature = Get-StringHash $obj "MD5"
$signature

It returns the result: 1680ea9b5d8b09ef6c9bd02641246fc4

返回结果:1680ea9b5d8b09ef6c9bd02641246fc4

When I use Python:

当我使用Python:

    import hashlib

xml = '''
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
<OPS_envelope>
    <header>
        <version>0.9</version>
    </header>
    <body>
        <data_block>
            <dt_assoc>
                <item key="protocol">XCP</item>
                <item key="action">get</item>
                <item key="object">nameserver</item>
                <item key="domain">domainname</item>
                <item key="attributes">
                    <dt_assoc>
                        <item key="name">all</item>
                    </dt_assoc>
                </item>
            </dt_assoc>
        </data_block>
    </body>
</OPS_envelope>
'''
key = '12345'
md5_obj = hashlib.md5()
md5_obj.update(xml + key)
signature = md5_obj.hexdigest()
print("SIGNATURE: " + signature)

It results with: d2faf89015178b2ed50ed4a90cbab9ff

它的结果:d2faf89015178b2ed50ed4a90cbab9ff

1 个解决方案

#1


2  

The two input strings are not actually identical, for two reasons:

这两个输入字符串实际上并不相同,有两个原因:

1) Triple-quoted strings in python start and end on the same line as the quotes - here-strings in PowerShell start on the line below @"/@' and end on the line above "@/'@, so change that:

1) python中的三引号字符串开始和结束在与PowerShell中的引号- here-string相同的行上,从下面的行@“/@”开始,到上面的行@/'@结束,因此更改如下:

$xml = @'

<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
<OPS_envelope>
    <header>
        <version>0.9</version>
    </header>
    <body>
        <data_block>
            <dt_assoc>
                <item key="protocol">XCP</item>
                <item key="action">get</item>
                <item key="object">nameserver</item>
                <item key="domain">domainname</item>
                <item key="attributes">
                    <dt_assoc>
                        <item key="name">all</item>
                    </dt_assoc>
                </item>
            </dt_assoc>
        </data_block>
    </body>
</OPS_envelope>

'@

2) Line breaks in PowerShell here-strings default to [Environment]::NewLine, which in windows would be \r\n, whereas python defaults to \n, so make sure you normalize those:

2) PowerShell here-string中的换行符默认为[Environment]:::NewLine, windows中的换行符是\r\n,而python默认为\n,所以请确保对它们进行规范化:

$obj = $obj.Replace([System.Environment]::NewLine,"`n")

#1


2  

The two input strings are not actually identical, for two reasons:

这两个输入字符串实际上并不相同,有两个原因:

1) Triple-quoted strings in python start and end on the same line as the quotes - here-strings in PowerShell start on the line below @"/@' and end on the line above "@/'@, so change that:

1) python中的三引号字符串开始和结束在与PowerShell中的引号- here-string相同的行上,从下面的行@“/@”开始,到上面的行@/'@结束,因此更改如下:

$xml = @'

<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
<OPS_envelope>
    <header>
        <version>0.9</version>
    </header>
    <body>
        <data_block>
            <dt_assoc>
                <item key="protocol">XCP</item>
                <item key="action">get</item>
                <item key="object">nameserver</item>
                <item key="domain">domainname</item>
                <item key="attributes">
                    <dt_assoc>
                        <item key="name">all</item>
                    </dt_assoc>
                </item>
            </dt_assoc>
        </data_block>
    </body>
</OPS_envelope>

'@

2) Line breaks in PowerShell here-strings default to [Environment]::NewLine, which in windows would be \r\n, whereas python defaults to \n, so make sure you normalize those:

2) PowerShell here-string中的换行符默认为[Environment]:::NewLine, windows中的换行符是\r\n,而python默认为\n,所以请确保对它们进行规范化:

$obj = $obj.Replace([System.Environment]::NewLine,"`n")