Guido van Rossum | 37a2911 | 1995-06-21 00:55:59 +0000 | [diff] [blame] | 1 | class Security: |
| 2 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 3 | def __init__(self): |
| 4 | import os |
| 5 | env = os.environ |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 6 | if 'PYTHON_KEYFILE' in env: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 7 | keyfile = env['PYTHON_KEYFILE'] |
| 8 | else: |
| 9 | keyfile = '.python_keyfile' |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 10 | if 'HOME' in env: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 11 | keyfile = os.path.join(env['HOME'], keyfile) |
| 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 |
| 19 | try: |
| 20 | self._key = eval(open(keyfile).readline()) |
| 21 | except IOError: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +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 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 24 | def _generate_challenge(self): |
| 25 | import random |
| 26 | return random.randint(100, 100000) |
Guido van Rossum | 37a2911 | 1995-06-21 00:55:59 +0000 | [diff] [blame] | 27 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 28 | def _compare_challenge_response(self, challenge, response): |
| 29 | return self._encode_challenge(challenge) == response |
Guido van Rossum | 37a2911 | 1995-06-21 00:55:59 +0000 | [diff] [blame] | 30 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 31 | def _encode_challenge(self, challenge): |
| 32 | p, m = self._key |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 33 | return pow(int(challenge), p, m) |