Re: Cryptographically random numbers

2006-03-07 Thread Paul Rubin
Tim Hochberg <[EMAIL PROTECTED]> writes: > > is fast, but if a is aliased, Python can't do the optimization, so > Is this really true? After the first time through the loop, 'a' won't > be aliased any more since strings are immutable. Hmm, ok, there was some previous discussion of this problem a

Re: Cryptographically random numbers

2006-03-07 Thread Tim Hochberg
Paul Rubin wrote: > "Tuvas" <[EMAIL PROTECTED]> writes: > >>I've actually done the tests on this one, it's actually faster to use >>the += than a list, odd as it may sound. > > > Frederik explained the reason; there's an optimization in Python 2.4 > that I'd forgotten about, for that specific c

Re: Cryptographically random numbers

2006-03-07 Thread Paul Rubin
"Tuvas" <[EMAIL PROTECTED]> writes: > I've actually done the tests on this one, it's actually faster to use > the += than a list, odd as it may sound. Frederik explained the reason; there's an optimization in Python 2.4 that I'd forgotten about, for that specific case. It's not in earlier versio

Re: Cryptographically random numbers

2006-03-07 Thread Steven D'Aprano
On Tue, 07 Mar 2006 22:32:04 +0100, Fredrik Lundh wrote: >> Python lists have a special efficiency hack so that ret.append doesn't >> copy the whole list around, but rather, allocates space in bigger >> chunks so that appending usually takes constant time. > > in 2.4 and later, += on strings does

Re: Cryptographically random numbers

2006-03-07 Thread Tuvas
I've actually done the tests on this one, it's actually faster to use the += than a list, odd as it may sound. I ran into this one a while back. The best way to do it is to build an array from scratch, fill the array, and then join it, but I didn't have time to do it that way... -- http://mail.py

Re: Cryptographically random numbers

2006-03-07 Thread Fredrik Lundh
Paul Rubin wrote: > The usual Python idiom for building up a string in approx linear time > is: > >def cstring(n): > ret = [] > while (something): > ret.append(generate_another_character()) > return ''.join(ret) > > Python lists have a special efficiency hack so that

Re: Cryptographically random numbers

