这两个代码彼此等效吗?
我知道我以前问过类似的问题,但是:这是伪代码这里< /a> 和我的代码一样吗?大写变量是伪代码中带“'”的变量,带条件的值都在列表中,如:所有“s”条件都在列表“s”中,“s'”条件在列表“S”
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
i 中我已经(偶尔)研究这段代码很长一段时间了,但由于某种原因,我无法让这段代码正常工作。为什么???我确信数据的预处理是正确的,然而,即使当我复制其他人的代码并将它们翻译成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]
i know ive asked similar questions like this before, but: Is this pseudocode here the same as my code? upper case variables are the variables in the pseudocode with " ' " and the values with conditions are all in lists, such as: all "s" conditions are in list "s", and " s' " conditions in list "S"
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
i have been working on this code (sporadically) for quite some time now and i for some reason have not been able to get this code to work properly. why??? im sure that the preprocessing of the data is correct, and yet, even when i copy other people's codes and translate them into python, the outputs are no where near correct at all
this part of the code should be correct:
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您指向的伪代码定义了函数 f、常量 K 和 K'、选择器 r 和 r' 等 - 所有这些东西都隐藏在您所显示的代码中的哪里?您似乎正在使用它们,但是,我们(和您)如何在没有任何检查的情况下知道它们是正确的?
毕竟,您的错误可能存在于您对我们隐藏的所有代码中。
The pseudocode you point to defines a function f, constants K and K', selectors r and r', etc -- where are all of these things hiding in the code you're showing? You seem to be using them, but, how do we (and you) know they're right, without any inspection?
Your bug, after all, could be in all this code you're hiding from us.
我的建议是将代码放入函数中并对其进行单元测试。您有预期的输出,并且单元测试是验证代码是否执行其预期功能的非常有用的方法。例如,您的列表理解是否会生成正确的列表?
我还建议您阅读 Python 风格指南,因为您的代码是阅读起来比需要的更复杂。例如,一行中有多个语句。
My suggestion would be to put the code into functions and unit test it. You have an expected output, and unit tests are a very useful way to verify that the code does what it's supposed to. For example, does your list comprehension result in a correct list?
I also suggest you read the Style Guide for Python, as your code is more complicated to read than it needs to be. For example, you have multiple statements on a single line.