
    g                     X    d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 dZ
dZdZd	Zd
 Zd Zy)a  

    Securely hash and check passwords using PBKDF2.

    Use random salts to protect againt rainbow tables, many iterations against
    brute-force, and constant-time comparaison againt timing attacks.

    Keep parameters to the algorithm together with the hash so that we can
    change the parameters and keep older hashes working.

    See more details at http://exyr.org/2011/hashing-passwords/

    Author: Simon Sapin
    License: BSD

    )urandom)	b64encode	b64decode)pbkdf2_hmac)compare_digest      sha256i'	 c                 ,   t        | t              r| j                  d      } t        t	        t
                    }dj                  t        t        |j                  d      t        t        t        | |t        t                    j                  d            S )z>Generate a random salt and return a new hash for the password.utf-8zPBKDF2${}${}${}${})
isinstancestrencoder   r   SALT_LENGTHformatHASH_FUNCTIONCOST_FACTORdecoder   
KEY_LENGTH)passwordsalts     &/opt/Tautulli/lib/hashing_passwords.py	make_hashr   #   sq    (C ??7+W[)*D&&G+mXt[*UV]]^ef	h h    c           	      ^   t        | t              r| j                  d      } |j                  d      \  }}}}}|dk(  sJ t	        |j                  d            }t        || |j                  d      t        |      t        |            }t        |      t        |      k(  sJ t        ||      S )z*Check a password against an existing hash.r   $PBKDF2)	r   r   r   splitr   r   intlenr   )r   hash_	algorithmhash_functioncost_factorr   hash_ahash_bs           r   
check_hashr'   /   s    (C ??7+:?++c:J7I}k4   v}}W-.F$++g2FKHXZ]^dZefFv;#f+%%%&&))r   N)__doc__osr   base64r   r   hashlibr   hmacr   r   r   r   r   r   r'    r   r   <module>r.      s;   "  '   
 	h	*r   