Function from C/PHP to Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grom

    Function from C/PHP to Python

    Hello everyone :)
    I have one problem with that function in C

    int calc_passcode(c onst char* pass, char* code) {
    int magic1 = 0x50305735;
    int magic2 = 0x12345671;
    int sum = 7;
    char z;
    while ((z = *pass++) != 0) {
    if (z == ' ') continue;
    if (z == '\t') continue;
    magic1 ^= (((magic1 & 0x3f) + sum) * z) + (magic1 << 8);
    magic2 += (magic2 << 8) ^ magic1;
    sum += z;
    }
    magic1 &= 0x7fffffff;
    magic2 &= 0x7fffffff;
    return sprintf(code, "%08x%08x", magic1, magic2);
    } // end _calc_passcode( );

    Can someone help me to rewrite it to python?

    There is the same function, in PHP:

    function _calc_passcode( $pass) {

    $magic1 = 0x50305735;
    $magic2 = 0x12345671;
    $sum = 7;
    for ($i = 0; $i < strlen($pass); $i++) {
    $z = ord($pass[$i]);
    if ($z == 32)
    continue;
    if ($z == 9)
    continue;
    $magic1 = $magic1 ^ (((($magic1 & 0x3f) + $sum) * $z) + ($magic1 <<
    8));
    $magic2 = $magic2 + (($magic2 << 8) ^ $magic1);
    $sum += $z;
    $magic1 = $magic1 & 0x7fffffff;
    $magic2 = $magic2 & 0x7fffffff;
    }
    return sprintf('%08x%0 8x', $magic1, $magic2);

    } // end _calc_passcode( );

    Please... its very important to me
  • Steven D'Aprano

    #2
    Re: Function from C/PHP to Python

    On Wed, 06 Aug 2008 20:15:11 -0700, Grom wrote:
    Hello everyone :)
    I have one problem with that function in C
    ....
    Can someone help me to rewrite it to python?
    >
    There is the same function, in PHP:
    ....
    Please... its very important to me
    I don't know C or PHP. But since it's important, and because you said
    please, let me try:


    def calc_passcode(p assword):
    magic1 = 0x50305735
    magic2 = 0x12345671
    sum = 7
    for i in range(len(passw ord)):
    z = ord(password[i])
    if z == 32 or z == 9:
    continue
    magic1 = magic1 ^ ((((magic1 & 0x3f) + sum) * z) + (magic1 << 8))
    magic2 = magic2 + ((magic2 << 8) ^ magic1)
    sum += z
    magic1 = magic1 & 0x7fffffff
    magic2 = magic2 & 0x7fffffff
    return '%08x%08x' % (magic1, magic2)



    --
    Steven

    Comment

    • Grom

      #3
      Re: Function from C/PHP to Python

      On 7 Sie, 06:01, Steven D'Aprano <st...@REMOVE-THIS-
      cybersource.com .auwrote:
      On Wed, 06 Aug 2008 20:15:11 -0700, Grom wrote:
      Hello everyone :)
      I have one problem with that function in C
      ...
      Can someone help me to rewrite it to python?
      >
      There is the same function, in PHP:
      ...
      Please... its very important to me
      >
      I don't know C or PHP. But since it's important, and because you said
      please, let me try:
      >
      def calc_passcode(p assword):
          magic1 = 0x50305735
          magic2 = 0x12345671
          sum = 7
          for i in range(len(passw ord)):
              z = ord(password[i])
              if z == 32 or z == 9:
                  continue
              magic1 = magic1 ^ ((((magic1 & 0x3f) + sum) * z) + (magic1 << 8))
              magic2 = magic2 + ((magic2 << 8) ^ magic1)
              sum += z
              magic1 = magic1 & 0x7fffffff
              magic2 = magic2 & 0x7fffffff
          return '%08x%08x' % (magic1, magic2)
      >
      --
      Steven
      It works, thanks :)

      Comment

      Working...