Solved

signed / unsigned integer problem

Posted on 2003-10-25
1
151 Views
Last Modified: 2008-02-01
I have a function in C++:

    int gg_login_hash(char *password, int seed) {
            unsigned int x, y, z;

            y = seed;

            for (x = 0; *password; password++) {
                    x = (x & 0xffffff00) | *password;
                    y ^= x;
                    y += x;
                    x <<= 8;
                    y ^= x;
                    x <<= 8;
                    y -= x;
                    x <<= 8;
                    y ^= x;

                    z = y & 0x1f;
                    y = (y << z) | (y >> (32 - z));
          }
          return y;
    }
   
... and i have converted it to PHP:

    function ggLoginHash($password, $seed) {
   
        $y = $seed;
        $x = 0;
        for ($nr = 0, $length = strlen($password); $nr < $length; $nr++) {
            $x = ($x & 0xFFFFFF00) | ord($password[$nr]);
            $y ^= $x;
            $y += $x;
            $x <<= 8;
            $y ^= $x;
            $x <<= 8;
            $y -= $x;
            $x <<= 8;
            $y ^= $x;
           
            $z = $y & 0x1F;
            $y = ($y << $z) | ($y >> (32 - $z));
        }
        return $y;
    }

The problem is that it doesn't realy seem to return the same value.
I think the problem is that there are variables in the C++ code marked as unsigned int, which i can not do in PHP.
Can someone help me converting that so it will work correctly?

Example:
Calling code from C++:
gg_login_hash("dupa", 128) gives 1453413288 in a result.
0
Comment
Question by:Lukasz Lach
1 Comment
 
LVL 14

Accepted Solution

by:
ThG earned 125 total points
Comment Utility

Hmm, /me thinks.
From a quick debug looks like the $y >> (32 - $z) is performing an aritmetic shift right rather than a logical shift right, as it does (and should do) in C.
This made me think a little so i decided to check the PHP manual for the bitwise operators and in fact a comment points out that all the shift operations are signed (thus aritmetical). Very bad!

So here is a hackish workaround to clean the sign and complete the shifting:
 ...
            $z = $y & 0x1F;
            $y1 = ($y << $z);
            $y2 = 0;
            if ($z < 32) {
              $y2 = $y >> 1;
              $y2 &= 0x7fffffff;
              $y2 = $y2 >> (31 - $z);
            }
            $y = $y1 | $y2;
 ...

I'm the best PHP coder ;-)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Build JSON from table records 17 38
populate dynamic drop down from a csv 3 34
Time difference 10 33
.htaccess file settings 4 31
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now