2006-03-07 Thread Paul Rubin
"Tuvas" <[EMAIL PROTECTED]> writes: > from os import urandom > def cstring(bytes): > ret='' > while(len(ret) c=os.urandom(1) > if c>'0' and c<'z': > ret=ret+c > return ret > > That should do it, though I bet there might be a more efficient way. One efficie

Re: Cryptographically random numbers

2006-03-07 Thread Tuvas
I will admit though, I have the same question as Paul, why do you want a random string of numbers, letters, and symbols? But, you asked for it, so, that'll do. -- http://mail.python.org/mailman/listinfo/python-list

Re: Cryptographically random numbers

2006-03-07 Thread Gervasio Bernal
Tuvas wrote: > from os import urandom > def cstring(bytes): > ret='' > while(len(ret) c=os.urandom(1) > if c>'0' and c<'z': > ret=ret+c > return ret > > That should do it, though I bet there might be a more efficient way. I > don't know if that's the set of

Re: Cryptographically random numbers

2006-03-07 Thread Paul Rubin
Gervasio Bernal <[EMAIL PROTECTED]> writes: > How can I generate a random string containing digits, symbols and > letters? I will use this random string for the key of a cryptographic > algorithm. Generally if this is a string that some human user has to type in, it's preferable to select some wor

Re: Cryptographically random numbers

2006-03-07 Thread Tuvas
from os import urandom def cstring(bytes): ret='' while(len(ret)'0' and c<'z': ret=ret+c return ret That should do it, though I bet there might be a more efficient way. I don't know if that's the set of characters you want to use, but... If you want a better answer, you'd h

Re: Cryptographically random numbers

2006-03-07 Thread Gervasio Bernal
Bryan Olson wrote: > Tuvas wrote: > >>Ahh, you are correct, that is a large bug... How about this one? > > > Much better. Do note the comments from Emile van Sebille and Paul > Rubin. There are some minor style and efficiency points, but it > looks reasonable. > > Incidentally, as of Python 2.4

Re: Cryptographically random numbers

2006-03-06 Thread Tuvas
Thanks for the function Paul, it works alot nicer than the one I had in my program... Now, with all of this knowledge, I'm going to be brave and try out everything with AES. It seems to be working alright, I'll debug this more on my own than I did with my RSA code, which turned out to be full of bu

Re: Cryptographically random numbers

2006-03-06 Thread Bryan Olson
Tuvas wrote: [...] > As to the s2num(text), well, that looks really neat. Is there an easy > way to do the reverse of that? Thanks! Easy? Well, you be the judge: def num2string(n): """ Pass a non-negative int or long n. Returns a string with bytes holding the big-endian base

Re: Cryptographically random numbers

2006-03-06 Thread Tuvas
Wow, that would have been nice to know... Oh well, I've already got the function, might as well use it... I'm starting to learn alot more of the standard libraries that exist for alot of the little functions. It seems like every project I have I build a misc.py file that contains several small, but

Re: Cryptographically random numbers

2006-03-06 Thread Bryan Olson
Tuvas wrote: > Ahh, you are correct, that is a large bug... How about this one? Much better. Do note the comments from Emile van Sebille and Paul Rubin. There are some minor style and efficiency points, but it looks reasonable. Incidentally, as of Python 2.4, the standard library offers random.Sy

Re: Cryptographically random numbers

2006-03-06 Thread Paul Rubin
"Tuvas" <[EMAIL PROTECTED]> writes: > Wait, I now see that there is a native base 2 log in python, so I will > just do that rather than my adhoc way. The reason for adding one is to > make sure there isn't any problems if the log is, for instance, 2.2. It > will always round up. It's better to have

Re: Cryptographically random numbers

2006-03-06 Thread Tuvas
Wait, I now see that there is a native base 2 log in python, so I will just do that rather than my adhoc way. The reason for adding one is to make sure there isn't any problems if the log is, for instance, 2.2. It will always round up. It's better to have to try twice to make sure the number can ha

Re: Cryptographically random numbers

2006-03-06 Thread Bryan Olson
Paul Rubin wrote: > My favorite way to convert strings to numbers is with binascii: > > from binascii import hexlify > def s2num(text): >return int(hexlify(text), 16) Neat. I use the empty string as a binary representation of zero, which you can accommodate with: def s2num(text): ret

Re: Cryptographically random numbers

2006-03-06 Thread Paul Rubin
"Tuvas" <[EMAIL PROTECTED]> writes: > def s2num(text): > if(len(text)==1): > return ord(text) > else: > return ord(text[0])+256*s2num(text[1:]) My favorite way to convert strings to numbers is with binascii: from binascii import hexlify def s2num(text): return int(hexl

Re: Cryptographically random numbers

2006-03-06 Thread Tuvas
Ahh, you are correct, that is a large bug... How about this one? def s2num(text): if(len(text)==1): return ord(text) else: return ord(text[0])+256*s2num(text[1:]) def cran_rand(min,max): range=int(log(abs(max-min))/log(2))+1 num=max+1 if range%8==0: cra

Re: Cryptographically random numbers

2006-03-06 Thread Bryan Olson
Tuvas wrote: > Okay, I'm working on devoloping a simple, cryptographically secure > number, from a range of numbers (As one might do for finding large > numbers, to test if they are prime). My function looks like this: > > def cran_rand(min,max): > if(min>max): > x=max > max=mi

Re: Cryptographically random numbers

2006-03-05 Thread Tuvas
Good idea about the max and min values. Yes, urandom is os.urandom. s2num('blah') will convert the phrase blah to ascii, and treat them as if they were a big function. Anyone else whose still interested, I found another small bug, but it was in the modular (Again). It won't do much, but... I did

Re: Cryptographically random numbers

2006-03-05 Thread Emile van Sebille
> def cran_rand(min,max): You're shadowing built-ins here. Not a problem, but something I'd generally avoid. >if(min>max): >x=max >max=min >min=x If the args were a,b you could say: maxarg,minarg = min(a,b),max(a,b) >range=round(log(max-min)/log(256)) more

Cryptographically random numbers

2006-03-04 Thread Tuvas
Okay, I'm working on devoloping a simple, cryptographically secure number, from a range of numbers (As one might do for finding large numbers, to test if they are prime). My function looks like this: def cran_rand(min,max): if(min>max): x=max max=min min=x range=rou