Converting strings to ints
The problem is to convert the random string data obtained from os.urandom() in Python to an integer.
os.urandom() returns specified number of bytes of random data. This data is to be interpreted as an integer, because we need large 512 bit or above random prime numbers to generate keys in RSA.
Python being a high level language, one cannot just typecast a pointer.
Well after googling and popping the question at PLUG meeting, the conlusion was that there is no inbuilt function that just interprets the returned bytes as ints.
So here is the function:
def getint(randomstring):
exp = len(randomstring) - 1
sum = 0
for a in randomstring:
sum = sum + ord(a) * 256 ** exp
exp = exp - 1
return sum
Python gives unlimited precision with longs.
os.urandom() returns specified number of bytes of random data. This data is to be interpreted as an integer, because we need large 512 bit or above random prime numbers to generate keys in RSA.
Python being a high level language, one cannot just typecast a pointer.
Well after googling and popping the question at PLUG meeting, the conlusion was that there is no inbuilt function that just interprets the returned bytes as ints.
So here is the function:
def getint(randomstring):
exp = len(randomstring) - 1
sum = 0
for a in randomstring:
sum = sum + ord(a) * 256 ** exp
exp = exp - 1
return sum
Python gives unlimited precision with longs.
0 Comments:
Post a Comment
<< Home