On Mon, 2007-01-01 at 09:23 -0800, Ryan Roth wrote:
> How do I get urandom to only do valid chars?

You could read in 8 characters, and then coerce them to the range needed
with modulo reduction.  Something like:

        import string
        chars = string.letters + string.digits + '/.'
        salt = [ chars[ord(x) % len(chars)] for x in 
file('/dev/urandom').read(8) ]
        salt = "".join(salt)

>From an anal-retentive cryptographic perspective, this would produce a
statistical bias toward the first character ('a') if the length of chars
isn't a power of 2.  It strikes me as not a coincidence though that, per
spec, the allowed salt chars is 64, exactly a power of 2.

If it weren't, the proper (again, paranoid) approach would be to zero
the unneeded MSB bits in each /dev/urandom char to put the value to the
closest power of 2, and then discard the character if it's not less than
len(chars).

But even if the length of chars wasn't a power of 2, I'd probably tell
you not to worry about it. :)

Cheers,
Jason.


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-users mailing list
Freevo-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/freevo-users

Reply via email to