# Path: pyTOP/packages/requests/utils.py
# def randombytes(n):
#     """Return n random bytes."""
#     # Use /dev/urandom if it is available.  Fall back to random module
#     # if not.  It might be worthwhile to extend this function to use
#     # other platform-specific mechanisms for getting random bytes.
#     if os.path.exists("/dev/urandom"):
#         f = open("/dev/urandom")
#         s = f.read(n)
#         f.close()
#         return s
#     else:
#         L = [chr(random.randrange(0, 256)) for i in range(n)]
#         return "".join(L)
# 
# def parse_dict_header(value):
#     """Parse lists of key, value pairs as described by RFC 2068 Section 2 and
#     convert them into a python dict:
# 
#     >>> d = parse_dict_header('foo="is a fish", bar="as well"')
#     >>> type(d) is dict
#     True
#     >>> sorted(d.items())
#     [('bar', 'as well'), ('foo', 'is a fish')]
# 
#     If there is no value for a key it will be `None`:
# 
#     >>> parse_dict_header('key_without_value')
#     {'key_without_value': None}
# 
#     To create a header from the :class:`dict` again, use the
#     :func:`dump_header` function.
# 
#     :param value: a string with a dict header.
#     :return: :class:`dict`
#     """
#     result = {}
#     for item in _parse_list_header(value):
#         if '=' not in item:
#             result[item] = None
#             continue
#         name, value = item.split('=', 1)
#         if value[:1] == value[-1:] == '"':
#             value = unquote_header_value(value[1:-1])
#         result[name] = value
#     return result


import time
import hashlib
from base64 import b64encode
from urlparse import urlparse
from .utils import randombytes, parse_dict_header

            algorithm = algorithm.upper()
            # lambdas assume digest modules are imported at the top level
            if algorithm == 'MD5':
                H = lambda x: hashlib.md5(x).hexdigest()
            elif algorithm == 'SHA':
                H = lambda x: hashlib.sha1(x).hexdigest()
            # XXX MD5-sess
            KD = lambda s, d: H("%s:%s" % (s, d))

            if H is None:
                return None

            # XXX not implemented yet
            entdig = None
            p_parsed = urlparse(r.request.url)
            path = p_parsed.path + p_parsed.query

            A1 = "%s:%s:%s" % (username, realm, password)
            A2 = "%s:%s" % (r.request.method, path)

            if qop == 'auth':
                if nonce == last_nonce:
                    nonce_count += 1
                else:
                    nonce_count = 1
                    last_nonce = nonce

                ncvalue = '%08x' % nonce_count
                cnonce = (hashlib.sha1("%s:%s:%s:%s" % (

# Path: pyTOP/packages/requests/utils.py
# def randombytes(n):
#     """Return n random bytes."""
#     # Use /dev/urandom if it is available.  Fall back to random module
#     # if not.  It might be worthwhile to extend this function to use
#     # other platform-specific mechanisms for getting random bytes.
#     if os.path.exists("/dev/urandom"):
#         f = open("/dev/urandom")
#         s = f.read(n)
#         f.close()
#         return s
#     else:
#         L = [chr(random.randrange(0, 256)) for i in range(n)]
#         return "".join(L)
# 
# def parse_dict_header(value):
#     """Parse lists of key, value pairs as described by RFC 2068 Section 2 and
#     convert them into a python dict:
# 
#     >>> d = parse_dict_header('foo="is a fish", bar="as well"')
#     >>> type(d) is dict
#     True
#     >>> sorted(d.items())
#     [('bar', 'as well'), ('foo', 'is a fish')]
# 
#     If there is no value for a key it will be `None`:
# 
#     >>> parse_dict_header('key_without_value')
#     {'key_without_value': None}
# 
#     To create a header from the :class:`dict` again, use the
#     :func:`dump_header` function.
# 
#     :param value: a string with a dict header.
#     :return: :class:`dict`
#     """
#     result = {}
#     for item in _parse_list_header(value):
#         if '=' not in item:
#             result[item] = None
#             continue
#         name, value = item.split('=', 1)
#         if value[:1] == value[-1:] == '"':
#             value = unquote_header_value(value[1:-1])
#         result[name] = value
#     return result


# Path: pyTOP/packages/requests/auth.py
import time
import hashlib
from base64 import b64encode
from urlparse import urlparse
from .utils import randombytes, parse_dict_header


            algorithm = algorithm.upper()
            # lambdas assume digest modules are imported at the top level
            if algorithm == 'MD5':
                H = lambda x: hashlib.md5(x).hexdigest()
            elif algorithm == 'SHA':
                H = lambda x: hashlib.sha1(x).hexdigest()
            # XXX MD5-sess
            KD = lambda s, d: H("%s:%s" % (s, d))

            if H is None:
                return None

            # XXX not implemented yet
            entdig = None
            p_parsed = urlparse(r.request.url)
            path = p_parsed.path + p_parsed.query

            A1 = "%s:%s:%s" % (username, realm, password)
            A2 = "%s:%s" % (r.request.method, path)

            if qop == 'auth':
                if nonce == last_nonce:
                    nonce_count += 1
                else:
                    nonce_count = 1
                    last_nonce = nonce

                ncvalue = '%08x' % nonce_count
                cnonce = (hashlib.sha1("%s:%s:%s:%s" % (

                    nonce_count, nonce, time.ctime(), randombytes(8)))