Prev: ANN: Leo 4.7 final released
Next: AKKA vs Python
From: mk on 23 Feb 2010 09:36 Hello, I need to generate passwords and I think that pseudo-random generator is not good enough, frankly. So I wrote this function: import struct def gen_rand_string(): fileobj = open('/dev/urandom','rb') rstr = fileobj.read(4) rnum = struct.unpack('L',rstr)[0] rstr = '%i' % rnum rnuml = [] while len(rstr) >= 2: c = rstr[:2] try: num = int(c) rnuml.append(num) except ValueError: pass rstr = rstr[2:] rnuml = map(lambda x: 97+x/4, rnuml) rnumc = map(chr, rnuml) return ''.join(rnumc) if __name__ == "__main__": print gen_rand_string() (yes I know that this way generated string will not contain 'z' because 99/4 + 97 = 121 which is 'y') The question is: is this secure? That is, can the string generated this way be considered truly random? (I abstract from not-quite-perfect nature of /dev/urandom at the moment; I can always switch to /dev/random which is better) Regards, mk
From: Paul Rubin on 23 Feb 2010 14:19 mk <mrkafk(a)gmail.com> writes: > I need to generate passwords and I think that pseudo-random generator > is not good enough, frankly. So I wrote this function:... > The question is: is this secure? That is, can the string generated > this way be considered truly random? (I abstract from > not-quite-perfect nature of /dev/urandom at the moment; I can always > switch to /dev/random which is better) urandom is fine and the entropy loss from the numeric conversions and eliminating 'z' in that code before you get letters out is not too bad. The code is pretty ugly. The main problem is you end up with a password that's usually 5 letters but sometimes just 4 or fewer. Passwords that short are vulnerable to dictionary attacks. Longer passwords made from random letters are difficult to remember. I find it's most practical to use a few random words (chosen from a word list like /usr/dict/words) rather than random letters. Words are easier to remember and type. You might look at the site www.diceware.com for an approach to this, which you can implement with a program. The docs there are pretty thoughtful and may help you understand the relevant issues.
From: mk on 23 Feb 2010 14:59 On Feb 23, 7:19 pm, Paul Rubin <no.em...(a)nospam.invalid> wrote: > The code is pretty ugly. The main problem is you end up with a password > that's usually 5 letters but sometimes just 4 or fewer. Well I didn't write the whole thing here, in actual use I'd write a loop repeating the function until I have enough characters and then I'd select a substring of specified length. Anything else in the code that is ugly and I should correct? > You might look at the sitewww.diceware.comfor an approach to this, > which you can implement with a program. The docs there are pretty > thoughtful and may help you understand the relevant issues. Thanks. But I would also be grateful for indicating what is wrong/ugly in my code. Regards, mk
From: Robert Kern on 23 Feb 2010 15:04 On 2010-02-23 13:19 PM, Paul Rubin wrote: > I find it's most practical to use a few random words (chosen from a word > list like /usr/dict/words) rather than random letters. Words are easier > to remember and type. > > You might look at the site www.diceware.com for an approach to this, > which you can implement with a program. The docs there are pretty > thoughtful and may help you understand the relevant issues. I like RFC 1751 for this: http://gitweb.pycrypto.org/?p=crypto/pycrypto-2.x.git;a=blob;f=lib/Crypto/Util/RFC1751.py;h=1c98a212c22066adabfee521b495eeb4f9d7232b;hb=HEAD Shortened URL: http://tr.im/Pv9B -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
From: Robert Kern on 23 Feb 2010 15:49
On 2010-02-23 13:59 PM, mk wrote: > On Feb 23, 7:19 pm, Paul Rubin<no.em...(a)nospam.invalid> wrote: > >> The code is pretty ugly. The main problem is you end up with a password >> that's usually 5 letters but sometimes just 4 or fewer. > > Well I didn't write the whole thing here, in actual use I'd write a > loop repeating the function until I have enough characters and then > I'd select a substring of specified length. > > Anything else in the code that is ugly and I should correct? I would recommend using random.SystemRandom.choice() on a sequence of acceptable characters. E.g. (untested) import random import string characters = string.letters + string.digits + '~!@#$%^&*()-+=,;./\?><|' # ... or whatever. def gen_rand_string(length): prng = random.SystemRandom() chars = [] for i in range(length): chars.append(prng.choice(characters)) return ''.join(chars) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco |