title: Python TEA implementation
body: <p>Anybody knows proper python implementation of TEA (Tiny Encryption Algorithm)? I tried the one I've found here: <a href="http://sysadminco.com/code/python-tea/" rel="nofollow noreferrer">http://sysadminco.com/code/python-tea/</a> - but it does not seem to work properly.</p>
  
  <p>It returns different results than other implementations in C or Java. I guess it's caused by completely different data types in python (or no data types in fact).</p>
  
  <p>Here's the code and an example:</p>
  
  <pre><code>def encipher(v, k):
      y=v[0];z=v[1];sum=0;delta=0x9E3779B9;n=32
      w=[0,0]
      while(n&gt;0):
          y += (z &lt;&lt; 4 ^ z &gt;&gt; 5) + z ^ sum + k[sum &amp; 3]
          y &amp;= 4294967295L # maxsize of 32-bit integer
          sum += delta
          z += (y &lt;&lt; 4 ^ y &gt;&gt; 5) + y ^ sum + k[sum&gt;&gt;11 &amp; 3]
          z &amp;= 4294967295L
          n -= 1
  
      w[0]=y; w[1]=z
      return w
  
  def decipher(v, k):
      y=v[0]
      z=v[1]
      sum=0xC6EF3720
      delta=0x9E3779B9
      n=32
      w=[0,0]
      # sum = delta&lt;&lt;5, in general sum = delta * n
  
      while(n&gt;0):
          z -= (y &lt;&lt; 4 ^ y &gt;&gt; 5) + y ^ sum + k[sum&gt;&gt;11 &amp; 3]
          z &amp;= 4294967295L
          sum -= delta
          y -= (z &lt;&lt; 4 ^ z &gt;&gt; 5) + z ^ sum + k[sum&amp;3]
          y &amp;= 4294967295L
          n -= 1
  
      w[0]=y; w[1]=z
      return w
  </code></pre>
  
  <p>Python example:</p>
  
  <pre><code>&gt;&gt;&gt; import tea
  &gt;&gt;&gt; key = [0xbe168aa1, 0x16c498a3, 0x5e87b018, 0x56de7805]
  &gt;&gt;&gt; v = [0xe15034c8, 0x260fd6d5]
  &gt;&gt;&gt; res = tea.encipher(v, key)
  &gt;&gt;&gt; "%X %X" % (res[0], res[1])
  **'70D16811 F935148F'**
  </code></pre>
  
  <p>C example:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  
  void encipher(unsigned long *const v,unsigned long *const w,
     const unsigned long *const k)
  {
     register unsigned long       y=v[0],z=v[1],sum=0,delta=0x9E3779B9,
                                  a=k[0],b=k[1],c=k[2],d=k[3],n=32;
  
     while(n--&gt;0)
        {
        sum += delta;
        y += (z &lt;&lt; 4)+a ^ z+sum ^ (z &gt;&gt; 5)+b;
        z += (y &lt;&lt; 4)+c ^ y+sum ^ (y &gt;&gt; 5)+d;
        }
  
     w[0]=y; w[1]=z;
  }
  
  int main()
  {
          unsigned long v[] = {0xe15034c8, 0x260fd6d5};
          unsigned long key[] = {0xbe168aa1, 0x16c498a3, 0x5e87b018, 0x56de7805};
  
          unsigned long res[2];
  
          encipher(v, res, key);
  
          printf("%X %X\n", res[0], res[1]);
  
          return 0;
  }
  
  $ ./tea
  **D6942D68 6F87870D**
  </code></pre>
  
  <p>Please note, that both examples were run with the same input data (v and key), but results were different. I'm pretty sure C implementation is correct - it comes from a site referenced by wikipedia (I couldn't post a link to it because I don't have enough reputation points yet - some antispam thing)</p>