encryption with python

Mike Meyer mwm at mired.org
Fri Sep 9 17:08:38 EDT 2005


Steven D'Aprano <steve at REMOVETHIScyber.com.au> writes:
> On Wed, 07 Sep 2005 14:31:03 -0700, jlocc wrote:
>> Basically I will like to combine a social security number (9 digits)
>> and a birth date (8 digits, could be padded to be 9) and obtain a new
>> 'student number'. It would be better if the original numbers can't be
>> traced back, they will be kept in a database anyways. Hope this is a
>> bit more specific, thanks!!!
> last_number_used = 123  # or some other appropriate value
>
> def make_studentID():
>     global last_number_used
>     last_number_used = last_number_used + 1
>     return last_number_used
>
> For a real application, I'd check the database to see if the number has
> already been used before returning the number. Also, if you need more
> than four digits in your IDs, I'd add a checksum to the end so you can
> detect many typos and avoid much embarrassment.
[...]
> In a real application you would need to store the global variables in a
> database, otherwise each time you reload the Python script you start
> generating the same IDs over and over again.

For real applications (ignoring your theoretical need to generate the
numbers in a random order) I'd not only store the number in the
database - I'd let the databae generate it. Most have some form of
counter that does exactly what you want without needing to keep track
of it and check the database for consistency.

   <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list