# Path: werkzeug/_compat.py
# PY2 = sys.version_info[0] == 2
#     def fix_tuple_repr(obj):
#         def __repr__(self):
#     def implements_iterator(cls):
#     def implements_to_string(cls):
#     def native_string_result(func):
#         def wrapper(*args, **kwargs):
#     def implements_bool(cls):
#     def make_literal_wrapper(reference):
#     def normalize_string_tuple(tup):
#     def try_coerce_native(s):
#     def wsgi_decoding_dance(s, charset='utf-8', errors='replace'):
#     def wsgi_encoding_dance(s, charset='utf-8', errors='replace'):
#     def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
#     def to_native(x, charset=sys.getdefaultencoding(), errors='strict'):
#     def iter_bytes(b):
#     def reraise(tp, value, tb=None):
#     def make_literal_wrapper(reference):
#     def normalize_string_tuple(tup):
#     def wsgi_get_bytes(s):
#     def wsgi_decoding_dance(s, charset='utf-8', errors='replace'):
#     def wsgi_encoding_dance(s, charset='utf-8', errors='replace'):
#     def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
#     def to_native(x, charset=sys.getdefaultencoding(), errors='strict'):
# def to_unicode(x, charset=sys.getdefaultencoding(), errors='strict',
#                allow_none_charset=False):


import os
import hmac
import hashlib
import posixpath
import codecs
from struct import Struct
from random import SystemRandom
from operator import xor
from itertools import starmap
from werkzeug._compat import range_type, PY2, text_type, izip, to_bytes, \
     string_types, to_native
    a different hashlib `hashfunc` can be provided.

    .. versionadded:: 0.9

    :param data: the data to derive.
    :param salt: the salt for the derivation.
    :param iterations: the number of iterations.
    :param keylen: the length of the resulting key.  If not provided
                   the digest size will be used.
    :param hashfunc: the hash function to use.  This can either be the
                     string name of a known hash function or a function
                     from the hashlib module.  Defaults to sha1.
    """
    if isinstance(hashfunc, string_types):
        hashfunc = _hash_funcs[hashfunc]
    elif not hashfunc:
        hashfunc = hashlib.sha1
    salt = to_bytes(salt)
    mac = hmac.HMAC(to_bytes(data), None, hashfunc)
    if not keylen:
        keylen = mac.digest_size
    def _pseudorandom(x, mac=mac):
        h = mac.copy()
        h.update(x)
        return bytearray(h.digest())
    buf = bytearray()
    for block in range_type(1, -(-keylen // mac.digest_size) + 1):
        rv = u = _pseudorandom(salt + _pack_int(block))
        for i in range_type(iterations - 1):
            u = _pseudorandom(bytes(u))

# Path: werkzeug/_compat.py
# PY2 = sys.version_info[0] == 2
#     def fix_tuple_repr(obj):
#         def __repr__(self):
#     def implements_iterator(cls):
#     def implements_to_string(cls):
#     def native_string_result(func):
#         def wrapper(*args, **kwargs):
#     def implements_bool(cls):
#     def make_literal_wrapper(reference):
#     def normalize_string_tuple(tup):
#     def try_coerce_native(s):
#     def wsgi_decoding_dance(s, charset='utf-8', errors='replace'):
#     def wsgi_encoding_dance(s, charset='utf-8', errors='replace'):
#     def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
#     def to_native(x, charset=sys.getdefaultencoding(), errors='strict'):
#     def iter_bytes(b):
#     def reraise(tp, value, tb=None):
#     def make_literal_wrapper(reference):
#     def normalize_string_tuple(tup):
#     def wsgi_get_bytes(s):
#     def wsgi_decoding_dance(s, charset='utf-8', errors='replace'):
#     def wsgi_encoding_dance(s, charset='utf-8', errors='replace'):
#     def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
#     def to_native(x, charset=sys.getdefaultencoding(), errors='strict'):
# def to_unicode(x, charset=sys.getdefaultencoding(), errors='strict',
#                allow_none_charset=False):


# Path: werkzeug/security.py
import os
import hmac
import hashlib
import posixpath
import codecs
from struct import Struct
from random import SystemRandom
from operator import xor
from itertools import starmap
from werkzeug._compat import range_type, PY2, text_type, izip, to_bytes, \
     string_types, to_native

    a different hashlib `hashfunc` can be provided.

    .. versionadded:: 0.9

    :param data: the data to derive.
    :param salt: the salt for the derivation.
    :param iterations: the number of iterations.
    :param keylen: the length of the resulting key.  If not provided
                   the digest size will be used.
    :param hashfunc: the hash function to use.  This can either be the
                     string name of a known hash function or a function
                     from the hashlib module.  Defaults to sha1.
    """
    if isinstance(hashfunc, string_types):
        hashfunc = _hash_funcs[hashfunc]
    elif not hashfunc:
        hashfunc = hashlib.sha1
    salt = to_bytes(salt)
    mac = hmac.HMAC(to_bytes(data), None, hashfunc)
    if not keylen:
        keylen = mac.digest_size
    def _pseudorandom(x, mac=mac):
        h = mac.copy()
        h.update(x)
        return bytearray(h.digest())
    buf = bytearray()
    for block in range_type(1, -(-keylen // mac.digest_size) + 1):
        rv = u = _pseudorandom(salt + _pack_int(block))
        for i in range_type(iterations - 1):
            u = _pseudorandom(bytes(u))

            rv = bytearray(starmap(xor, izip(rv, u)))