views:

130

answers:

2

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]
+3  A: 

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.

Alex Martelli
okay. i added the constants and f. Im pretty sure they are correct though
calccrypto
there is a big difference between "pretty sure" and "sure/proven"
fuzzy lollipop
so are they the same?
calccrypto
A: 

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.

rioch