Guido van Rossum | 37a2911 | 1995-06-21 00:55:59 +0000 | [diff] [blame] | 1 | class Security: |
| 2 | |
| 3 | def __init__(self): |
| 4 | import os |
| 5 | env = os.environ |
| 6 | if env.has_key('PYTHON_KEYFILE'): |
| 7 | keyfile = env['PYTHON_KEYFILE'] |
Guido van Rossum | 37a2911 | 1995-06-21 00:55:59 +0000 | [diff] [blame] | 8 | else: |
| 9 | keyfile = '.python_keyfile' |
Guido van Rossum | 541df3e | 1995-06-21 02:08:55 +0000 | [diff] [blame] | 10 | if env.has_key('HOME'): |
| 11 | keyfile = os.path.join(env['HOME'], keyfile) |
Guido van Rossum | 83d6bbf | 1995-10-07 19:35:07 +0000 | [diff] [blame] | 12 | if not os.path.exists(keyfile): |
| 13 | import sys |
| 14 | for dir in sys.path: |
| 15 | kf = os.path.join(dir, keyfile) |
| 16 | if os.path.exists(kf): |
| 17 | keyfile = kf |
| 18 | break |
Guido van Rossum | 37a2911 | 1995-06-21 00:55:59 +0000 | [diff] [blame] | 19 | try: |
| 20 | self._key = eval(open(keyfile).readline()) |
| 21 | except IOError: |
Guido van Rossum | 83d6bbf | 1995-10-07 19:35:07 +0000 | [diff] [blame] | 22 | raise IOError, "python keyfile %s: cannot open" % keyfile |
Guido van Rossum | 37a2911 | 1995-06-21 00:55:59 +0000 | [diff] [blame] | 23 | |
| 24 | def _generate_challenge(self): |
Guido van Rossum | 6c3a2cb | 1998-05-20 17:13:01 +0000 | [diff] [blame] | 25 | import random |
| 26 | return random.randint(100, 100000) |
Guido van Rossum | 37a2911 | 1995-06-21 00:55:59 +0000 | [diff] [blame] | 27 | |
| 28 | def _compare_challenge_response(self, challenge, response): |
| 29 | return self._encode_challenge(challenge) == response |
| 30 | |
| 31 | def _encode_challenge(self, challenge): |
| 32 | p, m = self._key |
Guido van Rossum | 541df3e | 1995-06-21 02:08:55 +0000 | [diff] [blame] | 33 | return pow(long(challenge), p, m) |