我知道之前我已经问了类似这样的问题,但是:这个伪代码here和我的代码一样吗?大写变量是带有“'”的伪代码中的变量,带有条件的值都在列表中,例如:所有“s”条件在列表“s”中,“s”条件在列表“S”中< / p>
for i in xrange(t):
a = h0; b = h1; c = h2; d = h3; e = h4
A = h0; B = h1; C = h2; D = h3; E = h4
X = data[512*i:512*(i+1)] # the data is a binary string
X = [int(X[32*x:32*(x+1)],2) for x in xrange(16)]
for j in xrange(80):
a, e, d, c, b = e, d, ROL(c,10), b, ROL((a + F(b, c, d, j) + X[r[j]] + k[j/16])%(1<<32), s[j]) + e
A, E, D, C, B = E, D, ROL(C,10), B, ROL((A + F(B, C, D, 79-j) + X[R[j]] + K[j/16])%(1<<32), S[j]) + E
T = (h1+c+D)%(1<<32)
h1 = (h2+d+E)%(1<<32)
h2 = (h3+e+A)%(1<<32)
h3 = (h4+a+B)%(1<<32)
h4 = (h0+b+C)%(1<<32)
h0 = T
我一直在研究这段代码(偶尔)已经有一段时间了,我出于某些原因无法让这段代码正常工作。为什么???我确信数据的预处理是正确的,然而,即使我复制其他人的代码并将其转换为python,输出也不是接近正确的
这部分代码应该是正确的:
def F(x,y,z,round):
if round<16:
return x ^ y ^ z
elif 16<=round<32:
return (x & y) | (~x & z)
elif 32<=round<48:
return (x | ~y) ^ z
elif 48<=round<64:
return (x & z) | (y & ~z)
elif 64<=round:
return x ^ (y | ~z)
h0 = 0x67452301; h1 = 0xEFCDAB89; h2 = 0x98BADCFE; h3 = 0x10325476; h4 = 0xC3D2E1F0
k = [0, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E]
K = [0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0]
s = [ 11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8,
7,6,8,13,11,9,7,15,7,12,15,9,11,7,13,12,
11,13,6,7,14,9,13,15,14,8,13,6,5,12,7,5,
11,12,14,15,14,15,9,8,9,14,5,6,8,6,5,12,
9,15,5,11,6,8,13,12,5,12,13,14,11,8,5,6]
S = [ 8,9,9,11,13,15,15,5,7,7,8,11,14,14,12,6,
9,13,15,7,12,8,9,11,7,7,12,7,6,15,13,11,
9,7,15,11,8,6,6,14,12,13,5,14,13,13,7,5,
15,5,8,11,14,14,6,14,6,9,12,9,12,5,15,8,
8,5,12,9,12,5,14,6,8,13,6,5,15,13,11,11]
r = range(16) + [
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13]
R = [ 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11]
答案 0 :(得分:3)
你指向的伪代码定义了一个函数f,常量K和K',选择器r和r'等等 - 所有这些东西隐藏在你展示的代码中?你似乎在使用它们,但是,我们(和你)如何知道它们是对的,没有任何检查?
毕竟,您的错误可以包含在您隐藏的所有代码中。
答案 1 :(得分:0)
我的建议是将代码放入函数中并对其进行单元测试。您有一个预期的输出,单元测试是一种非常有用的方法来验证代码是否符合预期。例如,您的列表理解是否会产生正确的列表?
我还建议你阅读Style Guide for Python,因为你的代码阅读比它需要的更复杂。例如,您在一行上有多个语句。