Function from C/PHP to Python

2008-08-06 Thread Grom
Hello everyone :)
I have one problem with that function in C

int calc_passcode(const 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 &= 0x7fff;
magic2 &= 0x7fff;
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 & 0x7fff;
$magic2 = $magic2 & 0x7fff;
}
return sprintf('%08x%08x', $magic1, $magic2);

} // end _calc_passcode();

Please... its very important to me
--
http://mail.python.org/mailman/listinfo/python-list


Re: Function from C/PHP to Python

2008-08-07 Thread Grom
On 7 Sie, 06:01, Steven D'Aprano <[EMAIL PROTECTED]
cybersource.com.au> wrote:
> 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(password):
>     magic1 = 0x50305735
>     magic2 = 0x12345671
>     sum = 7
>     for i in range(len(password)):
>         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 & 0x7fff
>         magic2 = magic2 & 0x7fff
>     return '%08x%08x' % (magic1, magic2)
>
> --
> Steven

It works, thanks :)
--
http://mail.python.org/mailman/listinfo/python-list


Problem with Twisted

2008-09-01 Thread Grom
Hello. I have something like that:

from twisted.words.protocols.jabber import xmlstream
from twisted.internet import protocol
from twisted.words.xish import domish, utility
from twisted.internet import reactor
from twisted.python import log
import sys, hashlib
log.startLogging(sys.stdout)


def magicHash(password, sid):
magic1 = 0x50305735
magic2 = 0x12345671
sum = 7
for s in range(len(password)):
z = ord(password[s]);
if (z == ' '):
continue
if (z == '\t'):
continue
magic1 = magic1 ^ magic1 & 0x3f) + sum) * z) + (magic1
<< 8))
magic2 = magic2 + ((magic2 << 8) ^ magic1)
sum += z
magic1 = magic1 & 0x7fff
magic2 = magic2 & 0x7fff

return ('%08x%08x'%(magic1, magic2))

def ping(xmlstream):
print 'ping'
xmlstream.send('  \t  ')
reactor.callLater(40, ping, xmlstream)

class TlenAuthInitializer(object):
def __init__(self, xs):
print "Wykonywanie TlenAuthInitializer"
self.xmlstream = xs
def initialize(self):
print "Wykonywanie TlenAuthInitializer - initialize"
iq = xmlstream.IQ(self.xmlstream, "set")
print '1'
iq['id'] = self.xmlstream.sid
print '2'
q = iq.addElement('query', 'jabber:iq:auth')
print '3'
q.addElement('username', content = 
self.xmlstream.authenticator.jid)
print '4'
q.addElement('digest', content =
hashlib.sha1(magicHash(self.xmlstream.authenticator.password,
self.xmlstream.sid)).hexdigest())
print '4'
q.addElement('resource', content  = 't')
print '6'
q.addElement('host', content = 'tlen.pl')
print q.toXml(prefixes=self.prefixes, closeElement=0)
print '7'
d = iq.send('q')
print '8'
d.addCallback(self._authreply)
print '9'
d.addErrBack(self._authfail)
print '10'

def _authreply(self, el):
print "Wykonywanie TlenAuthInitializer - _authreply"
print el.toXml()
reactor.callLater(40, ping, self.xmlstream)

def _authfail(self, el):
print "Wykonywanie TlenAuthInitializer - _authfail"
print el.toXml()

class TlenAuthenticator(xmlstream.ConnectAuthenticator):
def __init__(self, jid, password, host):
print "Wykonywanie TlenAuthenticator"
xmlstream.ConnectAuthenticator.__init__(self, host)
self.jid = jid
self.password = password

def associateWithStream(self, xs):
print "Wykonywanie TlenAuthenticator - associateWithStream"
xs.version = (0, 0)
xmlstream.ConnectAuthenticator.associateWithStream(self, xs)

inits = [(TlenAuthInitializer, True)]
for initClass, required in inits:
init = initClass(xs)
init.required = required
xs.initializers.append(init)

class TlenStream(xmlstream.XmlStream):
def sendHeader(self):
print "Wykonywanie TlenStream - sendHeader"
rootElem = domish.Element((None, 's'))
rootElem['v'] = '9'
rootElem['t'] = '06000224'
self.rootElem = rootElem
self.send(rootElem.toXml(prefixes=self.prefixes, 
closeElement=0))
self._headerSent = True
print 'XMLed rootElem from sendHeader
'+rootElem.toXml(prefixes=self.prefixes, closeElement=0)

def sendFooter(self):
print "Wykonywanie TlenStream - sendFooter"
self.send('')

def onDocumentStart(self, rootelem):
print "Wykonywanie TlenStream - onDocumentStart"
xmlstream.XmlStream.onDocumentStart(self, rootelem)
print 'rootelem from onDocumentStart '+rootelem.toXml()
if rootelem.hasAttribute("i"):
self.sid = rootelem["i"]
self.authenticator.streamStarted(rootelem)

class TlenStreamFactory(xmlstream.XmlStreamFactory):
def __init__(self, authenticator):
print "Wykonywanie TlenStreamFactory"
xmlstream.XmlStreamFactory.__init__(self, authenticator)
self.authenticator = authenticator


def buildProtocol(self, _):
print "Wykonywanie TlenStreamFactory - buildProtocol"
self.resetDelay()
# Create the stream and register all the bootstrap observers
xs = TlenStream(self.authenticator)
xs.factory = self
for event, fn in self.bootstraps: xs.addObserver(event, fn)
return xs

def lg(el):