tags:

views:

80

answers:

3

How could I ensure that a randomizing algorithm would produce the same random number for two different programs. I am trying to make a chat program that utilizes a shared password, or key and then uses that key to generate a random string that is only predictable to both programs.

For example

Person A: asd4sa5d8s5s5d5sd Person B: asd4sa5d8s5s5d5sd

Person A: 2SDASD5545S Person B: 2SDASD5545S

+2  A: 

Use random.seed, giving the same value to the function in both clients.

Brian S
A: 

You should make a private instance of Random(). If you just use the random module the usual way, you will get unpredictable results if random numbers are consumed anywhere else in your program

from md5 import md5
from random import Random

hsh=md5("passphrase").hexdigest()
seed = long(hsh,16)
myrandom = Random()
myrandom.seed(seed)
print myrandom.randint(0,100)
gnibbler
+1  A: 

If you want a predictable secure string based on a password why not just use a hash? You can encode the hash into a string using Base64 encoding.

If you add a salt to the string before hashing it will make it much harder for people to use a brute force or rainbow table attack to go from the hash back to the password.

>>> password = "asd4sa5d8s5s5d5sd"
>>> import hashlib, base64
>>> salt = "mysecretstring"
>>> m = hashlib.md5()
>>> m.update(salt)
>>> m.update(password)
>>> base64.b64encode(m.digest())
'NR6jEKdkVcfcT4pgBnF96A=='
Dave Webb