SHA-based subclass for random module

Trevor Perrin trevp_spam at trevp.net
Wed Mar 24 14:35:28 EST 2004


Raymond Hettinger wrote:
> [Trevor Perrin] 
> 
>>I'm proposing that getrandstring() (returning a string) is a better 
>>primitive than getrandbits() (returning a long). 
> [...]
> 
> Sorry, I do not think that introducing a string method into an
> otherwise numeric module would make it easier to use.

Okay.


> What might be nice is if longs had methods for converting to and from
> bytestrings.  

I was thinking the same.  How about something like:

   bigNumber = long(bigNumberString, 256)

   bigNumberString = bigNumber.tostring()

The string->long conversion treats a big-endian byte string as "base 
256", and converts it to a non-negative integer.

The long->string conversion would only work on non-negative integers. 
It's 'inspired' by the array module's "tostring()" method, which returns 
a similarly low-level string.

Big-endian seems the common way to do this, at least in cryptography, in 
the common ASN.1 encodings, and in the libraries I've seen.


Trevor







More information about the Python-list mailing list