1class 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']
8        else:
9            keyfile = '.python_keyfile'
10            if env.has_key('HOME'):
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:
22            raise IOError, "python keyfile %s: cannot open" % keyfile
23
24    def _generate_challenge(self):
25        import random
26        return random.randint(100, 100000)
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
33        return pow(long(challenge), p, m)
34